Generators

Generator functions use fn* and yield to produce values lazily on demand.

Summary

A generator function is declared with fn*. Each yield pauses execution and emits a value; the function resumes when the next value is requested. Generators integrate directly with for..in loops, so they work anywhere an iterable is expected.

Canonical

Generator functions use fn* and yield to produce values lazily.

fn* count_up(n) {
    var i = 0
    while i < n {
        yield i
        i = i + 1
    }
}

for x in count_up(5) {
    print("{x} ")                -- 0 1 2 3 4
}

Generators pause at each yield and resume when the next value is requested. They work with for..in loops.