Last active
November 18, 2017 21:55
-
-
Save GianlucaGuarini/e4acc089587576f8d6205e8057b29d09 to your computer and use it in GitHub Desktop.
Function to curry any javascript method
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
/** | |
* Function to curry any javascript method | |
* @param {Function} fn - the target function we want to curry | |
* @param {...[args]} acc - initial arguments | |
* @returns {Function|*} it will return a function until the target function | |
* will receive all its arguments | |
*/ | |
function curry(fn, ...acc) { | |
return (...args) => { | |
args = [...acc, ...args] | |
return args.length < fn.length ? | |
curry(fn, ...args) : | |
fn(...args) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment