Skip to content

Instantly share code, notes, and snippets.

@fwg
Forked from puffnfresh/currying.js
Last active December 15, 2015 04:50
Show Gist options
  • Save fwg/5204917 to your computer and use it in GitHub Desktop.
Save fwg/5204917 to your computer and use it in GitHub Desktop.
function curry(f) {
return function() {
if(arguments.length < f.length)
return curry(f.bind.apply(f, [this].concat([].slice.call(arguments))));
return f.apply(this, arguments);
};
}
var sum = curry(function(x, y) {
return x + y;
});
var incr = sum(1);
console.log(incr(2));
console.log(sum(1)(2));
console.log(sum(1, 2));
var tuple4 = curry(function(a, b, c, d) {
return [a, b, c, d];
});
console.log(tuple4(1)(2, 3, 4));
console.log(tuple4(1, 2)(3, 4));
console.log(tuple4(1, 2)(3)(4));
console.log(tuple4(1, 2, 3)(4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment