1. 作用
函数式编程就是一种抽象程度很高的编程范式。
2. 操作
map(Func, Iterable)函数,函数Func接受一个参数,
>>> r = map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> r
<map at 0x7fa1f4d9b940>
>>> list(r)
[1, 4, 9, 16, 25, 36, 49, 64, 81]
reduce(Func, Iterable), 函数Func接受两个参数
>>> from functools import reduce
>>> reduce(lambda x, y: x + y, [1, 2, 3, 4, 5, 6])
21
map与reduce
def str2int(s):
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, }
return reduce(lambda x, y: x * 10 + y, map(lambda s: DIGITS[s], s ))
str2int('1234577')
1234577