うう、日本語がややこしい。
JavaScriptを関数型的に使いたい時に結構役に立つ。ついでにカリー化。
op2fn.js
function curry (fn) {
return function curried() {
var
args = Array.prototype.slice.call(arguments, 0);
return args.length >= fn.length ? fn.apply(this, arguments)
: function () {
return curried.apply(this, args.concat(Array.prototype.slice.apply(arguments)));
};
};
}
function op(o) {
return curry(Function('x, y', 'return x ' + o + 'y;'));
}
使い方。
op2js-usage.js
var
xs = [1,2,3,4,5];
console.log(xs.reduce(op('*'))); //=> 120
console.log(xs.map(op('+')(10))); //=> [11,12,13,14,15]
カリー化されてるからmapにも渡せる。