🧱 B PRECURSOR DIFFICULTY: Adept Behavioral

The Iterator

Traversing the untyped arrays of the old ones without exposing their layout.

✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Iterator
ARCANUM SCHOOL: Divination // Scrying
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// B PATTERN //
/* Iterator state: [collection_ptr, current_index, length] */

init_iterator(iter, collection, len) {
    iter[0] = collection;
    iter[1] = 0;
    iter[2] = len;
}

has_next(iter) {
    return iter[1] < iter[2];
}

get_next(iter) {
    auto col, idx, val;
    col = iter[0];
    idx = iter[1];
    val = col[idx];
    iter[1] = idx + 1;
    return val;
}

traverse_relics() {
    auto relics[3], iter[3];
    relics[0] = 'X'; relics[1] = 'Y'; relics[2] = 'Z';

    init_iterator(iter, relics, 3);
    while(has_next(iter)) {
        putchar(get_next(iter));
    }
}
Runic Tags: #b #design-pattern #precursor #bell-labs-magic #untyped