The Iterator Hex
Traversing a forbidden collection of cursed artifacts without exposing its underlying structure.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Actor Model Hexes
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// MOTOKO PATTERN //
module Iterator {
public type Iterator<T> = {
hasNext : () -> Bool;
next : () -> ?T;
};
public class RelicVault(relics : [Text]) {
public func createIterator() : Iterator<Text> {
var currentIndex : Nat = 0;
object {
public func hasNext() : Bool {
currentIndex < relics.size();
};
public func next() : ?Text {
if (currentIndex < relics.size()) {
let item = relics[currentIndex];
currentIndex += 1;
?item;
} else {
null;
}
};
}
};
};
public actor Archaeologist {
let vault = RelicVault(["Cursed Amulet", "Bone Dagger", "Void Chalice"]);
public func inspectVault() : async [Text] {
let iter = vault.createIterator();
var found : [Text] = [];
while (iter.hasNext()) {
switch (iter.next()) {
case (?relic) {
found := Array.append(found, [relic]);
};
case (null) {};
};
};
found;
};
};
}
The Iterator Hex allows a mage to sequence through the elements of a RelicVault without knowing whether it is a flat array, a linked grimoire, or a dimensional pocket. The iteration logic is cleanly decoupled from the collection itself.
Runic Tags:
#motoko
#behavioral
#iterator
#actor-model