Re:Underscore.js: Annotated Source Code
by steadicat on Thursday October 07, 2010 @03:29PM
(#1769122)
Can you do things like
var sum = curry(reduce, add);
with the OO style?Am I wrong in thinking that this kind of currying is one of the fundamental building blocks of functional programming?
Re:Underscore.js: Annotated Source Code
by shawndumas on Thursday October 07, 2010 @04:29PM
(#1769376)
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?
Re:Underscore.js: Annotated Source Code
by steadicat on Thursday October 07, 2010 @04:34PM
(#1769395)
That'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.
Re:Underscore.js: Annotated Source Code
by shawndumas on Thursday October 07, 2010 @06:22PM
(#1769771)
All 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
-----
-----
http://documentcloud.github.com/underscore/#styles