Courses/Functional Python

λ Functional Python

functools: reduce, cache, partial

Fold sequences, memoize results, pre-fill arguments.

functools gives you the classic functional toolkit.

from functools import reduce, lru_cache, partial

total = reduce(lambda a, b: a + b, [1, 2, 3, 4])  # 10

@lru_cache
def fib(n):
    return n if n < 2 else fib(n - 1) + fib(n - 2)

add5 = partial(lambda a, b: a + b, 5)
main.py
Output
Press Run to execute.
Expected output
120

Sign in to track your progress across lessons.