The Iterator of Mystic Collections
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation in Move.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Chronomancy
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
module arcane::iterator {
use std::vector;
struct Iterator<T> has drop {
collection: vector<T>,
index: u64,
}
public fun new<T>(collection: vector<T>): Iterator<T> {
Iterator { collection, index: 0 }
}
public fun has_next<T>(iter: &Iterator<T>): bool {
iter.index < vector::length(&iter.collection)
}
public fun next<T: copy>(iter: &mut Iterator<T>): T {
let item = *vector::borrow(&iter.collection, iter.index);
iter.index = iter.index + 1;
item
}
}
Runic Tags:
#behavioral
#iterator
#move
#vectors
#collections