From harness-claude
Implements custom iterables with Symbol.iterator and generators for lazy, composable sequences. Covers tree traversal, paginated APIs, and infinite sequences.
How this skill is triggered — by the user, by Claude, or both
Slash command
/harness-claude:gof-iterator-patternThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
> Traverse collections with Symbol.iterator and generators for lazy, composable sequences.
Traverse collections with Symbol.iterator and generators for lazy, composable sequences.
for...of loop support on a custom classCustom iterable class:
class LinkedList<T> {
private head: { value: T; next: { value: T; next: unknown } | null } | null = null;
prepend(value: T): this {
this.head = { value, next: this.head };
return this;
}
[Symbol.iterator](): Iterator<T> {
let current = this.head;
return {
next(): IteratorResult<T> {
if (current === null) return { done: true, value: undefined as T };
const value = current.value;
current = current.next as typeof current;
return { done: false, value };
},
};
}
}
const list = new LinkedList<number>().prepend(3).prepend(2).prepend(1);
for (const value of list) {
console.log(value); // 1, 2, 3
}
console.log([...list]); // [1, 2, 3]
Generator functions for lazy sequences:
// Infinite sequence — safe because it's lazy
function* naturals(start = 1): Generator<number> {
let n = start;
while (true) yield n++;
}
// Take first N values without materializing the infinite sequence
function* take<T>(iterable: Iterable<T>, n: number): Generator<T> {
let count = 0;
for (const value of iterable) {
if (count >= n) break;
yield value;
count++;
}
}
function* map<T, U>(iterable: Iterable<T>, fn: (value: T) => U): Generator<U> {
for (const value of iterable) yield fn(value);
}
function* filter<T>(iterable: Iterable<T>, pred: (value: T) => boolean): Generator<T> {
for (const value of iterable) if (pred(value)) yield value;
}
// Compose lazily — no intermediate arrays
const first10Evens = [
...take(
filter(naturals(), (n) => n % 2 === 0),
10
),
];
console.log(first10Evens); // [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Async iterator for paginated APIs:
async function* paginatedUsers(pageSize = 100): AsyncGenerator<User> {
let page = 0;
let hasMore = true;
while (hasMore) {
const { users, total } = await fetchUsers({ page, pageSize });
for (const user of users) yield user;
page++;
hasMore = page * pageSize < total;
}
}
// Process all users without loading everything into memory
for await (const user of paginatedUsers(50)) {
await processUser(user);
}
Tree traversal iterator:
interface TreeNode<T> {
value: T;
children: TreeNode<T>[];
}
function* depthFirst<T>(node: TreeNode<T>): Generator<T> {
yield node.value;
for (const child of node.children) {
yield* depthFirst(child); // delegate to recursive generator
}
}
function* breadthFirst<T>(root: TreeNode<T>): Generator<T> {
const queue: TreeNode<T>[] = [root];
while (queue.length > 0) {
const node = queue.shift()!;
yield node.value;
queue.push(...node.children);
}
}
Generator return types in TypeScript:
Generator<Yield, Return, Next> — synchronous generatorAsyncGenerator<Yield, Return, Next> — async generator (use async function* and for await)Iterable<T> — any object with [Symbol.iterator]()AsyncIterable<T> — any object with [Symbol.asyncIterator]()Performance: Generators are lazy — they compute values only when requested. This is critical for large datasets. Compare:
// Eager — allocates entire array in memory
const users = await db.findAll(); // 1M rows
const emails = users.map((u) => u.email);
// Lazy — streams one at a time
for await (const user of db.stream()) {
// cursor-based
await sendEmail(user.email);
}
Anti-patterns:
return() and throw() iterator protocol methods — important for cleanup in async iteratorsasync function* are hard to traceBuilt-in iterables: Arrays, Maps, Sets, Strings, and arguments all implement the iterator protocol. Use this as a baseline — your custom collections should too.
refactoring.guru/design-patterns/iterator
npx claudepluginhub intense-visions/harness-engineering --plugin harness-claudeImplements the Iterator protocol and generators for traversing custom data structures with for...of. Useful for lazy sequences, data pipelines, and tree/graph iteration.
Implements the Iterator design pattern to sequentially access collection elements without exposing underlying structure. Useful when decoupling traversal logic from data structures.
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.