🐪 OCAML METAMAGIC DIFFICULTY: Adept Behavioral

The State

Morphing the behavior of a magical automaton based on its internal elemental attunement.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Transmutation // Automaton Core
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// OCAML PATTERN //
type state = Idle | Aggressive | Defensive

let react state event =
  match state, event with
  | Idle, "attacked" -> Aggressive
  | Aggressive, "calmed" -> Defensive
  | _, _ -> state

let current_core = ref Idle
let process_event ev =
  current_core := react !current_core ev

The State pattern is naturally expressed as a Finite State Machine using variant types and pattern matching, elegantly dictating the automaton’s reactions.

Runic Tags: #Caml Metamagic #State Machines #OCaml