Python supports the creation of anonymous functions (i.e. functions that are not
bound to a name) at runtime, using a construct called "lambda" and is often used
in conjunction with typical functional concepts like filter(), map() and reduce().
This piece of code shows the difference between a normal function definition ("f")
and a lambda function ("g"):
>>> def f (x): return x**2
...
>>> print f(8)
64
>>>
>>> g = lambda x: x**2
>>>
>>> print g(8)
64
As you can see, f() and g() do exactly the same and can be used in the same ways.
Note that the lambda definition does not include a "return" statement -- it always
contains an expression which is returned. Also note that you can put a lambda
definition anywhere a function is expected, and you don't have to assign it to a
variable at all.
-----------------------------
>>> def make_incrementor (n): return lambda x: x + n
>>>
>>> f = make_incrementor(2)
>>> g = make_incrementor(6)
>>>
>>> print f(42), g(42)
44 48
>>>
>>> print make_incrementor(22)(33)
55
The above code defines a function "make_inrementor" that creates an anonymous
function on the fly and returns it. The returned function increments its argument by
the value that was specified when it was created.
You can now create multiple different incrementor functions and assign them to
variables, then use them independent from each other. As the last statement
demonstrates, you don't even have to assign the function anywhere -- you can
just use it instantly and forget it when it's not needed anymore.
The following takes this a step further.
>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo)
139
First we define a simple list of integer values, then we use the standard functions
filter(), map() and reduce() to do various things with that list. All of the three
functions expect two arguments: A function and a list.
-----------------------------
The following example is one way to compute prime numbers in Python (not the most
efficient one, though):
>>> nums = range(2, 50)
>>> for i in range(2, 8):
... nums = filter(lambda x: x == i or x % i, nums)
...
>>> print nums
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
-----------------------------
In the following example, a sentence is split up into a list of words, then a list is
created that contains the length of each word.
>>> sentence = 'It is raining cats and dogs'
>>> words = sentence.split()
>>> print words
['It', 'is', 'raining', 'cats', 'and', 'dogs']
>>>
>>> lengths = map(lambda word: len(word), words)
>>> print lengths
[2, 2, 7, 4, 3, 4]
-----------------------------
Nice tutorial. I was looking for a quick intro to lambdas, and this was really helpful. That being said, the last example doesn't really tell me why I should be using lambdas. It can be expressed more concisely without a lambda construct.
ReplyDeletemap(len, words) == map(lambda word: len(word), words)