Map, Filter, and Reduce
You'll learn the three essential operations that transform collections without loops or mutation.
The Core Idea
Rather than writing explicit loops with mutable counters and conditionals, functional programming provides three declarative operations that handle most collection transformations:
- Map: Transform each element using a function
- Filter: Keep only elements that satisfy a condition
- Reduce: Combine all elements into a single result
Think of these like assembly-line stations: map modifies each item as it passes through, filter removes items that don't meet quality standards, and reduce packages everything into a final product.
How They Work
Map applies a function to every element, producing a new collection of the same size. If you have [1, 2, 3] and map with double, you get [2, 4, 6].
Filter tests each element with a predicate (a function returning true/false), keeping only those that pass. Filtering [1, 2, 3, 4] with isEven yields [2, 4].
Reduce (also called "fold") combines elements using a binary function and an initial value, accumulating a single result. Reducing [1, 2, 3] with add and starting at 0 gives 6.
Why They Matter
These three operations form the foundation of declarative collection processing. They're composable—you can chain them together—and they eliminate the need for explicit loop variables, indices, and temporary mutable state. Most data transformations can be expressed as combinations of map, filter, and reduce.
Key Takeaway: Map transforms elements, filter selects elements, and reduce combines them—three pure, composable operations that replace most imperative loops.