# partial( )

``````def exp(base,  power):
return base ** power # 这是一个二元函数
``````

``````def two_to_the(power):
return exp(2,power)
print two_to_the(3) # 8
``````

``````from functools import partial
two_to_the = partial(exp, 2)
print two_to_the(3) # 8
``````

``````square_of = partial(exp, power=2)
print square_of(3) # 9
``````

# lambda( )

lambda( )主要用于定义“行内函数”，有点像Matlab的“匿名函数”，具体的操作如下：

``````f = lambda x : x + 2 # 定义函数 f(x)=x+2
g = lambda x, y : x + y # 定义函数 g(x, y)=x+y
``````

# map( )

map( ) 函数用于逐一遍历。例如我们有一个list a = [1, 2, 3, 4], 要给 a 中的每一个元素加2得到一个新的list，有两种方式：

``````b = [i+2 for i in a] # list comprehension
``````
``````b = map(lambda x : x+2, a) # map( )
``````

``````products = map(lambda x, y : x * y, [1, 2], [3, 4]) # [1*3, 2*4] = [3, 8]
``````

# reduce( )

``````multiply = reduce(lambda x, y : x * y, [1, 2, 3, 4]) # 1 * 2 * 3 * 4 = 24
``````

reduce( ) 结合了列表的两个元素，它们的结果又结合列表的第3个元素，这个结果之后又结合了第4个元素，依次下去，直到得到一个单独的结果。

``````def multiply(x, y):
return x * y
list_product = partial(reduce, multiply) # 将函数 multiply 作为参数传给 reduce
x_product = list_product([1, 2, 3, 4])
``````

# filter( )

``````b = filter(lambda x : x > 5 and x < 8, range(10)) # [6, 7]
``````

``````b = [i for i in range(10) if i > 5 and i < 8]
``````

Stay hungry, Stay foolish. -- Steve Jobs