Last active
May 12, 2018 17:38
-
-
Save RatoX/c2a13dec72caf391129a01475966b895 to your computer and use it in GitHub Desktop.
Infinite sum until the result function was called with Javascript
This file contains 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 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