Created
August 23, 2019 01:56
-
-
Save ThomRoman/d3ac89e7b729094fdf60d0b76387c4c4 to your computer and use it in GitHub Desktop.
avoid stack overflow
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
const trampoline = fn => (...args)=>{ | |
let result = fn(...args) | |
while (typeof result === 'function') | |
result = result() | |
return result | |
} | |
const sum = (number,step=0)=>( | |
!number?step:()=>sum(number-1,step+number) | |
) | |
// const tsum = trampoline(sum)(500000) // currying | |
const tsum = trampoline(sum) | |
const result = tsum(50000) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment