Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save malkomalko/30a0d23188a7602a4aa5 to your computer and use it in GitHub Desktop.
Save malkomalko/30a0d23188a7602a4aa5 to your computer and use it in GitHub Desktop.
Autocurry functions in javascript
function curry(fn) {
var numargs = fn.length;
return createRecurser([]);
function createRecurser(acc) {
return function () {
var args = Array.prototype.slice.call(arguments);
return recurse(acc, args);
};
}
function recurse(acc, args) {
var newacc = acc.concat(args);
if (newacc.length < numargs) {
return createRecurser(newacc);
} else {
return fn.apply(this, newacc);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment