✨ GLEAM ALCHEMY DIFFICULTY: Adept Behavioral

The State

Transitioning actor behavior via state machines.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Phase Shifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// GLEAM PATTERN //
import gleam/otp/actor

pub type State {
  Solid
  Liquid
  Gas
}

pub type Msg {
  Heat
  Cool
}

fn handle(msg: Msg, state: State) -> actor.Next(Msg, State) {
  case state, msg {
    Solid, Heat -> actor.continue(Liquid)
    Liquid, Heat -> actor.continue(Gas)
    Gas, Cool -> actor.continue(Liquid)
    Liquid, Cool -> actor.continue(Solid)
    _, _ -> actor.continue(state)
  }
}

The State

An OTP actor implicitly implements the State pattern. Its loop naturally transitions through various states based on incoming messages.

Runic Tags: #transmutation #state #gleam #otp