Skip to content

Instantly share code, notes, and snippets.

@leolanese
Created October 28, 2019 15:17
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/e7e50139f8cb45d1dd1695153ac6ee0d to your computer and use it in GitHub Desktop.
Save leolanese/e7e50139f8cb45d1dd1695153ac6ee0d to your computer and use it in GitHub Desktop.
fp-curry
/**
* @param curry
*
* @usage
* const _sum3 = (x, y, z) => x + y + z;
* const _sum4 = (p, q, r, s) => p + q + r + s;
*
* const sum3 = curry(_sum3);
* sum3(1)(3)(2); // 6
* const sum4 = curry(_sum4);
* sum4(1)(3)(2)(4); // 10
*
*/
export const curry = fn => {
if (fn.length === 0) {
return fn;
}
const innerFn = (N, args) => (...x) => {
if (N <= x.length) {
return fn(...args, ...x);
}
return innerFn(N - x.length, [...args, ...x]);
};
return innerFn(fn.length, []);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment