Synchronous Transforms¶
Synchronous transform methods execute inline inside the consumer's iter.Seq2 loop with zero extra goroutines and zero per-item channel allocations. See the runnable Stream Reconciliation Example for FlatMap and TryMap in an end-to-end pipeline.
Same-Type Stages¶
Filter¶
(s Stream[T]) Filter(pred func(T) bool) Stream[T] emits only items for which pred(v) returns true. Passing nil yields ErrNilFunc.
billable := automi.FromSlice(orders).
Filter(func(o Order) bool { return !o.Canceled && o.TotalCents > 0 })
Take¶
(s Stream[T]) Take(n int) Stream[T] emits at most the first n items and stops pulling upstream as soon as n items are delivered. If n <= 0, upstream is never iterated.
Drop¶
(s Stream[T]) Drop(n int) Stream[T] skips the first n items before emitting remaining elements. If n <= 0, Drop returns s unchanged.
Tap¶
(s Stream[T]) Tap(fn func(T)) Stream[T] runs fn(v) as an inline side effect and forwards the unmodified value downstream. Use Tap for metrics, counters, or structured debug logs without breaking method chaining. Passing nil yields ErrNilFunc.
Generic Type-Changing Methods¶
Go 1.27 generic methods on Stream[T] allow transformations to change element types cleanly within a single method chain.
Map¶
(s Stream[T]) Map[U any](fn func(T) U) Stream[U] transforms each element with an infallible pure function. Passing nil yields ErrNilFunc.
csvRows := automi.FromSlice(orders).
Map(func(o Order) []string {
return []string{o.ID, o.Region, strconv.Itoa(o.TotalCents)}
})
TryMap¶
(s Stream[T]) TryMap[U any](fn func(context.Context, T) (U, error)) Stream[U] executes a context-aware fallible transform per item:
- Returning
nilemits the transformed value. - Returning
ErrSkipdrops the item and continues streaming. - Returning any other error stops iteration and surfaces that error to the terminal.
Any Step[T, U] matches fn's signature and passes directly to TryMap. Passing nil yields ErrNilFunc.
parsed := automi.FromLines(rawMetrics).
TryMap(func(_ context.Context, line string) (Metric, error) {
m, err := parseMetricLine(line)
if errors.Is(err, errMalformed) {
return Metric{}, automi.ErrSkip
}
return m, err
})
FlatMap¶
(s Stream[T]) FlatMap[U any](fn func(T) iter.Seq[U]) Stream[U] expands each input item into zero, one, or many downstream items returned as a standard iter.Seq[U]. Returning nil from fn skips emitting items for that input.
FlatMap checks ctx.Err() on every yielded inner item and stops immediately if ctx cancels or a downstream stage (such as Take) exits early. Passing nil yields ErrNilFunc.
lineItems := automi.FromSlice(invoices).
FlatMap(func(inv Invoice) iter.Seq[LineItem] {
return slices.Values(inv.Items)
})
Through¶
(s Stream[T]) Through[U any](stage func(Stream[T]) Stream[U]) Stream[U] applies a reusable sub-pipeline function inside a fluent chain. It also integrates package-level functions like Batch inline. Passing nil yields ErrNilFunc.