Traffic Light State Machine

State transitions with match() guards

Why it matters

  • State effect tracks a traffic light without mutating shared data.
  • `match()` validates each transition before the state handler commits it.
Statematch()seq()Console Console.capture()State.with()Exception.tryCatch() Traffic Light state
Program
const program = () =>
  seq()
    .let(() => State.get<Light>()) // ctx.v1
    .tap((current) => Console.op.log(`Current: ${lightLabel(current)}`))
    .then((current) => nextLight(current))
    .tap((next) => State.put(next))
    .tap((next) => Console.op.log(`Next: ${lightLabel(next)}`))
    .return((next, ctx) => ({ previous: ctx!["v1"] as Light, current: next }));

Awaiting execution

Console output, timeline events and traffic light state snapshots will render here after you run the program.

Handlers: Console.capture() · State.with() · Exception.tryCatch()