Documentation
¶
Overview ¶
Package iter provides lazy, composable sequences over Go 1.27 iterators.
Values flow through a pipeline of generic methods, so lookup cost is paid only on the final terminal method:
iter.Of(1, 2, 3, 4).Filter(isEven).Map(square).Collect()
Range-over-func makes consumers the driver: breaking out of a `range` on a returned sequence stops upstream work immediately.
Fallible operations (MapErr, FlatMapErr) run every element and pair the outcome as a Result[U], a sequence of (result, error): (result, nil) on success, (zero, err) on failure. The error travels as data, so it can be logged with Tap, dropped with IgnoreErr, collected with Errors, or surfaced with CollectErr.
Index ¶
- func Contains[T comparable](s Seq[T], v T) bool
- func Max[T cmp.Ordered](s Seq[T]) (T, bool)
- func Min[T cmp.Ordered](s Seq[T]) (T, bool)
- func Sum[T Number](s Seq[T]) T
- func ToMap[K comparable, V any](s Seq2[K, V]) map[K]V
- type KeyValue
- type Number
- type Result
- func (s Result[V]) CollectErr() ([]V, error)
- func (s Result[V]) Count() int
- func (s Result[V]) Errors() Seq[error]
- func (s Result[V]) Filter(f func(V, error) bool) Result[V]
- func (s Result[V]) IgnoreErr() Seq[V]
- func (s Result[V]) Seq() stditer.Seq2[V, error]
- func (s Result[V]) Take(n int) Result[V]
- func (s Result[V]) Tap(f func(V, error)) Result[V]
- type Seq
- func Chunk[T any](s Seq[T], n int) Seq[[]T]
- func Concat[T any](seqs ...Seq[T]) Seq[T]
- func From[T any](seq stditer.Seq[T]) Seq[T]
- func FromKeys[K comparable, V any](m map[K]V) Seq[K]
- func FromValues[K comparable, V any](m map[K]V) Seq[V]
- func Of[T any](vals ...T) Seq[T]
- func Range(from, to, step int) Seq[int]
- func Repeat[T any](n int, v T) Seq[T]
- func RepeatBy[T any](n int, f func(int) T) Seq[T]
- func Uniq[T comparable](s Seq[T]) Seq[T]
- func (s Seq[T]) Collect() []T
- func (s Seq[T]) Count() int
- func (s Seq[T]) CountBy(pred func(T) bool) int
- func (s Seq[T]) Drop(n int) Seq[T]
- func (s Seq[T]) DropWhile(pred func(T) bool) Seq[T]
- func (s Seq[T]) Enumerate() Seq2[int, T]
- func (s Seq[T]) Every(pred func(T) bool) bool
- func (s Seq[T]) Filter(f func(T) bool) Seq[T]
- func (s Seq[T]) Find(pred func(T) bool) (T, bool)
- func (s Seq[T]) First() (T, bool)
- func (s Seq[T]) FlatMap[U any](f func(T) Seq[U]) Seq[U]
- func (s Seq[T]) FlatMapErr[U any](f func(T) (Seq[U], error)) Result[U]
- func (s Seq[T]) ForEach(f func(T))
- func (s Seq[T]) GroupBy[K comparable](f func(T) K) map[K][]T
- func (s Seq[T]) KeyBy[K comparable](key func(T) K) map[K]T
- func (s Seq[T]) Last() (T, bool)
- func (s Seq[T]) Map[U any](f func(T) U) Seq[U]
- func (s Seq[T]) MapErr[U any](f func(T) (U, error)) Result[U]
- func (s Seq[T]) MaxBy[K cmp.Ordered](key func(T) K) (T, bool)
- func (s Seq[T]) MinBy[K cmp.Ordered](key func(T) K) (T, bool)
- func (s Seq[T]) None(pred func(T) bool) bool
- func (s Seq[T]) Reduce[U any](init U, f func(U, T) U) U
- func (s Seq[T]) ReduceErr[U any](init U, f func(U, T) (U, error)) (U, error)
- func (s Seq[T]) Seq() stditer.Seq[T]
- func (s Seq[T]) SkipErr(check func(T) error) Seq[T]
- func (s Seq[T]) Some(pred func(T) bool) bool
- func (s Seq[T]) SumBy[K Number](key func(T) K) K
- func (s Seq[T]) Take(n int) Seq[T]
- func (s Seq[T]) TakeWhile(pred func(T) bool) Seq[T]
- func (s Seq[T]) Tap(f func(T)) Seq[T]
- func (s Seq[T]) UniqBy[K comparable](key func(T) K) Seq[T]
- func (s Seq[T]) Zip[U any](o Seq[U]) Seq2[T, U]
- type Seq2
- func (s Seq2[K, V]) Collect() []KeyValue[K, V]
- func (s Seq2[K, V]) Count() int
- func (s Seq2[K, V]) Drop(n int) Seq2[K, V]
- func (s Seq2[K, V]) DropWhile(pred func(K, V) bool) Seq2[K, V]
- func (s Seq2[K, V]) Filter(f func(K, V) bool) Seq2[K, V]
- func (s Seq2[K, V]) Keys() Seq[K]
- func (s Seq2[K, V]) Seq() stditer.Seq2[K, V]
- func (s Seq2[K, V]) Take(n int) Seq2[K, V]
- func (s Seq2[K, V]) TakeWhile(pred func(K, V) bool) Seq2[K, V]
- func (s Seq2[K, V]) Tap(f func(K, V)) Seq2[K, V]
- func (s Seq2[K, V]) Values() Seq[V]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](s Seq[T], v T) bool
Contains reports whether the sequence contains v. Like Uniq, it is a package function because comparable applies to the element type; predicate checks are covered by Some.
func ToMap ¶
func ToMap[K comparable, V any](s Seq2[K, V]) map[K]V
ToMap eagerly collects a pair sequence into a map.
Types ¶
type KeyValue ¶ added in v0.2.0
type KeyValue[K, V any] struct { K K V V }
KeyValue is a single key/value pair, the element type of Seq2.Collect.
type Number ¶
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
~float32 | ~float64 |
~complex64 | ~complex128
}
Number is a constraint for all builtin numeric types, including complex (which cannot be ordered, so Max/Min stay on cmp.Ordered). It cannot be a method receiver constraint, so summing lives here as a package function, as math/rand/v2 does with its intType.
type Result ¶
type Result[V any] struct { // contains filtered or unexported fields }
Result is a fallible sequence: pairs of (result, error), where success is (result, nil) and failure is (zero, err). The errors travel as data, so they can be inspected or dropped without stopping the pipeline.
It is a Seq2 with the second type argument pre-bound to error, which is what lets its methods express error semantics that generic Seq2 methods cannot (Go does not allow a method to constrain its receiver's type parameters).
func FromResult ¶
FromResult wraps a standard pair iterator as a fallible sequence.
func (Result[V]) CollectErr ¶
CollectErr eagerly evaluates the fallible sequence, returning every successful value and all failures joined into one error with errors.Join, or nil if nothing failed.
func (Result[V]) IgnoreErr ¶
IgnoreErr drops the error pairs and yields only the successful values as a plain sequence from here on: the errors are skipped and the value is kept.
func (Result[V]) Seq ¶
Seq exposes the underlying standard pair iterator, for ranging directly:
for v, err := range res.Seq() { ... }
type Seq ¶
type Seq[T any] struct { // contains filtered or unexported fields }
Seq is a lazy sequence of values of type T.
func Chunk ¶
Chunk groups the sequence into slices of at most n elements. It panics if n is not positive.
Chunk is a package function, not a method: a generic method returning Seq[[]T] instantiates the receiver's own type with []T and trips the compiler's instantiation-cycle check.
func FromKeys ¶
func FromKeys[K comparable, V any](m map[K]V) Seq[K]
FromKeys yields the keys of a map.
func FromValues ¶
func FromValues[K comparable, V any](m map[K]V) Seq[V]
FromValues yields the values of a map.
func Range ¶
Range yields from up to, but not including, to, stepping by step. It panics if step is zero. If step has the wrong sign for the direction from from to to, it yields nothing.
func Uniq ¶
func Uniq[T comparable](s Seq[T]) Seq[T]
Uniq keeps only the first occurrence of each equal element, deduplicating the sequence. Like Max/Min/Sum, it is a package function because its comparable constraint applies to the element type, which a method cannot re-restrict; the key-based variant UniqBy handles non-comparable elements as a method.
func (Seq[T]) Collect ¶
func (s Seq[T]) Collect() []T
Collect eagerly evaluates the sequence into a slice.
func (Seq[T]) FlatMapErr ¶
FlatMapErr is FlatMap with an error source: each element produces either a run of (result, nil) pairs or a single (zero, err) pair.
func (Seq[T]) ForEach ¶ added in v0.2.0
func (s Seq[T]) ForEach(f func(T))
ForEach drives the sequence, calling f for every element.
func (Seq[T]) GroupBy ¶ added in v0.2.0
func (s Seq[T]) GroupBy[K comparable](f func(T) K) map[K][]T
GroupBy buckets elements by the key produced by f, returning a map of the buckets.
func (Seq[T]) KeyBy ¶ added in v0.2.0
func (s Seq[T]) KeyBy[K comparable](key func(T) K) map[K]T
KeyBy pivots a single element per key: the last element for each key wins.
func (Seq[T]) MapErr ¶
MapErr applies f to each element, pairing every outcome: (result, nil) on success, (zero, err) on failure. The error pairs never stop the pipeline; handling them is up to the consumer (IgnoreErr, Errors, CollectErr).
func (Seq[T]) MaxBy ¶ added in v0.2.0
MaxBy returns the element with the greatest key, or ok=false if the sequence is empty.
func (Seq[T]) MinBy ¶ added in v0.2.0
MinBy returns the element with the least key, or ok=false if the sequence is empty.
func (Seq[T]) ReduceErr ¶
ReduceErr is Reduce with a fallible step. It stops at the first error, returning the zero value of U and the error. The remaining elements are not processed.
func (Seq[T]) Seq ¶
Seq exposes the underlying standard iterator, for ranging directly:
for v := range seq.Seq() { ... }
func (Seq[T]) SkipErr ¶
SkipErr keeps elements for which check returns nil, skipping the failures and moving on to the next one.
func (Seq[T]) SumBy ¶ added in v0.2.0
SumBy sums the keys produced by key. The empty sequence sums to zero.
func (Seq[T]) Tap ¶
Tap inspects every element as it flows through, then yields it unchanged. It is the Go counterpart of Elixir's |> tap/2: a hook for logging and side effects in the middle of a pipeline.
func (Seq[T]) UniqBy ¶ added in v0.2.0
func (s Seq[T]) UniqBy[K comparable](key func(T) K) Seq[T]
UniqBy keeps only the first element for each distinct key produced by key.
type Seq2 ¶
type Seq2[K, V any] struct { // contains filtered or unexported fields }
Seq2 is a lazy sequence of key/value pairs.
func FromMap ¶
func FromMap[K comparable, V any](m map[K]V) Seq2[K, V]
FromMap wraps map iteration as a key/value Seq2 (order not defined).
func (Seq2[K, V]) Collect ¶ added in v0.2.0
Collect eagerly evaluates the pair sequence into a slice of KeyValue pairs.
func (Seq2[K, V]) DropWhile ¶ added in v0.2.0
DropWhile discards pairs from the start while pred is true.
func (Seq2[K, V]) Seq ¶
Seq exposes the underlying standard pair iterator, for ranging directly:
for k, v := range pairs.Seq() { ... }
func (Seq2[K, V]) TakeWhile ¶ added in v0.2.0
TakeWhile yields pairs from the start while pred is true.