The Iterator Hex
Traversing chaotic collections of magical artifacts safely.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Seeking
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// KOTLIN PATTERN //
class Grimoire(private val spells: List<String>) : Iterable<String> {
override fun iterator(): Iterator<String> = GrimoireIterator(spells)
}
class GrimoireIterator(private val spells: List<String>) : Iterator<String> {
private var index = 0
override fun hasNext() = index < spells.size
override fun next(): String {
if (!hasNext()) throw NoSuchElementException("No more spells")
return spells[index++]
}
}
The Iterator Hex
To scan a collection of volatile artifacts without triggering a cataclysm requires precision. The Iterator Hex separates the traversal mechanism from the data structure itself. In modern Kotlin, this pattern is deeply integrated into the language via Iterable and Sequence, hiding the complexity behind elegant for loops.
Runic Tags:
#kotlin
#behavioral
#iterator