jhusain/learnrx — pull request #18 by frhack (2014-02-06 15:15:03+00:00)
Title: Exercise13 

Please consider this new version of Exercise 13.

Exercise 13 Implement flatMap using map and mergeAll
The actual answer is:

Array.prototype.flatMap = function(projectionFunctionThatReturnsArray) {
return this.
map(function(item) {
return projectionFunctionThatReturnsArray(item);
}).
// apply the mergeAll function to flatten the two-dimensional array
mergeAll();
};

The proposed answer:

Array.prototype.flatMap = function(projectionFunctionThatReturnsArray) {
return this.
map(projectionFunctionThatReturnsArray).
// apply the mergeAll function to flatten the two-dimensional array
mergeAll();
};

Better example:

/*
var spanishFrenchEnglishWords = [
{'language':'spanish', 'words':["uno","dos","tres"]},
{'language':'french', 'words': ["un","deux","trois"]},
{'language':'english', 'words':["one","two","three"]}
];
// collect spanish and french words in a single, flat list
var spanishFrenchWords = [0,1].
flatMap(function(index) {
return (spanishFrenchEnglishWords[index].words);
});

return JSON.stringify(spanishFrenchWords) === '["uno","dos","tres","un","deux","trois"]';
*/