combining several lambda equations

P

Paddy McCarthy

Hi,
I am trying to use eval as little as possible but solve this problem.

#If given:two or more lambda equations
x=lambda : A < B
y=lambda : C+6 >= 7
....

How do I create another lambda expression Z equivalent to

Z=lambda : (A<B) and (C+6>=7)

# i.e. the anding together of the originals, but without referencing
# globals x and y as they are artificial in that I will start of with
# probably a list of lambda equations.

Your help would be appreciated.
Thanks, Paddy.
 
F

Fredrik Lundh

Paddy said:
#If given:two or more lambda equations
x=lambda : A < B
y=lambda : C+6 >= 7

How do I create another lambda expression Z equivalent to

Z=lambda : (A<B) and (C+6>=7)

# i.e. the anding together of the originals, but without referencing
# globals x and y as they are artificial in that I will start of with
# probably a list of lambda equations.

x=lambda : A < B
y=lambda : C+6 >= 7
Z=lambda x=x, y=y: x() and y()
del x, y

</F>
 
P

paddy3118

Fredrik said:
x=lambda : A < B
y=lambda : C+6 >= 7
Z=lambda x=x, y=y: x() and y()
del x, y

</F>

Thanks Frederik.

I actually have a set of lambdas so my use will be more like:

s = set([lambda : A < B, lambda : C+6 >= 7])
x=s.pop(); y=s.pop()
Z=lambda x=x, y=y: x() and y()
del x,y
A,B,C = [2,3,1]
Z() True
A,B,C = [2,3,0]
Z() False
A,B,C = [3,3,1]
Z() False

- Gosh, isn't life fun!

- Pad.
 
P

Peter Otten

I actually have a set of lambdas so my use will be more like:

A set of lambdas gains you nothing.
(lambda: a > 0) in set([lambda: a > 0])
False

is probably not what you expected. So you might want to go back to strings
containing expressions. Anyway, here is a way to "and" an arbitrary number
of functions (they all must take the same arguments):
.... def all_true(*args, **kw):
.... for f in functions:
.... if not f(*args, **kw):
.... return False
.... return True
.... return all_true
....False

For a set/list of lambdas/functions, you would call make_and() with a
preceding star:

and_all = make_and(*some_set_of_functions)
- Gosh, isn't life fun!

I seem to remember that the manual clearly states otherwise :)

Peter
 
S

Steven Bethard

Paddy said:
x=lambda : A < B
y=lambda : C+6 >= 7
[snip]

Z=lambda : (A<B) and (C+6>=7)

See "Inappropriate use of Lambda" in
http://www.python.org/moin/DubiousPython

Perhaps your real example is different, but notice that
<name> = lambda <args>: <expr>
is equivalent to
def <name>(<args>):
return <expr>
except that the latter will give your function a useful name. No reason
to use the *anonymous* function construct to create a *named* function.

STeVe
 
P

Paddy

Steve,
Thanks for the info but I do know about that..
What I am doing is taking a set of inputted functions that don't
take arguments and programmatically analysing them and
combining them to create new functions that are further analysed.

During testing I keep the numbers low, and am only dealing with one to
two hundred equations, but real life problems could involve maybe
thousands of them., (and then I'd ptobably shift to using tuples of
ints or tuples of strings as 'handles' or keys to
my generated functions, to convey more info on how
intermediate functions are generated).

Thanks again for the interest,
- Paddy.
 
A

Antoon Pardon

Op 2005-02-18 said:
Paddy said:
x=lambda : A < B
y=lambda : C+6 >= 7
[snip]

Z=lambda : (A<B) and (C+6>=7)

See "Inappropriate use of Lambda" in
http://www.python.org/moin/DubiousPython

Perhaps your real example is different, but notice that
<name> = lambda <args>: <expr>
is equivalent to
def <name>(<args>):
return <expr>
except that the latter will give your function a useful name. No reason
to use the *anonymous* function construct to create a *named* function.

So and if I have code like this:

f = lamda x:x
for g in some_iter:
f = compose(g,f)


Do you still think that one should use a named function in this case?
 
S

Steven Bethard

Antoon said:
So and if I have code like this:

f = lamda x:x
for g in some_iter:
f = compose(g,f)

Do you still think that one should use a named function in this case?

Yes. If you really don't like taking two lines, Python still allows you
to write this as:

def f(x): return x
for g in some_iter:
f = compose(g, f)

On the other hand, if you really love FP enough to write this kind of
code, shouldn't you be using reduce istead? ;) I'm horrible with
reduce, but something like:

def identity(x):
return x
f = reduce(compose, some_iter, identity)

or if you want to use lambda (note that my complaint about making an
named function with the anonymous function syntax doesn't apply here):

f = reduce(compose, some_iter, lambda x: x)

Not sure if the order of composition is right here, but you get the idea.

STeVe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top