Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Forked from JamieMason/es6-partial-application.md
Created December 18, 2017 12:41
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 ThomasBurleson/1eddf90e63fb25f64610cfa55b062d63 to your computer and use it in GitHub Desktop.
Save ThomasBurleson/1eddf90e63fb25f64610cfa55b062d63 to your computer and use it in GitHub Desktop.
ES6 Curry Function

ES6 Curry Function

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

Example

Create a function like so:

const add = curry((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