🧮 Map, Filter, Reduce¶
These three functions are classic tools in functional programming:
- map: transform each element
- filter: keep elements that match a condition
- reduce: combine all elements into one value
✅ map¶
Apply a function to every element.
✅ filter¶
Keep items that satisfy a predicate.
✅ reduce¶
Reduce combines values into a single result. In Python, it lives in functools.
from functools import reduce
nums = [1, 2, 3, 4]
total = reduce(lambda acc, x: acc + x, nums, 0)
# 10
✅ Alternatives¶
Python also offers comprehensions, which are often clearer:
🔍 Key Takeaways¶
maptransforms,filterselects,reduceaggregates.- Use lambdas for short logic, named functions for clarity.
- Comprehensions are often more readable in Python.