Last active
August 2, 2018 21:58
-
-
Save HichemBenChaaben/cb020a55a085fc1b47a49fc95838c6b9 to your computer and use it in GitHub Desktop.
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
// 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