Skip to content

Instantly share code, notes, and snippets.

@thearunkumar
Created February 26, 2023 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thearunkumar/7dbfdafaa1bdeffdde6cd60d3e57ccba to your computer and use it in GitHub Desktop.
Save thearunkumar/7dbfdafaa1bdeffdde6cd60d3e57ccba to your computer and use it in GitHub Desktop.
simple curry in javascript
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