From grimoire
Implements the Iterator design pattern to sequentially access collection elements without exposing underlying structure. Useful when decoupling traversal logic from data structures.
How this skill is triggered — by the user, by Claude, or both
Slash command
/grimoire:apply-iterator-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Provide a way to sequentially access elements of a collection without exposing its underlying structure.
Provide a way to sequentially access elements of a collection without exposing its underlying structure.
Adopted by: Python's iteration protocol (__iter__/__next__ — every for loop
uses it, making it the most-invoked pattern in the language), Java's Iterable/
Iterator (every Java collection implements it — for-each loops, streams, and
collectors all depend on it), C++ STL iterators (the foundation of the entire Standard
Template Library algorithm set), and Ruby's Enumerable module.
Impact: Python's iterator protocol is cited in the language reference as the reason
generators, comprehensions, and the for statement all share one protocol. The unifying
effect: any object implementing __iter__ works with for, zip, map, list(),
and every standard library function — without those functions knowing the collection's
type.
Why best: The alternative — index-based traversal — exposes the collection type
(list[i] doesn't work on a linked list or a file stream). Iterator abstracts over
any sequential structure, enabling algorithms that work on lists, trees, streams,
database cursors, and network responses uniformly.
Sources: Gamma et al. (1994) pp. 257–271; Python iterator protocol documentation;
Java Iterable specification
class NumberRange:
def __init__(self, start: int, end: int, step: int = 1):
self._start = start
self._end = end
self._step = step
def __iter__(self):
current = self._start
while current < self._end:
yield current
current += self._step
Using yield creates a generator iterator — the simplest correct implementation.
__next__ explicitlyclass NumberRangeIterator:
def __init__(self, start: int, end: int, step: int):
self._current = start
self._end = end
self._step = step
def __iter__(self):
return self
def __next__(self):
if self._current >= self._end:
raise StopIteration
value = self._current
self._current += self._step
return value
Use explicit __next__ when the iterator must be paused and resumed externally
(e.g., paginated API responses loaded one page at a time).
class BookShelf:
def __init__(self):
self._books: list[str] = []
def add(self, book: str):
self._books.append(book)
def __iter__(self):
return iter(self._books) # each call creates a fresh iterator
iter(self._books) creates a new list iterator each time, so two simultaneous
for loops over the same BookShelf don't interfere.
next(), zip(), enumerate()shelf = BookShelf()
shelf.add("Refactoring")
shelf.add("Clean Code")
for i, book in enumerate(shelf): # enumerate works — shelf is iterable
print(f"{i+1}. {book}")
paired = list(zip(shelf, shelf)) # multiple iterators — independent
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
for n in fibonacci(): # infinite iterator
if n > 100:
break
print(n)
collection[i] more than sequential traversal, a list interface is clearer.Modifying the collection during iteration. Deleting or inserting elements while iterating over them produces undefined behavior in most languages. Collect items to remove, then remove after the loop.
Making the collection and iterator the same object. If BookShelf.__iter__ returns self and BookShelf.__next__ manages position, two for loops share state and conflict. Return a new iterator object.
Forgetting StopIteration in __next__. An iterator that never raises StopIteration produces an infinite loop in a for loop. Always raise it when exhausted.
npx claudepluginhub jeffreytse/grimoire --plugin grimoireImplements the Iterator protocol and generators for traversing custom data structures with for...of. Useful for lazy sequences, data pipelines, and tree/graph iteration.
Generates Iterator pattern for PHP 8.4 collections: iterator interface, concrete iterator, iterable collection, value objects, and unit tests. Use for sequential traversal without exposing internals.
Adds new operations to stable object structures without modifying element classes, using the Visitor pattern. Useful for AST analysis, code quality tools, and compiler passes.