Reducing Enumerable — Part Two: Chartreuse, Master of Map

This brings us to part two of Reducing Enumerable where we meet Chartreuse, the master of Map.

Table of Contents

  1. The Journey Begins
  2. Chartreuse — The Master of Map
  3. Indigo — The Master of Select
  4. Violet — The Master of Find
  5. Cerulean — The Master of Tally By
  6. A Final Lesson from Scarlet

<< Previous | Next >>

Chartreuse — Master of Map

So Red journeyed through the forests of transformation, deep into the groves where the mushrooms bloomed and the creeks ran a sparkling blue. There he found a path, and at the end was a house, and outside of it was the first master he was to meet.

Inside the house was Chartreuse, the master of Map.

“Welcome, Red. I’ve heard much about your journey from Master Scarlett.” said Chartreuse.

Red peered into the potion that Chartreuse was making, trying to make sense of what form of wizardry arts she was performing, so he asked: “What amazing thing are you crafting?”

“Mmm? Oh! This. It’s just food for my kitties, you see it’s time for their supper so I have to set to transform a list of ingredients into something they might find a bit more enjoyable.” said Chartreuse.

“And the scroll?” asked Red.

“Just a recipe, the kitties helped me make it. They’re quite talented at cooking when given some incentive.” answered Chartreuse.

“Wise master, can you tell me how map works so I can implement it with reduce?” asked Red

“Let me show you, young one, the ways of mapping” said Chartreuse

“To map is to apply a function to every element of a list, getting back a new list.” said Chartreuse.

How to Map

So then Chartreuse began to explain map. Shall we take a look?

“Such that we have a list of one two and three, and we wish to multiply every element by two. That is our function.” said Chartreuse.

“So each element of the list is doubled, and added to a new list! This means that the original list is left intact, unchanged!” exclaimed Chartreuse.

Map with Reduce

With that, Red decided to attempt mapping himself.

Given that Red now had an array instead of a number to work with, he would have to use push to join an element to the accumulator, a.

But before he could push a new element onto an array, he would need to apply a function to it. Applying a function was essentially using call to call a block function in Ruby.

All together it might look a little something like this, but let’s break it down a bit and see what we’re doing here.

First we have our list, much like our original reduce, except we’re taking it as an argument to our new method version of map. Each value in our list goes into our reduce as v.

Next we have our accumulator, which is now an array. In our reduce, it’s seen as a.

To add elements, or rather join them to our accumulator array, we use push which adds them to the end of the list.

…but not before we apply, or call, our block function we receive from calling the method on the value.

Which all together means that we push onto our new list the result of calling a function on each element of our list.

Seen together, this makes a bit more sense now what’s going on, but how does the data flow through this function? How do we call it? Let’s take a look.

Unlike Enumerable#map, our new map function takes a list as an argument. After that, much like Enumerable#map we take a block function and end up returning the result of doubling every number in our original list without mutating it.

Meaning that the data would flow through our function a bit like this:

  1. We start with an empty array, and we push onto it the result of our first element, 1, applied to our function v * 2, which means the value 2 gets pushed onto our accumulator.
  2. We start with an accumulator [2], and we push onto it the result of doubling 2, giving us a new accumulator of [2, 4].
  3. We start with an accumulator [2, 4], and we push onto it the result of doubling 3, giving us a final accumulator of [2, 4, 6].
  4. Now that we’ve run out of elements, our final accumulator is returned.

What is Map?

To use map is to apply a function to every element of a list to get a new list.

To implement map with reduce, you need to apply a function to each element of a list before adding it to a new list.


Now that Red had learned from Chartreuse, it was time to visit Master Indigo of Select.

<< Previous | Next >>