Last active
February 5, 2018 06:23
-
-
Save HelderFigueiredo/651133f1bbe5e210a200aed088ef23f7 to your computer and use it in GitHub Desktop.
Currify a function.
This file contains hidden or 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
| function curry(...args) { | |
| let storedArgs = args.slice(1); | |
| const fn = args[0]; | |
| const fnArgsLen = fn.length; | |
| const curriedFn = (...newArgs) => { | |
| let currentArgs = [...storedArgs, ...newArgs]; | |
| if (currentArgs.length >= fnArgsLen) { | |
| return fn(...(currentArgs.slice(0, fnArgsLen))); | |
| } | |
| return curry(fn, ...currentArgs); | |
| }; | |
| return curriedFn; | |
| } | |
| module.exports = curry; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment