Created
June 10, 2019 07:39
-
-
Save mindevolution/b9b2c970341095ebd1d890130f763a1a to your computer and use it in GitHub Desktop.
js curry
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 => { | |
if (fn.length <= 1) return fn; | |
const generator = (...args) => { | |
if (fn.length === args.length) { | |
return fn(...args); | |
} else { | |
return (...args2) => generator(...args, ...args2); | |
} | |
}; | |
return generator; | |
}; | |
const add = (a, b, c, d) => a + b + c + d; | |
const curriedAdd = curry(add); | |
const added = curriedAdd(5)(6)(7)(8); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment