Functional Python
Zhu, Justin

Functional Python
Thu, Feb 21, 2019


Map

map(function_to_apply, list_of_inputs)

items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
funcs = [multiply, add]
for i in range(5):
    value = list(map(lambda x: x(i), funcs))

Filter

number_list = range(-5, 5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero)

#Output : [-5, -4, -3, -2, -1]

Reduce

from functools import

Here, we obtain a reduction for our final product:

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