Skip to content

Instantly share code, notes, and snippets.

@leolanese
Created October 28, 2019 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leolanese/34f672c6f288efd948226e2f0ba21785 to your computer and use it in GitHub Desktop.
Save leolanese/34f672c6f288efd948226e2f0ba21785 to your computer and use it in GitHub Desktop.
fp-varadocCurry
/**
*
* @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