函数式编程总结
a = ["shi", "xiao", "gu"]
def map_func(item: str) -> str:
"""
产生新集合
:param item:
:return:
"""
item = "da" if item == "xiao" else item
return item
# map函数
output_res = map(map_func, a)
print(list(output_res))
b = [10, 6, 1, 3, 0, 89]
def filter_func(item: int) -> int:
"""
筛选子集
:param item:
:return:
"""
if item > 3:
return item
# filter函数
filtered = filter(filter_func, b)
print(list(filtered))
from functools import reduce
# reduce函数,产生一个结果
sum_ = reduce(lambda x, y: x + y, [3, 6, 1])
print(sum_)
上面这一部分的手法在重构中称之为:以管道取代循环