Prototype in Elm
Cloning and mutating immutable records in Elm using the Prototype pattern.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Prototype
ARCANUM SCHOOL: Illusion // Cloning
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
module Prototype exposing (SpellRecord, baseSpell, fireBolt, iceLance)
type alias SpellRecord =
{ element : String
, damage : Int
, manaCost : Int
, isAoE : Bool
}
baseSpell : SpellRecord
baseSpell =
{ element = "Arcane"
, damage = 10
, manaCost = 5
, isAoE = False
}
fireBolt : SpellRecord
fireBolt =
{ baseSpell | element = "Fire", damage = 25, manaCost = 15 }
iceLance : SpellRecord
iceLance =
{ baseSpell | element = "Ice", damage = 15, isAoE = True }
The Prototype: Reflections in the Immutable Mirror
True cloning is a fundamental law of the Elm universe. Because all data is immutable, the Prototype pattern is not an arcane ritual, but the very nature of existence. Using the record update syntax, an alchemist can spawn a flawless replica of a base construct, altering only the specific resonant frequencies needed for the new spell, leaving the original matrix untouched and pure.
Runic Tags:
#elm
#creational
#prototype
#record-updates
#immutability