Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save futurist/eae3e0c3b96b80b1122cf310df61087d to your computer and use it in GitHub Desktop.
Save futurist/eae3e0c3b96b80b1122cf310df61087d to your computer and use it in GitHub Desktop.
ES6 Partial Application in 3 Lines

ES6 Partial Application in 3 Lines

const pApply = (fn, ...cache) => (...args) => {
  const all = cache.concat(args);
  return all.length >= fn.length ? fn(...all) : pApply(fn, ...all);
};

Example

Create a function like so:

const add = pApply((a, b, c) => a + b + c);

Use it as follows:

add(1);
// => function
add(1, 2);
// => function
add(1, 2, 3);
// => 6
add(1)(2)(3);
// => 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment