Last active
September 1, 2017 09:43
-
-
Save renaudtertrais/f335b154cfb202e1a50fa9be38a217b3 to your computer and use it in GitHub Desktop.
A simple ES6 curry function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sandbox : http://jsbin.com/letohe/edit?js,console