Closed masaeedu closed 5 years ago
In general, there are two ways to deal with this – one is to do a monadic fold with a trampoline (this is a bit tricky, and often requires using hyloM
rather than just cataM
). The other approach is described in the Clowns/Jokers paper, and I haven’t seen it implemented yet.
I think I understand how to do this now. Thanks!
@masaeedu this might help https://medium.com/@safareli/stack-safe-function-composition-85d61feee37e
This is probably going to be a little off topic, apologies in advance. I'm trying to write recursion schemes in JS, but I'm having issues with stack usage. For example:
The code above works fine, but if I scale the list up to 100000 elements, I'm going to end up setting up a giant 10,000 frame deep call and blow the stack. I was wondering if this sort of problem is unavoidable (at least with catamorphisms), and if not, how this library solves it (hopefully in a way I can imitate in JS :sweat_smile:).
I can think of a way to write a list-specific
iterativeCata
that reifies the stack in an array, and does things iteratively, but that will be list-specific and won't work for e.g.TreeF
.