Created
March 5, 2016 18:48
-
-
Save lyoshenka/f7a2778a0cfc9da5a49b to your computer and use it in GitHub Desktop.
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 autoCurry = (function () { | |
var toArray = function toArray(arr, from) { | |
return Array.prototype.slice.call(arr, from || 0); | |
}, | |
curry = function curry(fn /* variadic number of args */) { | |
var args = toArray(arguments, 1); | |
return function curried() { | |
return fn.apply(this, args.concat(toArray(arguments))); | |
}; | |
}; | |
return function autoCurry(fn, numArgs) { | |
numArgs = numArgs || fn.length; | |
return function autoCurried() { | |
if (arguments.length < numArgs) { | |
return numArgs - arguments.length > 0 ? | |
autoCurry(curry.apply(this, [fn].concat(toArray(arguments))), numArgs - arguments.length) : | |
curry.apply(this, [fn].concat(toArray(arguments))); | |
} | |
else { | |
return fn.apply(this, arguments); | |
} | |
}; | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment