Created
January 9, 2021 09:09
-
-
Save sematgt/b3b70a00216277d95f4933de1988727b to your computer and use it in GitHub Desktop.
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
function sum(a, b, c) { | |
return a + b + c; | |
} | |
function curry(func) { | |
return function curried(...args) { | |
if (args.length >= func.length) { | |
return func.apply(this, args); | |
} else { | |
return function pass(...args2) { | |
return curried.apply(this, args.concat(args2)) | |
} | |
} | |
} | |
} | |
let curriedSum = curry(sum); | |
console.log(curriedSum(1)(2)(3)) | |
console.log(curriedSum(1, 2)(3)) | |
console.log(curriedSum(1)(2, 3)) | |
console.log(curriedSum(1, 2, 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment