Note: This is a mult-part series! You're going to benefit the most following it from the very beginning, since we re-use code and concepts from previous posts to build our newer ones.

Back to Basics: Functional Programming

Alright, we're at the end of this journey!

From Google:

MapReduce is a programming model and an associated implementation for processing and generating large data sets. Users specify a map function that processes a key/value pair to generate a set of intermediate key/value pairs, and a reduce function that merges all intermediate values associated with the same intermediate key.

How does this apply to the functional programming we've been diving into? Well, we're going to create a MapReduce function.

Like its name implies, we're going to map an array, and then reduce.

Let's say I still have this array of pets:

var pets = ['dog', 'cat', 'parrot', 'hamster', 'lizard']

And remember count?

function count(string) {  
  return string.length;
}

Now, for reminder, map and reduce:

function map(arr, otherFunction) {  
  // Create an array to stire our values
  var result = [];

  // Call `each`, which iterates over our values and fires a callback:
  each(arr, function(elem) {
    // We're using an anonymous function here to handle the pushing of results. We're still calling otherFunction, passing in the element being iterated over.
    result.push(otherFunction(elem));
  })
  // At the end, we want to return the actual array
  return result;
}
function reduce(array, callback, startValue) {  
  each(array, function(elem) {
    if (!startValue) {
      startValue = elem;
    } else {
      startValue = callback(startValue, elem);
    }
  })

  return startValue;
}

Great. I'd like to get the sum of the number of all the characters within my pets array. How can I do this? Well, I remember that I can use map to first give me an array of numbers to work with:

I'll call the following:

map(pets, count) // returns [3, 3, 6, 7, 6]

I'd like to also run reduce immediately afterwards, without having to store the results of my map function into a variable to use later on. How can I do this?

Well, I can't perform method chaining because I'm not operating on any prototype chains here and I'd like to avoid using native Javascript Array prototype methods if I can.

Here's how I'm going to handle it. I'm going to call reduce. Its first parameter is going to be an array, but it's not going to be set yet. It's going to be the result that map returns me. But again, I'm not going to store it as a variable.

Instead, I'm going to call map within the parameters of reduce:

reduce(map(pets, count), function(a, b) {  
  return a + b;
}, 0) // returns 25

Wait, what? How does this even work?

Let's break it down, inside out.

map works the same way it always has. When passed pets and count, it still returns [3, 3, 6, 7, 6] like normal. When reduce is run with map(pets, count) within it, it evaluates that function and operates on the array that gets returned.

It's tricky, but give it a shot in the console.

And that's it! Thanks for following along.