πŸ”΄ SCALA ALCHEMY DIFFICULTY: Adept Behavioral

The Chain of Responsibility

Pass an unshaped arcane query through a hierarchy of wards until one can intercept and process it.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Chain of Responsibility
ARCANUM SCHOOL: Abjuration // Warding
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// SCALA PATTERN //
type Handler = PartialFunction[String, String]

val fireWard: Handler = {
  case "fireball" => "FireWard dissipates the fireball."
}

val iceWard: Handler = {
  case "frostbite" => "IceWard shatters the frostbite."
}

val generalWard: Handler = {
  case _ => "GeneralWard absorbs the unknown spell."
}

// Composing the chain using Scala's PartialFunction `orElse`
val mysticShield: Handler = fireWard orElse iceWard orElse generalWard

// Usage:
// println(mysticShield("frostbite")) 

Scala’s PartialFunction provides an incredibly idiomatic and functionally pure way to implement the Chain of Responsibility. Wards are chained together seamlessly.

Runic Tags: #scala #behavioral #warding #partial-functions