Skip to content

Instantly share code, notes, and snippets.

@ABHISHEK-KEDAR-21
Created March 16, 2023 10:46
Show Gist options
  • Save ABHISHEK-KEDAR-21/52b40a3f918c3044befe88a7dd2e5897 to your computer and use it in GitHub Desktop.
Save ABHISHEK-KEDAR-21/52b40a3f918c3044befe88a7dd2e5897 to your computer and use it in GitHub Desktop.
cuurrying in JS
function sum(...args) {
let total = 0;
let adjust = -1
if (args.length && args[0] === 0) {
adjust = 0
}
function inner(...innerArgs) {
if (innerArgs.length === 0) {
return total
}
args = [...args, ...innerArgs]
total = args.reduce((acc, i) => acc + parseInt(i), adjust)
return inner
}
return inner(args)
}
console.log(sum(0,0,0)()) // 0
console.log(sum(0,1)(1)()) // 2
console.log(sum(0,1)(1)(2,3)(2)()) // 9
console.log(sum(0,0)(1)(2,3)(0)()) // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment