Visitor in Elm
Extending functionality over a Custom Type via exhaustive pattern matching.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Visitor
ARCANUM SCHOOL: Divination // Structure Traversal
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
module Visitor exposing (ASTNode(..), countNodes, extractText)
type ASTNode
= TextNode String
| ElementNode String (List ASTNode)
-- Visitor 1: Counting the nodes
countNodes : ASTNode -> Int
countNodes node =
case node of
TextNode _ ->
1
ElementNode _ children ->
1 + List.sum (List.map countNodes children)
-- Visitor 2: Extracting all text
extractText : ASTNode -> String
extractText node =
case node of
TextNode text ->
text
ElementNode _ children ->
String.join " " (List.map extractText children)
The Visitor: The Astral Projection
The object-oriented Visitor pattern separates algorithms from the object structures they operate on via double-dispatch. In Elm, this is completely natively handled by Custom Types and exhaustive pattern matching. A function (the “Visitor”) simply takes the root type (ASTNode) and pattern matches across all variants. If a new Visitor is needed—say, to extract text rather than count nodes—a new pure function is written without modifying the underlying data structures at all.
Runic Tags:
#elm
#behavioral
#visitor
#pattern-matching
#recursion
#ast