Skip to content

Instantly share code, notes, and snippets.

@d4rkr00t
Created August 12, 2014 17:00
Show Gist options
  • Save d4rkr00t/5736b7fea7218133bc04 to your computer and use it in GitHub Desktop.
Save d4rkr00t/5736b7fea7218133bc04 to your computer and use it in GitHub Desktop.
Curry function with any numbers og arguments
var curry = function(fn) {
var _subCurry = function(fn) {
var args = [].slice.call(arguments, 1);
return function () {
return fn.apply(this, args.concat([].slice.call(arguments)));
};
};
var _curry = function(fn, length) {
return function () {
if (length - arguments.length > 0) {
return _curry(
_subCurry.apply(this, [fn].concat([].slice.call(arguments))),
length - arguments.length
);
}
return fn.apply(this, arguments);
}
};
return _curry(fn, fn.length)();
};
var func = curry(function (a, b, c, d) {
return a * b + c * d;
});
console.log(func(10)(2)(5)(3));
console.log(func(10)(2)(5, 3));
console.log(func(10)(2, 5)(3));
console.log(func(10)(2, 5, 3));
console.log(func(10, 2)(5, 3));
console.log(func(10, 2, 5)(3));
console.log(func(10, 2, 5, 3));
// TODO: holes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment