combining several lambda equations

Steven Bethard steven.bethard at gmail.com
Mon Feb 21 13:44:36 EST 2005


Antoon Pardon wrote:
> 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



More information about the Python-list mailing list