Programming languages / Map, filter and reduce

Map, filter and reduce

Three small tools that replace most loops over a list, shown on the list 5, 2, 8, 3: change every item, keep some, fold them into one value, then chain all three.

Beginner7 steps~6 min
Item — one value of the original list
Function — the small rule applied to each item
New item — a value produced by map
Accumulator — the running value reduce carries
// THE SHORT VERSION

In 30 seconds

Map, filter and reduce each take a list and a small function. Map applies the function to every item and returns a new list of the results. Filter keeps only the items for which the function answers yes.

Reduce carries a running value, the accumulator, through the list and returns it at the end, turning many items into one.

Because map and filter return lists, the three can be chained into a pipeline that reads like a description of the job. A loop can do the same work; the chain just names each step.

Map, filter and reduce, step by step

STEPS01 / 07

Shown in JavaScript; most languages have the same three under these or other names (reduce is also called fold). If the start value is left out, JavaScript uses the first item instead.

STEP 01 / 07 One list, three jobs
THE MATH
nums = [5, 2, 8, 3] map: change each item filter: keep some items reduce: combine into one
  1. Step 1: One list, three jobs. Most work on a list is one of three jobs: change every item, keep only some of them, or boil them all down to a single value. Map, filter and reduce are ready-made tools for exactly these jobs. Each is given a small function and applies it across the whole list.
  2. Step 2: Map: change every item. Map runs the function on each item in turn and collects the answers in a new list. Here the function doubles its input, so 5, 2, 8, 3 becomes 10, 4, 16, 6. The new list always has the same length and order as the old one, and the old one is left as it was.
  3. Step 3: Filter: keep some items. Filter asks a yes-or-no question about each item. Here the question is “is it greater than 4?”. Items that get a yes go into the new list, the rest are left out. The kept items are never altered and stay in their original order; only the length can change.
  4. Step 4: Reduce: carry a total. Reduce walks the list while carrying one value along, called the accumulator. Its function takes two inputs, the accumulator so far and the next item, and returns the new accumulator. You also supply the starting value, here 0. The first call adds the first item: 0 + 5 = 5.
  5. Step 5: Follow the accumulator. Step through the whole list. The accumulator starts at 0 and takes in one item per call: 5, then 7, then 15, then 18. When the items run out, the accumulator is the result. Adding is only one choice: with other functions the same pattern finds a maximum, counts items or builds a new list.
  6. Step 6: Chain them together. Map and filter each return a list, so another tool can be called straight on the result. Reading top to bottom: keep the numbers above 4, double them, add them up. The list shrinks to 5, 8, turns into 10, 16, and reduce folds that into 26.
  7. Step 7: The same job as a loop. A plain loop does the same work in one pass: test each item, double it, add it to a running total. The chain says the same three things as three named stages, with no counter or total to manage by hand. It builds two short-lived lists on the way, and unlike a loop it cannot stop early.
VALUES SO FAR
nums[5, 2, 8, 3]input
map····x * 2
filter····x > 4
start····acc
reduce····sum
chain····result
loop····same

Spotted a mistake on this page?