Connecting Tech Pros Worldwide Forums | Help | Site Map

combining several lambda equations

Paddy McCarthy
Guest
 
Posts: n/a
#1: Jul 18 '05
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.

Fredrik Lundh
Guest
 
Posts: n/a
#2: Jul 18 '05

re: combining several lambda equations


Paddy McCarthy wrote:
[color=blue]
> #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.[/color]

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

</F>



paddy3118@netscape.net
Guest
 
Posts: n/a
#3: Jul 18 '05

re: combining several lambda equations


Fredrik Lundh wrote:[color=blue]
> Paddy McCarthy wrote:
>[color=green]
> > #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[/color][/color]
referencing[color=blue][color=green]
> > # globals x and y as they are artificial in that I will start of[/color][/color]
with[color=blue][color=green]
> > # probably a list of lambda equations.[/color]
>
> x=lambda : A < B
> y=lambda : C+6 >= 7
> Z=lambda x=x, y=y: x() and y()
> del x, y
>
> </F>[/color]

Thanks Frederik.

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

[color=blue][color=green][color=darkred]
>>> 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()[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> A,B,C = [2,3,0]
>>> Z()[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>> A,B,C = [3,3,1]
>>> Z()[/color][/color][/color]
False[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]

- Gosh, isn't life fun!

- Pad.

Peter Otten
Guest
 
Posts: n/a
#4: Jul 18 '05

re: combining several lambda equations


paddy3118@netscape.net wrote:
[color=blue]
> I actually have a set of lambdas so my use will be more like:[/color]

A set of lambdas gains you nothing.
[color=blue][color=green][color=darkred]
>>> (lambda: a > 0) in set([lambda: a > 0])[/color][/color][/color]
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):
[color=blue][color=green][color=darkred]
>>> def make_and(*functions):[/color][/color][/color]
.... def all_true(*args, **kw):
.... for f in functions:
.... if not f(*args, **kw):
.... return False
.... return True
.... return all_true
....[color=blue][color=green][color=darkred]
>>> abc = make_and(lambda: a > 0, lambda: b < 0, lambda: c == 0)
>>> a, b, c = 1, -1, 0
>>> abc()[/color][/color][/color]
True[color=blue][color=green][color=darkred]
>>> c = 1
>>> abc()[/color][/color][/color]
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)
[color=blue]
> - Gosh, isn't life fun![/color]

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

Peter


Steven Bethard
Guest
 
Posts: n/a
#5: Jul 18 '05

re: combining several lambda equations


Paddy McCarthy wrote:[color=blue]
> x=lambda : A < B
> y=lambda : C+6 >= 7
>[/color]
[snip][color=blue]
>
> Z=lambda : (A<B) and (C+6>=7)[/color]

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
Paddy
Guest
 
Posts: n/a
#6: Jul 18 '05

re: combining several lambda equations


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.

Antoon Pardon
Guest
 
Posts: n/a
#7: Jul 18 '05

re: combining several lambda equations


Op 2005-02-18, Steven Bethard schreef <steven.bethard@gmail.com>:[color=blue]
> Paddy McCarthy wrote:[color=green]
>> x=lambda : A < B
>> y=lambda : C+6 >= 7
>>[/color]
> [snip][color=green]
>>
>> Z=lambda : (A<B) and (C+6>=7)[/color]
>
> 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.[/color]

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?

--
Antoon Pardon
Steven Bethard
Guest
 
Posts: n/a
#8: Jul 18 '05

re: combining several lambda equations


Antoon Pardon wrote:[color=blue]
> 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?[/color]

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
Closed Thread


Similar Python bytes