🐪 OCAML METAMAGIC DIFFICULTY: Adept Behavioral

The Chain of Responsibility

Passing magical anomalies down a lineage of wardens until resolved.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Abjuration // Anomaly Routing
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// OCAML PATTERN //
type anomaly = Minor | Major | WorldEnding

let handle_minor = function
  | Minor -> Some "Dealt with by Apprentice"
  | _ -> None

let handle_major = function
  | Major -> Some "Contained by Adept"
  | _ -> None

let rec route_anomaly handlers anomaly =
  match handlers with
  | [] -> failwith "Unstoppable anomaly!"
  | h :: t -> 
      match h anomaly with
      | Some resolution -> resolution
      | None -> route_anomaly t anomaly

let chain = [handle_minor; handle_major]

The Chain of Responsibility uses a list of handler functions, cascading the magical anomaly through pattern matching until a capable warden is found.

Runic Tags: #Caml Metamagic #Pattern Matching #OCaml