Skip to content

Instantly share code, notes, and snippets.

@alucky0707
Created August 18, 2013 11:55
Show Gist options
  • Save alucky0707/6261281 to your computer and use it in GitHub Desktop.
Save alucky0707/6261281 to your computer and use it in GitHub Desktop.
演算子を関数にする関数 ref: http://qiita.com/alucky0707/items/2e38690acdd9f0eb2725
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;'));
}
var
xs = [1,2,3,4,5];
console.log(xs.reduce(op('*'))); //=> 120
console.log(xs.map(op('+')(10))); //=> [11,12,13,14,15]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment