🌐 REASONML PACT DIFFICULTY: Adept Behavioral

Chain of Responsibility in ReasonML

Pipelining optionals until a handler catches the spell.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Enchantment // Compulsion
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// REASON PATTERN //
let handleFire = (req) => req == "Fire" ? Some("Burned") : None;
let handleWater = (req) => req == "Water" ? Some("Doused") : None;

let rec processReq = (req, handlers) =>
  switch (handlers) {
  | [] => "Unhandled"
  | [h, ...rest] =>
    switch (h(req)) {
    | Some(res) => res
    | None => processReq(req, rest)
    }
  };

Using recursive lists of functions and option types, a request trickles through the chain until a valid transmutation occurs.

Runic Tags: #reason #chain-of-responsibility #behavioral