Created
June 1, 2020 15:48
-
-
Save sunderls/a7b118b72d97f9d69f9d3e1a97da4fc5 to your computer and use it in GitHub Desktop.
curryify
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
function curry(fn) { | |
// fn(1,2,3) => fn(1)(2,3) => fn(1,2)(3) | |
// so it return a function which: | |
// 1. if enough param, just run it | |
// 2. if not enough param, return another function with param prefilled | |
return function curried(...args) { | |
if (args.length >= fn.length) { | |
return fn.apply(null, args) | |
} else { | |
return curried.bind(null, ...args) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment