Documentation
¶
Overview ¶
Package stream contains domain-neutral, typed lazy stream operators.
Index ¶
- func Ticks(ctx context.Context, hz int, maxCatchup time.Duration) iter.Seq2[time.Duration, error]
- type StreamOp
- func Batch[T any](size int) StreamOp[T, []T]
- func BatchByTime[T any](ctx context.Context, size int, timeout time.Duration) StreamOp[T, []T]
- func Collect[T any](maxItems int) StreamOp[T, []T]
- func Debounce[T any](ctx context.Context, interval time.Duration) StreamOp[T, T]
- func Distinct[T comparable]() StreamOp[T, T]
- func DistinctBy[T any, K comparable](keyFn func(T) K) StreamOp[T, T]
- func Filter[T any](pred func(T) bool) StreamOp[T, T]
- func FlatMap[In, Out any](fn func(In) iter.Seq2[Out, error]) StreamOp[In, Out]
- func Map[In, Out any](fn func(In) Out) StreamOp[In, Out]
- func MapE[In, Out any](fn func(In) (Out, error)) StreamOp[In, Out]
- func Reduce[T, Acc any](initial Acc, fn func(Acc, T) (Acc, error)) StreamOp[T, Acc]
- func Take[T any](n int) StreamOp[T, T]
- func Throttle[T any](interval time.Duration) StreamOp[T, T]
- func Window[T any](size, step int) StreamOp[T, []T]
- func WithContext[T any](ctx context.Context) StreamOp[T, T]
- func WorkerPool[In, Out any](ctx context.Context, concurrency int, bufferCapacity int, ...) StreamOp[In, Out]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ticks ¶ added in v0.30.0
Ticks yields a fixed-timestep duration (e.g., 16.6ms for 60Hz). It decouples physical wall-clock time from logical engine ticks.
Lifecycle: the underlying time.Ticker is stopped when the consumer breaks out of the range loop OR when ctx is canceled. Callers MUST ensure the context is canceled or the range loop terminates to avoid leaking the ticker.
Types ¶
type StreamOp ¶
StreamOp transforms a lazy stream without erasing its item types.
func Batch ¶
Batch groups successful items into bounded batches. A final partial batch is emitted when the upstream ends normally.
func BatchByTime ¶ added in v0.23.0
BatchByTime groups items up to a maximum size or until the timeout interval elapses.
func Collect ¶
Collect materializes a bounded stream into one item. A non-positive limit is rejected at iteration time so the operator remains lazy.
func Debounce ¶ added in v0.23.0
Debounce drops items followed by another item within the interval.
The operator is a feeder goroutine plus a timing loop:
- the feeder reads the upstream iterator and pushes items onto a channel, closing the channel when the iterator is exhausted or the context is canceled;
- the loop restarts a timer on every received item and yields the item only when the timer fires without a newer item arriving.
Splitting the two makes each side's branch count legible: the feeder has one select and one error check, the loop has one select and the pending/timer pair. Neither alone exceeds the complexity budget.
func Distinct ¶ added in v0.31.0
func Distinct[T comparable]() StreamOp[T, T]
Distinct yields the first occurrence of each element. Memory grows with cardinality; bound the input upstream.
func DistinctBy ¶ added in v0.31.0
func DistinctBy[T any, K comparable](keyFn func(T) K) StreamOp[T, T]
DistinctBy yields the first element per key. keyFn must not be nil. Memory grows with key cardinality; bound the input upstream.
func FlatMap ¶
FlatMap expands each successful item sequentially. The inner stream must honor the downstream yield result; no goroutine is created per item.
func Throttle ¶ added in v0.23.0
Throttle drops items that arrive faster than the specified interval. Zero allocations.
func WithContext ¶
WithContext stops an operation when ctx is canceled before or during iteration. It preserves upstream errors and downstream early stop.
func WorkerPool ¶ added in v0.27.0
func WorkerPool[In, Out any]( ctx context.Context, concurrency int, bufferCapacity int, worker func(context.Context, In) (Out, error), ) StreamOp[In, Out]
WorkerPool processes upstream items concurrently using a pool of workers. The worker callback runs for each item, up to `concurrency` in parallel. Results are yielded as they complete — not in input order — so callers that need ordering must sort downstream.
Termination:
- Upstream ends → feeder closes workChan → workers drain it and exit → resultChan closes after the WaitGroup finishes.
- Downstream stops consuming (yield returns false) → cancel pool → workers and feeder observe cancellation and return.
- ctx is canceled → same as above.
Panic isolation: a panic inside worker or inside the upstream iterator is recovered and delivered as an error on the result stream. The pool keeps running other workers; a panicking item produces one error and the pool continues with the next item.
The function is safe to call with nil worker (yields a single error) and with concurrency < 1 (clamped to 1). bufferCapacity < 1 defaults to 2×concurrency.