|
|
|
|
|
by hajile
4419 days ago
|
|
In python, it would be proper to say that ALL functions are lambdas except that some/most of those lambdas are assigned to variables via syntactic sugar (this is made very plain in sicp which is the main reason for suggesting it).The only thing different about python vs most other languages is guido refusing to implement multi-line lambdas. As to any complaint, lambdas exist in python (that is, are made accessible to the programmer) for the exact reason python also has a for loop in addition to a while loop -- because they make things easier and more maintainable. def myApplyAdd(func, x):
return lambda y: func(x+y)
applyAddInst = myApplyAdd(lambda z: z*2, 20)
applyAddInst(3) #output 26
becomes much less maintainable without lambdas and pollutes the scope with unnecessary variables you must then keep track of def myApplyAdd(func, x):
def innerAdd (y):
return func(x + y)
return innerAdd
def mulByTwo(x):
return x*2
applyAddInstance = myApplyAdd(mulByTwo, 20)
applyAddInst(3) #output 26
I would also note that list comprehensions make heavy use of lambdas. For example, the following is actually using a for loop and calling lambda i: i**2
each each iteration result = [ i**2 for i in range(3, 30) ]
|
|
If a function is "assigned to a variable" then it's no longer a lambda, because it's bound to an identifier. But I suppose the lines are a tad blurred.