Created
October 28, 2019 15:18
-
-
Save leolanese/34f672c6f288efd948226e2f0ba21785 to your computer and use it in GitHub Desktop.
fp-varadocCurry
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
/** | |
* | |
* @param varadocCurry | |
* | |
* @usage | |
* const toSum = varadocCurry((x, y) => x + y, 0); | |
* const toMul = varadocCurry((x, y) => x * y, 1); | |
* | |
* toSum(1)(1, 2)(3, 4)(5, 6, 7)(); // 29 | |
* toMul(1)(1, 2)(3, 4); // 24 | |
* | |
*/ | |
export const varadocCurry = (fn, seed) => { | |
const value = (args, seedValue) => args.reduce((acc, a) => fn.call(fn, acc, a), seedValue); | |
const next = (...args) => (...x) => { | |
if (!x.length) { | |
return value(args, seed); | |
} | |
return next(...args, value(x, seed)); | |
}; | |
return next(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment