The Iterator
Traversing boundless dimensions sequentially
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Scrying
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// HAXE PATTERN //
class AstralPlane {
private var entities:Array<String>;
public function new() { entities = ["Spirit", "Wisp", "Phantom"]; }
// Haxe has built-in iterator support via `iterator()`
public function iterator():Iterator<String> {
return new PlaneIterator(this.entities);
}
}
class PlaneIterator {
private var collection:Array<String>;
private var index:Int = 0;
public function new(collection:Array<String>) {
this.collection = collection;
}
public function hasNext():Bool {
return index < collection.length;
}
public function next():String {
return collection[index++];
}
}
class Scryer {
public static function scan() {
var plane = new AstralPlane();
for (entity in plane) {
trace('Scried entity: $entity');
}
}
}
Sequential traversal of astral nodes requires a standardized interface. Haxe beautifully integrates the Iterator pattern directly into its core syntax. Implementing hasNext() and next() permits usage within standard for ... in loops, abstracting away the complex dimensional geometry.
Runic Tags:
#divination
#iterator
#haxe