Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created January 30, 2017 20:31
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 unscriptable/5d051a79e32ffdd9be3f9d6558c734df to your computer and use it in GitHub Desktop.
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".
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