Skip to content

Instantly share code, notes, and snippets.

@renaudtertrais
Last active September 1, 2017 09:43
Show Gist options
  • Save renaudtertrais/f335b154cfb202e1a50fa9be38a217b3 to your computer and use it in GitHub Desktop.
Save renaudtertrais/f335b154cfb202e1a50fa9be38a217b3 to your computer and use it in GitHub Desktop.
A simple ES6 curry function
const curry = (fn, ...args) => fn.length === 0
? fn
: fn.length > args.length
? (...args2) => curry(...[fn,...args,...args2])
: fn(...args);
// example
const add = curry((a,b,c,d) => a + b + c + d);
const add1 = add(1);
const add3 = add1(2);
add(1, 2, 3, 4); // 10
add(1, 2, 3, 4, 5); // 10
add(1)(2)(3)(4); // 10
add(1)(2, 3)(4); // 10
add(1, 2, 3)(4); // 10
add(1, 2)(3, 4); // 10
add1(2, 3, 4); // 10
add1(2, 3)(4); // 10
add1(2)(3, 4); // 10
add3(3, 4); // 10
add3(3)(4); // 10
@renaudtertrais
Copy link
Author

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