✨ ARCANUM RUNE DIFFICULTY: Archmage Behavioral

The Visitor of Labyrinths

Decoupling algorithms from the magical structures they operate on via double dispatch.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Abjuration // Geometry
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
interface LabyrinthVisitor
    exposes [Node, Visitor, accept]
    imports []

Node : [
    Trap U64,
    Treasure Str
]

Visitor a : {
    visitTrap : U64 -> a,
    visitTreasure : Str -> a
}

accept : Node, Visitor a -> a
accept = \node, visitor ->
    when node is
        Trap power -> visitor.visitTrap power
        Treasure item -> visitor.visitTreasure item

# Example Visitor
descriptionVisitor : Visitor Str
descriptionVisitor = {
    visitTrap: \power -> "A deadly trap of power ${Num.toStr power}",
    visitTreasure: \item -> "A wondrous treasure: ${item}"
}
Runic Tags: #fast-functional-wards #roc #visitor #traversal