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 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 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]) Each(f func(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]) GroupByFunc[K comparable](f func(T) K) map[K][]T
- func (s Seq[T]) KeyByFunc[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]) MaxByFunc[K cmp.Ordered](key func(T) K) (T, bool)
- func (s Seq[T]) MinByFunc[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]) SumByFunc[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]) UniqByFunc[K comparable](key func(T) K) Seq[T]
- func (s Seq[T]) Zip[U any](o Seq[U]) Seq2[T, U]
- type Seq2
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 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 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 UniqByFunc 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]) Each ¶
func (s Seq[T]) Each(f func(T))
Each drives the sequence, calling f for every element.
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]) GroupByFunc ¶
func (s Seq[T]) GroupByFunc[K comparable](f func(T) K) map[K][]T
GroupByFunc buckets elements by the key produced by f, returning a map of the buckets.
func (Seq[T]) KeyByFunc ¶
func (s Seq[T]) KeyByFunc[K comparable](key func(T) K) map[K]T
KeyByFunc 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]) MaxByFunc ¶
MaxByFunc returns the element with the greatest key, or ok=false if the sequence is empty.
func (Seq[T]) MinByFunc ¶
MinByFunc 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. A failing step is skipped (the accumulator is kept) and remembered as the first error; evaluation runs to the end, so every value is 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]) 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]) UniqByFunc ¶
func (s Seq[T]) UniqByFunc[K comparable](key func(T) K) Seq[T]
UniqByFunc keeps only the first element of each run of equal keys, deduplicating by the comparable 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]) Seq ¶
Seq exposes the underlying standard pair iterator, for ranging directly:
for k, v := range pairs.Seq() { ... }