⚙️ PURESCRIPT RUNE DIFFICULTY: Adept Behavioral

The Chain of Responsibility of Web Runes

Pass arcane requests along a chain of magical wards until one handles it.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Abjuration // Spell Deflection
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// PURESCRIPT PATTERN //
module Arcane.ChainOfResponsibility where
import Prelude
import Data.Maybe (Maybe(..))
import Data.List (List(..))

type Request = { spellType :: String, payload :: String }
type Handler = Request -> Maybe String

handleLowLevel :: Handler
handleLowLevel req =
  if req.spellType == "Cantrip"
  then Just $ "Apprentice handled: " <> req.payload
  else Nothing

handleMidLevel :: Handler
handleMidLevel req =
  if req.spellType == "Hex"
  then Just $ "Warlock handled: " <> req.payload
  else Nothing

-- Compose the chain
chain :: List Handler -> Handler
chain Nil _ = Nothing
chain (Cons h rest) req = case h req of
  Just res -> Just res
  Nothing -> chain rest req
Runic Tags: #behavioral #chain-of-responsibility #purescript #runes