App patterns · real, tested code

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