4 ms·
When I need something lightweight I usually implement state machines using generators/coroutines when the language provides them, as they compose well and I fin
by chombier 3y ago
When I need something lightweight I usually implement state machines using generators/coroutines when the language provides them, as they compose well and I find it easier to follow to control flow anyways.
- burntcaramel 3y agoI’m curious how you use and compose generator functions? I created a JavaScript state machine library using them to define each state. https://github.com/JavaScriptRegenerated/yieldmachine https://github.com/JavaScriptRegenerated/yieldmachine I imagine your approach must be different?
- chombier 3y agoTo compose generators I use `yield*` (or `yield from` in python) https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Operators/yield\ https://developer.mozilla.org/fr/docs/Web/JavaScript/Referen...* (note that last star is actually part of the link) The main drawback with this approach is that it is easy to blow the stack, but it is not too hard to implement trampolining on top of it (by yielding the continuation to a wrapper generator) To use the generator I send its inputs using `.next()` and obtain current state information, as shown on your library webpage.