Created
October 28, 2019 15:17
-
-
Save leolanese/e7e50139f8cb45d1dd1695153ac6ee0d to your computer and use it in GitHub Desktop.
fp-curry
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 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