Skip to content

Instantly share code, notes, and snippets.

@smonn
Last active October 20, 2015 15:53
Show Gist options
  • Save smonn/19ba11b53a08ecc45d25 to your computer and use it in GitHub Desktop.
Save smonn/19ba11b53a08ecc45d25 to your computer and use it in GitHub Desktop.
curry method for javascript with support for context
// curry(fn[, context[, arity[, params...]]])
var curry = function (fn, context, arity) {
var args = Array.prototype.slice.call(arguments, 0);
if (!context) {
context = context || undefined;
args[1] = context;
}
if (!arity) {
arity = arity || fn.length;
args[2] = arity;
}
if (arity === (args.length - 3)) {
return fn.apply(context, args.slice(3));
}
return function () {
var args2 = Array.prototype.slice.call(arguments, 0);
return curry.apply(context, args.concat(args2));
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment