Last active
April 23, 2018 12:40
-
-
Save DayBr3ak/0e392a3757e84abcea09d2b36bae8e78 to your computer and use it in GitHub Desktop.
Js Es6 Auto Currying
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
/** cu - auto currying function | |
* | |
* @param {function} fn the function to autocurry | |
* @param {integer} argc the number of args to autocurry if reflection is not possible | |
*/ | |
function cu(fn, argc = null) { | |
const len = argc || fn.length; | |
const makeStack = (argsStack = []) => (...n) => { | |
const args = argsStack.concat(n); | |
if (args.length >= len) { | |
return fn(...args); | |
} | |
return makeStack(args); | |
}; | |
return makeStack() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment