Created
January 30, 2017 20:31
-
-
Save unscriptable/5d051a79e32ffdd9be3f9d6558c734df to your computer and use it in GitHub Desktop.
Function to partially apply function args even if the function might be "manually curried".
This file contains 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
export const papply = | |
(f, ...x) => { | |
const arity = f.length | |
const args = x.length | |
// shortcut no-ops for perf | |
if (args === arity) return f(...x) | |
if (args === 0) return f | |
if (args < arity) { | |
return (...y) => papply(f, ...x.concat(y)) | |
} | |
else { | |
const g = f(...x.slice(0, arity)) | |
return papply(g, ...x.slice(arity)) | |
} | |
} | |
/* | |
> papply((a, b) => (c) => a + b + c, 1, 2, 3) | |
6 | |
> papply((a, b) => (c) => a + b + c, 1, 2)(3) | |
6 | |
> papply((a, b) => (c) => a + b + c, 1)(2, 3) | |
6 | |
> papply((a, b) => (c) => a + b + c, 1)(2)(3) | |
6 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment