Last active
September 5, 2019 23:28
-
-
Save JenniferFuBook/b4e0d9a4bf2c2ce2da930208525e0cca to your computer and use it in GitHub Desktop.
Currying is about decomposing a function taking multiple arguments into numerous functions with single arguments. It is named after Haskell 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 === 0) { | |
return fn; | |
} | |
const _curry = (...args) => { | |
if (fn.length <= args.length) { | |
return fn.apply(null, args); | |
} | |
return _curry.bind(null, ...args); | |
} | |
return _curry; | |
} | |
const add = (a,b,c,d) => a + b + c + d; | |
curry(add)(1,2)(3)(4); // 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment