2 ms·
Do you mean like this? var sum = function (a) { return _(a).reduce(function (m, n) { return m + n; }, 0); }; sum([1,2,3,4,5,6,7,8,
by shawndumas 16y ago
Do you mean like this?
var sum = function (a) {
return _(a).reduce(function (m, n) {
return m + n;
}, 0);
};
sum([1,2,3,4,5,6,7,8,9]); // => 45
Or am I not getting you?
- steadicat 16y agoThat's one way to do it, but you're falling back to explicitly defining a new function, rather than creating it by combining higher-order functions. Which kinda defeats the point.
- shawndumas 16y agoAll I would have to do is abstract the outer function out and call it curry (partial[1], really). var add = function (m, n) { return m + n; }, partial = function (mth, fn) { return function (l) { return _(l)[mth](fn, 0); }; }, sum = partial('reduce', add); sum([1,2,3,4,5,6,7,8,9]); // => 45 ----- [1]: http://lambda-the-ultimate.org/node/2266 http://lambda-the-ultimate.org/node/2266