Created
May 16, 2019 15:03
-
-
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)
This file contains hidden or 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(...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