Documentation
¶
Overview ¶
Package seq provides lazy iterator operations on iter.Seq[T] with method chaining.
Seq[T] wraps iter.Seq[T] to enable fluent pipelines. Unlike stream.Stream (memoized), Seq pipelines re-evaluate on each Collect or range. Use .Iter() to unwrap back to iter.Seq[T] for interop with stdlib and other libraries.
Range works directly — no .Iter() needed for for-range loops.
Index ¶
- func Contains[T comparable](s Seq[T], target T) bool
- func Fold[T, R any](s Seq[T], initial R, fn func(R, T) R) R
- type Seq
- func Chunk[T any](s Seq[T], size int) Seq[[]T]
- func Concat[T any](a, b Seq[T]) Seq[T]
- func Empty[T any]() Seq[T]
- func Enumerate[T any](s Seq[T]) Seq[pair.Pair[int, T]]
- func FilterMap[T, R any](s Seq[T], fn func(T) (R, bool)) Seq[R]
- func FlatMap[T, R any](s Seq[T], fn func(T) Seq[R]) Seq[R]
- func From[T any](ts []T) Seq[T]
- func FromChannel[T any](ctx context.Context, ch <-chan T) Seq[T]
- func FromIter[T any](s iter.Seq[T]) Seq[T]
- func FromNext[T any](next func() (T, bool)) Seq[T]
- func Generate[T any](seed T, fn func(T) T) Seq[T]
- func Map[T, R any](s Seq[T], fn func(T) R) Seq[R]
- func Of[T any](vs ...T) Seq[T]
- func Repeat[T any](v T) Seq[T]
- func Scan[T, R any](s Seq[T], initial R, fn func(R, T) R) Seq[R]
- func Unfold[T, S any](seed S, fn func(S) (T, S, bool)) Seq[T]
- func Unique[T comparable](s Seq[T]) Seq[T]
- func UniqueBy[T any, K comparable](s Seq[T], fn func(T) K) Seq[T]
- func Zip[A, B any](a Seq[A], b Seq[B]) Seq[pair.Pair[A, B]]
- func (s Seq[T]) Any(fn func(T) bool) bool
- func (s Seq[T]) Collect() []T
- func (s Seq[T]) Convert(fn func(T) T) Seq[T]
- func (s Seq[T]) Drop(n int) Seq[T]
- func (s Seq[T]) DropWhile(fn func(T) bool) Seq[T]
- func (s Seq[T]) Each(fn func(T))
- func (s Seq[T]) Every(fn func(T) bool) bool
- func (s Seq[T]) Find(fn func(T) bool) option.Option[T]
- func (s Seq[T]) Intersperse(sep T) Seq[T]
- func (s Seq[T]) Iter() iter.Seq[T]
- func (s Seq[T]) KeepIf(fn func(T) bool) Seq[T]
- func (s Seq[T]) None(fn func(T) bool) bool
- func (s Seq[T]) Reduce(fn func(T, T) T) option.Option[T]
- func (s Seq[T]) RemoveIf(fn func(T) bool) Seq[T]
- func (s Seq[T]) Take(n int) Seq[T]
- func (s Seq[T]) TakeWhile(fn func(T) bool) Seq[T]
- func (s Seq[T]) ToChannel(ctx context.Context, buf int) <-chan T
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](s Seq[T], target T) bool
Contains returns true if target is in the sequence. Short-circuits on first match. On infinite sequences, terminates only if a match is found. Note: for float types, NaN != NaN, so Contains(s, NaN) is always false. Standalone because the comparable constraint cannot be expressed on the Seq[T any] receiver.
Types ¶
type Seq ¶
Seq is a lazy iterator over iter.Seq[T] with method chaining. Range works directly:
for v := range seq.From(data).KeepIf(pred) { ... }
Unlike stream.Stream (memoized), Seq pipelines re-evaluate on each Collect or range — standard iter.Seq behavior.
The zero value (nil) is NOT safe for direct range — it will panic. Use Empty, From, or other constructors. All constructors and Seq-returning operations return non-nil Seqs safe for range.
Use .Iter() when a function expects iter.Seq[T].
func Chunk ¶
Chunk groups elements into slices of at most size elements. The last chunk may have fewer than size elements. Each emitted slice is a stable snapshot with independent backing storage. Buffers up to size elements. Works with infinite sequences. Panics if size <= 0.
func Enumerate ¶
Enumerate pairs each element with its zero-based index, lazily. The index resets on each iteration. Safe for repeated use.
func FilterMap ¶
FilterMap applies fn to each element and keeps only the results where fn returns true. Combines filtering and type-changing transformation in a single lazy pass. Fully streaming with O(1) state. Works with infinite sequences. Panics if fn is nil.
func FlatMap ¶
FlatMap applies fn to each element of s and concatenates the resulting Seqs. Standalone because Go methods cannot introduce additional type parameters. Panics if fn is nil.
func FromChannel ¶
FromChannel creates a Seq that yields values received from ch. Iteration blocks on each receive. The sequence ends when ch is closed.
Cancellation is best-effort: if cancellation races with ready channel receives, cancellation does not necessarily win immediately. The sequence may yield additional values until the ctx.Done() case is selected by Go's pseudo-random select.
The provided ctx is captured in the returned Seq for its lifetime. Unlike most Go APIs, cancellation scope is fixed at construction time, not iteration time.
Like FromNext, the returned Seq is stateful — re-iteration continues from whatever channel state exists, not from the beginning.
Panics if ctx or ch is nil.
func FromIter ¶
FromIter wraps an existing iter.Seq[T] as a Seq for method chaining. If s is nil, returns an empty Seq safe for range.
func FromNext ¶
FromNext creates a Seq from a pull-style iterator function. next is called repeatedly; each call returns (value, ok). When ok is false the sequence ends. next is not called again after returning false.
Because next is typically stateful (a cursor), iterating the returned Seq a second time will see whatever state next is in — usually exhausted. Use .Collect() to materialize if you need multiple passes. Panics if next is nil.
func Generate ¶
Generate creates an infinite Seq: seed, fn(seed), fn(fn(seed)), ... Panics if fn is nil.
func Map ¶
Map applies fn to each element, returning a Seq of a different type. Standalone because Go methods cannot introduce additional type parameters. Panics if fn is nil.
func Scan ¶
Scan reduces a Seq like Fold, but yields all intermediate accumulator values. It includes the initial value as the first element (scanl semantics), so the result has len(s)+1 elements for a finite Seq. Standalone because Go methods cannot introduce additional type parameters. Panics if fn is nil.
func Unfold ¶
Unfold creates a Seq by repeatedly applying fn to a seed state. fn returns (element, nextState, continue). When continue is false, the sequence ends without emitting element. Fully lazy — fn is not called until iteration begins (unlike stream.Unfold, which eagerly evaluates the first element at construction time). Panics if fn is nil.
func Unique ¶
func Unique[T comparable](s Seq[T]) Seq[T]
Unique removes duplicate elements lazily, preserving first occurrence. Memory grows with the number of distinct elements seen — on infinite or high-cardinality streams, memory may grow without bound. On infinite repeating streams, Unique stalls once all distinct values have been emitted — requesting more elements than distinct values exist will never terminate. Note: for float types, NaN != NaN, so NaN values are never deduplicated. Standalone because the comparable constraint cannot be expressed on the Seq[T any] receiver.
func UniqueBy ¶
func UniqueBy[T any, K comparable](s Seq[T], fn func(T) K) Seq[T]
UniqueBy removes duplicate elements lazily by extracted key, preserving first occurrence. Memory grows with the number of distinct keys seen — on infinite or high-cardinality streams, memory may grow without bound. Note: for float key types, NaN != NaN, so NaN keys are never deduplicated. Standalone because the comparable constraint on K cannot be expressed on the Seq[T any] receiver. Panics if fn is nil.
func Zip ¶
Zip returns a Seq of pairs from corresponding elements of a and b. Truncates to the shorter sequence. Note: a is the driving side — if b is shorter, one extra element of a is consumed before truncation is detected. For side-effectful or single-use sources, be aware of this left-consumption bias.
func (Seq[T]) Any ¶
Any returns true if fn returns true for at least one element. Short-circuits on first match. Panics if fn is nil.
func (Seq[T]) Collect ¶
func (s Seq[T]) Collect() []T
Collect materializes the Seq into a slice. Requires a finite sequence.
func (Seq[T]) Convert ¶
Convert applies fn to each element, returning a Seq of results. Same-type transform — use standalone Map for cross-type mapping. Panics if fn is nil.
func (Seq[T]) Drop ¶
Drop returns a Seq that skips the first n elements. If n <= 0, yields all elements.
func (Seq[T]) DropWhile ¶
DropWhile returns a Seq that skips elements while fn returns true, then yields the rest. Panics if fn is nil.
func (Seq[T]) Each ¶
func (s Seq[T]) Each(fn func(T))
Each applies fn to every element for side effects. Requires a finite sequence. Panics if fn is nil.
func (Seq[T]) Every ¶
Every returns true if fn returns true for all elements. Returns true for an empty Seq (vacuous truth). Short-circuits on first mismatch. Panics if fn is nil.
func (Seq[T]) Find ¶
Find returns the first element where fn returns true. Short-circuits on first match. Panics if fn is nil.
func (Seq[T]) Intersperse ¶
Intersperse inserts sep between every adjacent pair of elements. Empty and single-element sequences pass through unchanged. Fully streaming with O(1) state. Works with infinite sequences.
func (Seq[T]) Iter ¶
Iter returns the underlying iter.Seq[T] for interop with stdlib and other libraries. Returns a no-op iterator if s is nil (zero value).
func (Seq[T]) KeepIf ¶
KeepIf returns a Seq containing only elements where fn returns true. Panics if fn is nil.
func (Seq[T]) None ¶
None returns true if fn returns false for all elements. Returns true for an empty Seq (vacuous truth). Panics if fn is nil.
func (Seq[T]) Reduce ¶
Reduce combines elements left-to-right using the first element as the initial value. Returns not-ok if the sequence is empty. For a single-element sequence, returns that element without calling fn. Requires a finite sequence. Panics if fn is nil (even for empty or single-element sequences). Note: this differs from slice.Reduce, which tolerates nil fn when len <= 1.
func (Seq[T]) RemoveIf ¶
RemoveIf returns a Seq containing only elements where fn returns false. It is the complement of KeepIf. Panics if fn is nil.
func (Seq[T]) TakeWhile ¶
TakeWhile returns a Seq yielding elements while fn returns true. Stops at the first element where fn returns false. Panics if fn is nil.
func (Seq[T]) ToChannel ¶
ToChannel sends values from s into a new channel, returning it. A goroutine is spawned to drive iteration; it closes the returned channel when iteration ends.
Cancellation is cooperative: the goroutine checks ctx at each yield/send boundary. If s blocks internally before yielding the next value (e.g., a nested FromChannel on a slow source), ctx cancellation cannot interrupt it — the goroutine remains blocked until s yields or terminates. The caller must drain or cancel to avoid goroutine leaks. If both ctx.Done() and the channel send are selectable, sends may continue until the cancellation branch is selected. Buffered channels or an actively receiving consumer can therefore observe additional post-cancel values.
If ctx is already canceled, no goroutine is spawned and a closed empty channel is returned.
If s is nil (zero value), returns a closed empty channel. buf sets the channel buffer size (0 for unbuffered). Panics if ctx is nil or buf < 0.
ToChannel assumes s follows the iter.Seq protocol: no concurrent yield calls, no retaining yield after return.