Skip to content

Instantly share code, notes, and snippets.

@Lavrend
Created May 16, 2019 15:03
Show Gist options
  • Save Lavrend/bd3fcd454c6e02b5ec810010585d17cf to your computer and use it in GitHub Desktop.
Save Lavrend/bd3fcd454c6e02b5ec810010585d17cf to your computer and use it in GitHub Desktop.
infinite function call: Sum(a)(b,c)...(x,y,z) => a + (b + c) + ... + (x + y + z)
function sum(...args) {
let total = 0;
const reduceSum = (arr = [], start = 0) => {
return arr.reduce((memo, val) => memo + val, start);
};
const fn = (...args) => {
const newSum = reduceSum(args, total);
total = newSum;
return fn;
};
fn.valueOf = () => total;
return fn(...args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment