← Back to context

Comment by eru

21 hours ago

Yes, exactly.

> If all state functions have the same parameters, then an approach with functions per state that return the new state and new parameter values, which get called from a loop using a state value to function dictionary can be relatively clean. But if states need different parameters, yeah that gets complicated.

In a language like Rust, you can encode your state in an enum, and the different variants (= states) in a Rust enum can have different parameters.

Then your implementation basically becomes a loop around a match statement over that enum.

But all you've done here is manually implement a 'trampoline'. The trampoline is to a tail call what manually maintaining a (call) stack is for simulating non-tail calls. See https://en.wikipedia.org/wiki/Trampoline_(computing)#High-le...