Skip to content

Instantly share code, notes, and snippets.

@HichemBenChaaben
Last active August 2, 2018 21:58
Show Gist options
  • Save HichemBenChaaben/cb020a55a085fc1b47a49fc95838c6b9 to your computer and use it in GitHub Desktop.
Save HichemBenChaaben/cb020a55a085fc1b47a49fc95838c6b9 to your computer and use it in GitHub Desktop.
// the trampoline wrapper function
const trampoline = fn => (...args) => {
let result = fn(...args)
while (typeof result === 'function') {
result = result()
}
return result
}
const countDown = (n) => {
console.log(n);
// calling countdown with () => {}
return (n > 0) ? () => countDown(n - 1) : n;
}
const num = 99999;
// wrapping countDown with the trampoline
const withTrampoline = trampoline(countDown(num));
withTrampoline(num);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment