Created
August 12, 2014 17:00
-
-
Save d4rkr00t/5736b7fea7218133bc04 to your computer and use it in GitHub Desktop.
Curry function with any numbers og arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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