Map, Filter, Reduce

Map, filter, and reduce are the three workhorse higher-order functions of functional programming. Each takes a function as an argument and applies it across a collection, so the looping is written once in the library and the caller supplies only the varying behavior. They replace explicit loops in most functional code.

“Structure and Interpretation of Computer Programs,” in its section on sequences as conventional interfaces, defines map plainly: map “takes as arguments a procedure of one argument and a list, and returns a list of the results produced by applying the procedure to each element in the list.” Filter selects elements, keeping “only those elements that satisfy a given predicate.” Reduce, which SICP calls accumulate, combines the elements of a sequence using a two-argument operator and an initial value.

SICP frames these as conventional interfaces: organizing a program around enumeration, filtering, mapping, and accumulation reveals a common structure and lets pieces be composed and reused. A pipeline that maps a transformation, filters out unwanted results, and reduces the rest to a summary expresses a whole computation as a chain of these operations.

The pattern scales beyond single-machine lists. The map and reduce stages name the same two ideas at the heart of large-scale data processing frameworks, where mapping a function over partitioned data and then reducing the partial results lets the same shape of computation run across many machines.