Skip to content

Instantly share code, notes, and snippets.

@RatoX
Last active May 12, 2018 17:38
Show Gist options
  • Save RatoX/c2a13dec72caf391129a01475966b895 to your computer and use it in GitHub Desktop.
Save RatoX/c2a13dec72caf391129a01475966b895 to your computer and use it in GitHub Desktop.
Infinite sum until the result function was called with Javascript
function infinite(fn, ...args) {
function partial(...args2) {
return infinite(fn, ...args.concat(args2));
}
partial.result = () => {
return fn(...args);
};
return partial;
}
function sum(...args) {
return args.reduce(((a, b) => a + b), 0);
}
console.log('Infinite SUM')
sumInfinite = infinite(sum)
console.log('---------')
console.log(sumInfinite) // Function
console.log(sumInfinite.result()) // 0
console.log(sumInfinite(1, 2)) // Function
console.log(sumInfinite(1, 2)(3, 50).result()) // 56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment