Skip to content

Instantly share code, notes, and snippets.

@qustosh
Forked from mkuklis/gist:5294248
Created April 2, 2013 17:42
Show Gist options
  • Save qustosh/5294401 to your computer and use it in GitHub Desktop.
Save qustosh/5294401 to your computer and use it in GitHub Desktop.
auto curry in JavaScript
function toArray(args) {
return [].slice.call(args);
}
function autocurry(fn) {
var len = fn.length;
var args = [];
return function next() {
args = args.concat(toArray(arguments));
return (args.length >= len) ?
fn.apply(this, args.splice(0)) :
next;
}
}
// usage
var add = autocurry(function (a, b, c, d) {
return a + b + c + d;
});
add(1)(2)(3)(4); // 10
var one = add(1);
one(4, 5, 6) // 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment