M4 · Trees and RecursionSession 8-9BehavioralKotlin replaces itOutline

Intent. Walk the elements of a collection without exposing how it is stored.

In Kotlin. Iterable and Sequence ship with the standard library; sequence { yield(x) } builds a lazy one.

Walking the structure without exposing it. Kotlin ships this as Iterable and Sequence, so the point of studying it is the contract – who owns the cursor, what happens when the collection changes mid-walk – not the implementation.

Structure

classDiagram
    class Aggregate {
        <<interface>>
        +iterator() Iterator
    }
    class ConcreteAggregate {
        +iterator() Iterator
    }
    class Iterator {
        <<interface>>
        +hasNext() Boolean
        +next() T
    }
    class ConcreteIterator {
        -cursor
    }
    Aggregate <|.. ConcreteAggregate
    Iterator <|.. ConcreteIterator
    ConcreteAggregate ..> ConcreteIterator : creates