Implement a State Machine in Larzscript
Declare states and transitions, fire events, and let the machine reject anything that isn't a legal move - a document workflow (draft -> review -> published) in a few lines.
fsm.transition() registers legal (event, from, to) moves, fsm.fire() applies one, and fsm.can() lets you check before you try - the machine itself is the source of truth for what's allowed, not scattered if-statements.
larzscript pkg install fsm
Code
import "fsm" as fsm
let m = fsm.new("draft")
fsm.transition(m, "submit", "draft", "review")
fsm.transition(m, "approve", "review", "published")
fsm.fire(m, "submit")
print(m["state"])
fsm.fire(m, "approve")
print(m["state"])
print(fsm.can(m, "submit"))Output
review published false
Related
larzfsmA declarative finite state machine - register transitions, fire events, read back history→Retry with Exponential Backoff in LarzscriptRetry a flaky call a few times, waiting longer between each attempt, instead of failing on the first hiccup or hammering a struggling service.→Memoize a Function in LarzscriptCache a function's results so calling it twice with the same input only does the real work once.→Paginate a List in LarzscriptSlice a list into pages with the has_next/has_prev/1-based-index bookkeeping every list-showing page needs, without hand-rolling the off-by-one math again.→