Skip to content

Instantly share code, notes, and snippets.

@nathggns
Created June 7, 2015 02:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nathggns/fb97cd9018454bfa87c1 to your computer and use it in GitHub Desktop.
Save nathggns/fb97cd9018454bfa87c1 to your computer and use it in GitHub Desktop.
Recursive curry functions in ES6
function curry(fn, ...args) {
if (args.length === fn.length) {
return fn(...args);
}
return curry.bind(this, fn, ...args);
}
function add(a, b) {
return a + b;
}
curry(add, 1, 2);
// 3
curry(add)(1)(2);
// 3
curry(add)(1, 2);
// 3
curry(add, 1)(2);
// 3
@micabe
Copy link

micabe commented Apr 7, 2016

one liner :
const curry = (fn, ...args) => args.length === fn.length ? fn(...args) : curry.bind(this, fn, ...args)

Cheers !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment