Skip to content

Instantly share code, notes, and snippets.

@koozdra
Created October 10, 2015 16:30
Show Gist options
  • Save koozdra/4f1edda2009293a85cf2 to your computer and use it in GitHub Desktop.
Save koozdra/4f1edda2009293a85cf2 to your computer and use it in GitHub Desktop.
Javascript Curry
// curry function and accepts lists of parameters
function curry(fn) {
// begin with an empty array of collected arguments
var partialArgs = [];
var f = function() {
// append all passed in arguments to the arguments collection
Array.prototype.push.apply(partialArgs, arguments);
// if we have matched or exceeded the argument length of the containing function
// call the function with the required length (extra parameters are ignored)
if (partialArgs.length >= fn.length){
// TODO: is 'this' appropriate here?
fn.apply(this, Array.prototype.slice.call(partialArgs, 0, fn.length));
}
// return the function to collect more arguments
return f;
};
// call and return the accumulator function
return f();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment