iterator

Traverse elements of a collection without exposing its internal representation.

SKILL.md

GitHub

Iterator

Provides a uniform way to sequentially access elements of a collection regardless of how the collection is structured (array, tree, graph, etc.).

When to Use

  • You want to hide the internal structure of a collection from consumers
  • Multiple traversal algorithms must be supported for the same collection
  • A unified interface is needed across different collection types
  • Lazy / infinite sequences (generate elements on demand)

How to Apply

  1. Define an Iterator interface with hasNext() and next() methods
  2. Define an Iterable interface with createIterator() returning an Iterator
  3. Implement concrete iterator classes (one per traversal strategy or collection type)
  4. The collection implements Iterable and returns the appropriate iterator
  5. In modern languages, implement the language's native iterable protocol (Symbol.iterator, __iter__)

Example

class Range {
  constructor(private start: number, private end: number) {}

  [Symbol.iterator](): Iterator<number> {
    let current = this.start;
    const end = this.end;
    return {
      next(): IteratorResult<number> {
        return current <= end
          ? { value: current++, done: false }
          : { value: undefined as any, done: true };
      },
    };
  }
}

for (const n of new Range(1, 5)) {
  console.log(n); // 1 2 3 4 5
}

References