Created
August 18, 2013 11:55
-
-
Save alucky0707/6261281 to your computer and use it in GitHub Desktop.
演算子を関数にする関数 ref: http://qiita.com/alucky0707/items/2e38690acdd9f0eb2725
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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;')); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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