Lambda function

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Feb 25 10:10:30 EST 2009


En Wed, 25 Feb 2009 12:42:32 -0200, Albert Hopkins  
<marduk at letterboxes.org> escribió:
> On Wed, 2009-02-25 at 17:56 +0530, aditya saurabh wrote:

>> I defined two functions - lets say
>> fa = lambda x: 2*x
>> fb = lambda x: 3*x
>> Now I would like to use fa*fb in terms of x
>> is there a way?
>> Thanks in advance
>
> I'm not sure what "use fa*fb in terms of x" means.
>
> But if you mean fa(x) * fb(x) then it's just:
>
>     fa(x) * fb(x)

I think he wants function composition, fb(fa(x)):

def compose(*funcs):
   def composed(x, funcs=funcs):
     for f in reversed(funcs):
       x = f(x)
     return x
   return composed

def square(x): return x**2
def plus1(x): return x+1
# same as plus1 = lambda x: x+1 but I like the def syntax

y = compose(square, plus1) # y=(x+1)**2
y(3) # -> 16

(or is it fa(fb(x))?)

-- 
Gabriel Genellina




More information about the Python-list mailing list