Created
May 13, 2021 22:05
-
-
Save kubarskii/d4c448176919c97f18f23795e417cb67 to your computer and use it in GitHub Desktop.
Trampoline example
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 sum(n, s = 0) { | |
return (n) ? () => sum(n - 1, s + n) : s | |
} | |
function trampoline(fn) { | |
return function (...args) { | |
let res = fn.apply(null, args) | |
while (typeof res === 'function') { | |
res = res() | |
} | |
return res | |
} | |
} | |
const num = 1000000 | |
const s = trampoline(sum) | |
const a = s(num) | |
console.log(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment