Skip to content

Instantly share code, notes, and snippets.

@kubarskii
Created May 13, 2021 22:05
Show Gist options
  • Save kubarskii/d4c448176919c97f18f23795e417cb67 to your computer and use it in GitHub Desktop.
Save kubarskii/d4c448176919c97f18f23795e417cb67 to your computer and use it in GitHub Desktop.
Trampoline example
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