Skip to content

Instantly share code, notes, and snippets.

@ezsi
Last active August 29, 2015 14:06
Show Gist options
  • Save ezsi/1709e12939ca3fd80c49 to your computer and use it in GitHub Desktop.
Save ezsi/1709e12939ca3fd80c49 to your computer and use it in GitHub Desktop.
JavaScript: currying function
// Currying: f(f1,...,fn) -> f(f1,...,fk)(fk+1,...,fn)
// Usage: f.curry(f1,...,fk) -> g(fk+1,...fn) == f(f1,...,fn)
Function.prototype.curry = function() { //f(f1,...,fk)
if( arguments.length == 0 )
return this;
var args = arguments;
var fn = this;
return function() { // g(fk+1,...,fn)
var prefix = Array.prototype.slice.call(args);
var suffix = Array.prototype.slice.call(arguments);
fn.apply(this, prefix.concat(suffix));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment