Created
February 26, 2023 01:31
-
-
Save thearunkumar/7dbfdafaa1bdeffdde6cd60d3e57ccba to your computer and use it in GitHub Desktop.
simple curry in javascript
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, d, e) { | |
console.log(a, b, c, d, e); | |
return a + b + c + d + e; | |
} | |
function curry(fn) { | |
let total = fn.length; | |
function inner(a) { | |
--total; | |
if (total === 0) { | |
return fn.call(null, a); | |
} | |
fn = fn.bind(null, a); | |
return inner; | |
} | |
return inner; | |
} | |
const sumOfAll = curry(sum); | |
console.log(sumOfAll(1)(2)(3)(4)(5)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment