Say we have
var i = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
and want to reduce()
it like
var plus = function(a, b)
{
return a + b;
};
var s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
.reduce(plus);
console.log(s);
Now, I want to compose reduce()
function itself from map()
function.
How would you do that? What is the smartest way?
Edit:
In the comments and answers, many have claimed fold
/reduce
can compose map, in shallow level, that can be true, however, in category theory, fundamentally reduce/fold is generalized to Catamorphism and it's all about functors (map
), and it's called F-algebra.