Skip to content

Instantly share code, notes, and snippets.

@bttmly
Forked from mkuklis/gist:5294248
Created August 13, 2014 20:28
Show Gist options
  • Save bttmly/a40dae511dcf62c8d66c to your computer and use it in GitHub Desktop.
Save bttmly/a40dae511dcf62c8d66c to your computer and use it in GitHub Desktop.
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
add(2)(3, 4)(5); // 14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment