Recally lambda
from last lecture:
f = lambda x,y,... : ...some_expression_using x and y ...
was a shortcut for a function defined by:
def f(x,y,...):
return ...some_expression_using_x_y_...
f = lambda x: x*x
f(5)
(lambda x,y : x + y)(2,5)
lambda
comes from Lambda Calculus, which was a model of computation that mathematicians thought about before computers existed. It was invented by Alonso Church (who was the advisor of Alan Turing)
map(f,xs)
, returns: [f(xs[0]), f(xs[1]),...]
. i.e. it applies f to each element of xs.
same as list comprehension:
[f(x) for x in xs]
from math import floor
list(map(floor, [1.234, 2.1234145, 3.42424, 4.525]))
[floor(x) for x in [1.234, 2.1234145, 3.42424, 4.525]]
list(map(lambda x: 2**x, range(15)))
Compare with:
[2**x for x in range(15)]
filter(f, xs)
filters out the elements x
in xs
for which f(x)
is False
:
list(filter(lambda x: x % 3 == 1, range(20)))
[x for x in range(20) if x % 3 == 1]
This is the one that's really new for us because it makes very nice one-liners, but it's in a library called functools
. (it used to be standard in Python 2)
reduce(f, xs)
, takes a function f(x,y)
of two variables, and applies the function first to x[0]
and x[1]
, getting f(x[0], x[1])
. And then applies f
to f(x[0], x[1])
and x[2]
, getting f(f(x[0], x[1]), x[2])
,...
from functools import reduce
reduce(lambda x, y: x+y, range(10))
Let's see what happened:
lambda x, y: x+y
is the addition function. range[10]
is [0,1,2,3,4,5,6,7,8,9]
reduce(lambda x, y: x+y, range(10))
first computes 0+1
, takes the result 1
and adds it to 2
, then takes the result 3
and adds it to the next element 3
.
reduce(lambda x, y: x*y, range(1,10))
$362880$ is $10!$
factorial = lambda n: reduce(lambda x, y : x*y, range(1,n))
factorial(10)
Do you remember the homework problem when we were supposed to write a function dupli(xs,k)
that takes a list and returns the same list but with each element repeated k
times:
xs = [1,2,4,1,2]
diplo = lambda xs, k: reduce(lambda x, y: x+y, map(lambda a: k*[a], xs))
# this is clearly not a good way to implement this function!! It's hard to read.
# still, it's cool that we can do this and it's good exercise for the brain
diplo(xs, 3)
apply(n,f,x)
below doing? apply = lambda n,f,x : reduce((lambda y, g : g(y)),([x] + n*[f]))
apply(4, lambda x: x*3, 3)
apply(3, lambda x: x*x, 2)