Map, Filter, Reduce
Lambda function
Map
numbers = [1, 2, 3]
def double():
return a * 2
# result = map(double, nubmers)
result = map(lambda a : a * 2, nubmers)
print(list(result)) # [2, 4, 6]
Filter
numbers = [1, 2, 3]
def isEvent(n):
return n % 2 == 0
# result = map(double, nubmers)
result = filter(lambda n : n % 2 == 0, nubmers)
print(list(result)) # [2]
Reduce
from functools import reduce
expenses = [
('Dinner', 80),
('Car repair', 120)
]
sum = 0
for expense in expenses:
sum += expense[1]
print(sum) # 200
sum2 = reduce(lambda a, b : a[1] + b[1], expenses) # 200