Recursion is awesome, but has the downside of growing the stack, which can limit its usefulness. Some languages like Scheme, however, have Tail-call optimization, which lets programmers write Tail-recursive functions that don't grow the call stack. Python does not have Tail-call optimization (TCO), but with asyncio, we can have something like Tail-call optimization. Basically, this method uses the asyncio event loop like a trampoline function.
Example:
A few things to note:
- This will NOT grow the call stack
- This is about 2x slower than a loop-based iterative approach (at least for this factorial algorithm)
- Notice the use of
asyncio.async()
- Notice that we are using a done callback - this is similar to Continuation-passing style.