Created
July 5, 2021 18:14
-
-
Save rostegg/9c0f9e9c303519c40b156d3735e88437 to your computer and use it in GitHub Desktop.
Curry function with unknown length of params
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
/* | |
const toCurryFn = (...args) => {...} | |
const curried = curry(toCurryFn); | |
... | |
*/ | |
function curry(fn) { | |
const argumentsArray = []; | |
function subCurry(...args) { | |
argumentsArray.push(...args); | |
return subCurry; | |
} | |
subCurry[Symbol.toPrimitive] = () => { | |
const result = fn.apply(this, argumentsArray); | |
argumentsArray.length = 0; | |
return result; | |
}; | |
return subCurry; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment