Created
November 14, 2012 21:42
-
-
Save JiriChara/4075031 to your computer and use it in GitHub Desktop.
Javascript Currying
This file contains 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 schonfinkelize(fn) { | |
var slice = Array.prototype.slice, | |
storedArgs = slice.call(arguments, 1); | |
return function () { | |
var newArgs = slice.call(arguments), | |
args = storedArgs.concat(newArgs); | |
return fn.apply(null, args); | |
}; | |
} | |
// Tests: | |
function add(a, b, c, d, e) { | |
return a + b + c + d + e; | |
} | |
schonfinkelize(add, 1, 2, 3)(5, 5); // 16 | |
var addOne = schonfinkelize(add, 1); | |
addOne(10, 10, 10, 10); // 41 | |
var addSix = schonfinkelize(addOne, 2, 3); | |
addSix(5, 5); // 16 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment