Sinks & Terminals¶
Terminal methods drive execution on a lazy Stream[T]. Every terminal takes a context.Context (nil defaults to context.Background()) and blocks until completion, error, or cancellation, except All, which exposes iter.Seq2[T, error] directly.
See Alerts, FanOut & ParEach and Stream Reconciliation for complete programs combining multiple sinks and terminal aggregations.
Built-In Sinks (Sink[T])¶
Use built-in sink constructors with Into or FanOut.
ToSlice¶
ToSlice[T any](dst *[]T) Sink[T] appends received items into *dst. Passing a nil pointer returns ErrNilSink.
ToChan¶
ToChan[T any](ch chan<- T, opts ...ChanOption) Sink[T] forwards items onto ch while honoring ctx.Done(). Passing a nil channel returns ErrNilSink.
| Option | Effect |
|---|---|
CloseOnDone() |
Closes ch via defer when consumption completes or exits on error. |
ToFunc¶
ToFunc[T any](fn func(context.Context, T) error) Sink[T] invokes fn(ctx, v) per item. Returning ErrSkip is ignored cleanly; any other error aborts consumption. Passing nil returns ErrNilFunc.
err := batches.Into(ctx, automi.ToFunc(func(ctx context.Context, b []Record) error {
return store.InsertBatch(ctx, b)
}))
ToWriter¶
ToWriter[T ~[]byte | ~string](w io.Writer) Sink[T] writes string or []byte stream elements directly to w (using zero-copy io.WriteString for strings). Passing nil returns ErrNilWriter.
ToCSV¶
ToCSV(w io.Writer, opts ...CSVOption) Sink[[]string] writes []string rows as CSV to w and flushes on completion or error. Supports Comma(r rune) (defaults to ,). Passing nil returns ErrNilWriter.
Discard¶
Discard[T any]() Sink[T] consumes all stream elements and returns the first stream error, if any. Useful for pipelines driven solely for upstream side effects like Tap.
Terminal Methods on Stream[T]¶
Into¶
(s Stream[T]) Into(ctx context.Context, sink Sink[T]) error passes s's iterator to sink.Consume. Passing nil returns ErrNilSink.
Collect¶
(s Stream[T]) Collect(ctx context.Context) ([]T, error) materializes all items into a slice and stops on the first error.
items, err := automi.FromSlice([]int{1, 2, 3}).
Map(func(n int) int { return n * 2 }).
Collect(ctx) // [2, 4, 6]
Each and ParEach¶
(s Stream[T]) Each(ctx context.Context, fn func(context.Context, T) error) errorrunsfnsequentially per item (ignoringErrSkip).(s Stream[T]) ParEach(ctx context.Context, workers int, fn func(context.Context, T) error) errorrunsfnacrossworkersconcurrent goroutines (or inline whenworkers == 1). See Bounded Concurrency.
Reduce¶
(s Stream[T]) Reduce[U any](ctx context.Context, init U, fn func(U, T) U) (U, error) folds all items into a single accumulated value starting from init. Passing nil returns ErrNilFunc.
totalCents, err := automi.FromSlice(invoices).
FlatMap(func(inv Invoice) iter.Seq[int] { return slices.Values(inv.Lines) }).
Reduce(ctx, 0, func(acc, cents int) int { return acc + cents })
GroupBy¶
(s Stream[T]) GroupBy[K comparable](ctx context.Context, keyFn func(T) K) (map[K][]T, error) partitions items into a map keyed by keyFn(v). Passing nil returns ErrNilFunc.
All¶
(s Stream[T]) All(ctx context.Context) iter.Seq2[T, error] returns the underlying Go iterator for native for range loops. Always check err inside the loop; breaking out early triggers defer cancellation and cleanup across all upstream concurrent stages.