The Iterator (Racket)
Traversing a collection of magical artifacts sequentially without exposing its underlying representation.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Seers
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// RACKET PATTERN //
#lang racket
(define (make-grimoire-iterator spells)
(let ([current spells])
(λ ()
(if (null? current)
'end-of-grimoire
(let ([next-spell (car current)])
(set! current (cdr current))
next-spell)))))
(define iter (make-grimoire-iterator '("Fireball" "Frostbolt" "Arcane Missiles")))
(displayln (iter))
(displayln (iter))
(displayln (iter))
(displayln (iter))
The Iterator
The Iterator acts as a Divining tool, moving sequentially through a Grimoire without revealing whether the pages are bound as lists, arrays, or mystical trees. A simple closure closing over the current state allows a Mage to pull spells one by one until the tome is empty.
Runic Tags:
#racket
#behavioral
#iterator
#stateful-magic