https://kotlinlang.org logo
#feed
Title
e

elizarov

04/25/2020, 8:37 PM
Running into StackOverflowError but have a lot of heap space to keep the state of your computation? Kotlin Coroutines can help https://medium.com/@elizarov/deep-recursion-with-coroutines-7c53e15993e3
❤️ 5
🤯 1
👍 25
⏸️ 5
👌 1
c

Casey Brooks

04/26/2020, 1:10 AM
This is awesome! I accidentally discovered this property some time ago when I made an infinitely-recursive coroutine and was surprised that it never ran out of memory. Since then, I’ve been interested in seeing how this property could help in writing recursive parsers with understandable error stacktraces, but didn’t know how I’d go about implementing one. This article looks to be pretty much exactly the key I needed for it!
j

janvladimirmostert

04/26/2020, 6:12 AM
wouldn't
tailrec
have solved this depth problem as well or is this "problem" just to indicate how you would solve other deep recursive problems where tailrec is not an option?
e

elizarov

04/26/2020, 6:45 AM
The
depth
function is not tail-recursive and cannot be rewritten into a tail-recursive form unlike a similar function on lists, because depth makes two recursive calls. Even if one of them is transformed into a tail-call, then the other is still not a tail-call.
👍 3
j

janvladimirmostert

04/26/2020, 6:51 AM
ah, that makes sense
8 Views