Skip to content

Instantly share code, notes, and snippets.

@aldonline
Created February 7, 2015 02:12
Show Gist options
  • Save aldonline/b2b723efca26170b7cc7 to your computer and use it in GitHub Desktop.
Save aldonline/b2b723efca26170b7cc7 to your computer and use it in GitHub Desktop.
##1 How-to
Everybody knows what **looping** over a collection is. But do you know what **reducing** a collection means?
No? Are you sure?
It sounds scary. But it is in fact pretty simple. You do it all the time.
Here. Let me show you an example:
<!--code lang=javascript linenums=true-->
var total = 0;
var numbers = [1, 5, 7, 3, 8, 9];
for ( var i = 0; i < numbers.length; i++ ){
total += numbers[i];
}
That's it. You just **reduced the "numbers" collection into the "total" variable**.
Pretty simple huh?
Let me show you another example:
<!--code lang=javascript linenums=true-->
var message = "";
var words = ["reducing", "is", "simple"];
for ( var i = 0; i < words.length; i++ ){
message += words[i];
}
You just **reduced the "words" collection into the "message" variable**.
You see the pattern right?
You start with a **collection** ( words ) and a **variable** ( message ) with an **initial value** ( the empty string in this case ). You then iterate over the collection and append ( or add ) the values to the variable.
So what's the big deal, you ask? Well. Check this out:
<!--code lang=javascript linenums=true-->
var sum = [1, 2, 3].reduce( function(total, num){ return total + num }, 0);
Whoa! So the Array object already knows how to reduce?
Indeed!
Every time you find yourself going from a list of values to one value ( reducing ) ask yourself if you could rewrite it using `Array.reduce()`. You can reduce with any sort of operation that combines two values. Not just addition. And not just arithmetic operations.
So you know what Array reduce means. Isn't that awesome? It only took you one minute.
##2 Bonus
When you hear people talking about "Map Reduce" they are just talking about a "pattern": Mapping over a collection and then reducing it. Yeah. Really. Now you know.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment