DEV Community

Discussion on: Explain it to me like I'm five: .map, .reduce, & .filter edition

Collapse
 
alainvanhout profile image
Alain Van Hout • Edited

Think of it like a car factory, where stuff goes in and cars come out, with long assembly lines all over the place. Along these assembly lines, there are places where stuff goes into a machine, and other stuff comes out at the other end.

Closely observe one of those machines: on the left side, pieces of raw metal go in, and on the right side, for each of those pieces of raw metal, a metal screw comes out. Some of the metal pieces are copper, other are steel, but all come out as the same kind of screw (though in different kinds of metal). In essence, the machine takes in raw metal pieces and transformed them into screw. That machine is map.

There are other machines that take in the screw, and then look at the quality of the screw. It lets pass the ones that are okay, but removes the bad ones (without transforming the screws in any way). You are currently looking a filter machines.

Finally, there are also other machines that take in lots of pieces, and output a single combined piece. It doesn't just lump them together all at once, but rather

  1. starts with a single piece that it's given
  2. takes a second piece and combines it with the first to form a new single piece
  3. takes a third piece and combines that with the newly created piece, to form another newly created piece
  4. repeats this until there are no more pieces to be had
  5. puts the lastly created single piece on the assembly line to continue on

This is a reduce machine.