Last active
November 23, 2020 10:36
-
-
Save rajatjain-21/f074b635b6a4edb37f95c672f9405843 to your computer and use it in GitHub Desktop.
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(func, length = func.length) { | |
// passing a second paramenter as the number of arguments | |
// passed to the function | |
return function (...args) { | |
if (args.length >= length) { | |
// if the function call contains arguments which are | |
// greater than or equal to the max, just the return the func | |
return func(...args); | |
} else { | |
// otherwise call the curry again, binding the already | |
// appeared arguments and this time, decreasing the length of agruments by | |
// number of arguments passed earlier | |
return curry(func.bind(this, ...args), length - args.length); | |
} | |
}; | |
} | |
function add(a, b, c) { | |
return a + b + c | |
} | |
const curryAdd = curry(add); | |
console.log(curryAdd(3,4,5)) // 12 | |
console.log(curryAdd(4,5)(4)) // 13 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment