ELI5: The filter, map, zip, and reduce operations
Tags: coding, eli5, beginners, programming
Published: 2018-08-27 22:54:34

---
title: ELI5: The filter, map, zip, and reduce operations
published: true
description: Some common set operations in simple terms
tags: coding, eli5, beginners, programming
cover_image: https://mortoray.files.wordpress.com/2018/08/biscuit-83807_1280.jpg
canonical_url: https://mortoray.com/2018/08/28/eli5-the-filter-map-zip-and-reduce-operations/
---

You've baked a fresh batch of gingerbread cookies. A bunch of little men, snowmen, stars, and bells are cooling on some trays. You clean up the table and lay out the colours and sprinkles in anticipation.

Good, the cookies are cool enough to start decorating. The little men need clothing. The snowmen covered with sugary snow. The stars outlined and filled with yellow. The bells get a simple jagged line pattern.

It's a bit messy to have the colours for all shapes open at once. It'd be better to do one form at a time. Let's start with the snowmen. You look through the cookies, grab the first snowman, put icing and sprinkles on it, then set it to the side. Now look through the cookies and do the same for all the snowmen you find.

Once done, you can pack up the snowman decorations and get the star colours ready. You go through all the cookies again, this time decorating the stars. Then change colours and repeat for the men and bells.

In programmer terms, you're doing two things to the cookies: filter and map. Scanning the big pile of cookies and taking only those of a given form, like snowmen, is called a "filter". You filter out all those you don't want, ending up with only the snowmen.

You decorate each cookie in a specific way. This is called a "map". It doesn't change the number of cookies. You start with a bunch of plain cookies, then end up with the same number of decorated cookies. They all get the same decoration as well. Since we have only one type at a time, like snowmen, we can keep using the same colours for the entire "map" process.

We do the "filter" and "map" once for each of the cookie cutters we used.  This is four times. Once for men, snowmen, stars, and bells.

You're left with four different sets of cookies, one of each shape. It'd be a bit boring to have them all grouped together like that in the cookie boxes.  First, you grab one of each form and stick them in a pile. Then you take each pile and dump it into the box. The piles are gone now, only a box full of mixed cookies remains.

Creating the piles of four cookie forms is sometimes called a "zip" operation. It works like the zipper on your pants, as it connects the left and right side one bit at a time. In this case, we have four things we're combining instead of two. We zip up the four piles of cookies into a bunch of piles, each with four different cookies.

Putting these all together in a box is called "reducing" the sets. We "reduce" the individual piles into a single box of cookies. An interesting thing about reducing is that it doesn't matter what order we grab the piles. We can even group some together in bigger piles first -- to save walking up and down the table too often. They all end up together in the same box.

"filter", "map", "zip" and "reduce", some funny terms for things you do every day. But we've missed something? What else can we do with the cookies?

That's right, we need to eat them. Programmers like to call that, getting the munchies.


----

> If you enjoy my writing then [follow me on Twitter](https://twitter.com/edaqa) or [become a patron](https://www.patreon.com/mortoray). Thank you for reading.