A Functional State Machine for Islands
Many language ecosystems provide ready-made state machine packages that we can customize to fit our own applications. Elixir is no exception to this in that OTP has a fine state machine called :gen_statem built in.
We could use :gen_statem to implement our state machine, but we’re going to build our own from scratch instead. We’ll be able to do it in much fewer lines of code, and we won’t need to spin up a new process for each state machine the way we would if we used :gen_statem.
Instead, we’ll define a new module, multiple clauses of a single function, and a data structure to represent the state. Each time we invoke the function, we’ll pass in the state as well as an event. The function will decide whether ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access