Skip to content

Instantly share code, notes, and snippets.

@JiriChara
Created November 14, 2012 21:42
Show Gist options
  • Save JiriChara/4075031 to your computer and use it in GitHub Desktop.
Save JiriChara/4075031 to your computer and use it in GitHub Desktop.
Javascript Currying
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