Execution Model¶
Automi separates pipeline construction from pipeline execution. Building a Stream[T] allocates only closures; goroutines and channels start only when a terminal consumes an asynchronous stage.

Pull-Based Lazy Blueprints¶
Every Stream[T] wraps an iterator factory func(context.Context) iter.Seq2[T, error]. Calling source constructors or transform methods returns a new immutable Stream[T] without reading data or starting background work.
A zero-value Stream[T]{} is valid and yields zero items with no error.
Execution begins when a terminal (Into, Collect, Each, ParEach, Reduce, GroupBy, All, or FanOut) passes a context.Context to pull elements upstream.
flowchart LR
Src["Source / From*"] --> Sync["Fused Sync Stages<br/>(Filter, Map, TryMap, FlatMap)"]
Sync --> Async["Async Boundary<br/>(ParMap / Batch / Buffer)"]
Async --> Term["Terminal<br/>(Into / Collect / ParEach / All)"]
Synchronous Iterator Fusion¶
Sequential stages run inline inside the consumer's for v, err := range seq loop with zero extra goroutines and zero per-item channel hops:
- Sources:
FromSlice,FromSeq,FromSeq2,FromFunc,FromCSV,FromLines,FromReader, andFrom - Transforms:
Filter,Take,Drop,Tap,Map,TryMap,FlatMap,Through - Size-only batching:
Batch(s, MaxSize(n))with noMaxWait - Serial fast-paths:
ParMapconfigured withWorkers(1)and noBufferSize, andParEachwithworkers == 1
When a downstream stage stops (for example via Take(5) or break in a for range loop over All(ctx)), yield returns false and the pull unwinds upstream immediately.
Structured Concurrency at Async Boundaries¶
Stages that require concurrent execution, buffering, or clocks launch bounded goroutines scoped to a child context derived from the terminal's ctx:
ParMap(Workers > 1orBufferSize > 0) andParEach(workers > 1)BatchwithMaxWait > 0BufferandBufferDropDebounceMerge,Zip, andFanOut
Every asynchronous stage enforces three invariants:
- Bounded channels: Internal queues are capped by worker count, window size, or explicit buffer capacity so slow consumers apply backpressure unless
DropOldest()orDropNewest()is chosen. - Sibling cancellation: The first non-
ErrSkiperror or recovered worker panic cancels the internal child context immediately. - Complete teardown before return: Each stage waits for its producer and worker goroutines (
sync.WaitGroup.Wait()) before returning fromInto/ParEachor exiting itsiter.Seq2function—even if a caller exits afor range s.All(ctx)loop early.
Why Batch, Zip, Concat, Merge, and FanOut Are Functions¶
Go 1.27 supports generic methods on concrete types (func (s Stream[T]) Map[U any](...) Stream[U]), which covers transforms like Map, TryMap, ParMap, FlatMap, Through, Reduce, and GroupBy, plus Step.Then.
However, Go's type checker forbids methods on a generic type Stream[T] whose signature instantiates Stream with a type parameter that wraps T (such as Stream[[]T] or Stream[Pair[T, U]]), because recursive instantiation checks reject type growth on methods. For that reason:
Batch[T](Stream[T] -> Stream[[]T]) andZip[A, B](Stream[Pair[A, B]]) are package-level functions.Concat[T],Merge[T], andFanOut[T]are package-level variadic combinators.
Because Batch, Zip, Concat, and Merge still return Stream[...], you continue chaining methods directly on their result: