map() vs. mapMany() was something that I struggled with, however the information in the tutorial is helpful. In exercise 13, we actually write the code for mapMany(), which you can see is simply a map operation followed by a mergeAll() operation. In JavaScript:
Array.prototype.mapMany = function(projectionFunctionThatReturnsArray) {
return this.
map(function(item) {
// Apply the projection function to each item. The projection
// function will return a new child array. This will create a
// two-dimensional array.
return projectionFunctionThatReturnsArray(item)
}).
// apply the mergeAll function to flatten the two-dimensional array
mergeAll();
};
Further, this exercise provides this example output, which is also helpful:
var spanishFrenchEnglishWords = [ ["cero","rien","zero"], ["uno","un","one"], ["dos","deux","two"] ];
// collect all the words for each number, in every language, in a single, flat list
var allWords = [0,1,2].
mapMany(function(index) {
return spanishFrenchEnglishWords[index];
});
return JSON.stringify(allWords) === '["cero","rien","zero","uno","un","one","dos","deux","two"]';
If we had instead used the map() function instead of the mapMany() function in the example above, then allWords would simply equal the original spanishFrenchEnglishWords array since no merge operation would occur.
-Greg