iterutils

package
v0.0.0-...-7685c8a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

README

Iterutils

Functional utilities for working with Go 1.23+ iterators (iter.Seq and iter.Seq2), enabling lazy evaluation and efficient data processing.

Features

  • Transformations - Map, filter, and transform sequences
  • Composition - Concatenate, flatten, and zip sequences
  • Predicates - Check conditions across sequences (any, all, contains, equal)
  • Reductions - Reduce sequences to single values with error handling
  • Truncation - Limit sequence length

Supports both single-value (iter.Seq) and key-value pair (iter.Seq2) iterators with generic types.

Documentation

Overview

Package iterutils provides functional utilities for working with Go 1.23+ iterators (iter.Seq and iter.Seq2), enabling lazy evaluation and efficient data processing.

See README.md for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[V any](seq iter.Seq[V], f func(V) bool) bool

All checks if all elements in the sequence satisfy the given predicate function.

func All2

func All2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) bool

All2 checks if all key-value pairs in the sequence satisfy the given predicate function.

func Any

func Any[V any](seq iter.Seq[V], f func(V) bool) bool

Any checks if any element in the sequence satisfies the given predicate function.

func Any2

func Any2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) bool

Any2 checks if any key-value pair in the sequence satisfies the given predicate function.

func Concat

func Concat[V any](seqs ...iter.Seq[V]) iter.Seq[V]

Concat concatenates multiple sequences of the same value type into a single sequence.

func Concat2

func Concat2[K, V any](seqs ...iter.Seq2[K, V]) iter.Seq2[K, V]

Concat2 concatenates multiple sequences of key-value pairs into a single sequence.

func Contains

func Contains[In comparable](seq iter.Seq[In], v In) bool

Contains checks if the given value is present in the sequence.

func Contains2

func Contains2[K comparable, V comparable](seq iter.Seq2[K, V], k K, v V) bool

Contains2 checks if the given key-value pair is present in the sequence.

func ContainsFunc

func ContainsFunc[In any](seq iter.Seq[In], v In, f func(In, In) bool) bool

ConstainsFunc checks if the given value is present in the sequence using a custom equality function.

func ContainsFunc2

func ContainsFunc2[K any, V any](seq iter.Seq2[K, V], k K, v V, f func(K, V, K, V) bool) bool

func Empty

func Empty[V any]() iter.Seq[V]

Empty returns an empty sequence of the given value type.

func Empty2

func Empty2[K, V any]() iter.Seq2[K, V]

Empty2 returns an empty sequence of key-value pairs of the given key and value types.

func Equal

func Equal[V comparable](x, y iter.Seq[V]) bool

Equal checks if two sequences of the same value type are equal.

func Equal2

func Equal2[K, V comparable](x, y iter.Seq2[K, V]) bool

Equal2 checks if two sequences of key-value pairs are equal.

func EqualFunc

func EqualFunc[V1, V2 any](x iter.Seq[V1], y iter.Seq[V2], f func(V1, V2) bool) bool

EqualFunc checks if two sequences are equal using a custom equality function.

func EqualFunc2

func EqualFunc2[K1, V1, K2, V2 any](x iter.Seq2[K1, V1], y iter.Seq2[K2, V2], f func(K1, V1, K2, V2) bool) bool

EqualFunc2 checks if two sequences of key-value pairs are equal using a custom equality function.

func Filter

func Filter[V any](seq iter.Seq[V], f func(V) bool) iter.Seq[V]

Filter filters the elements of the sequence based on the given predicate function.

func Filter2

func Filter2[K, V any](seq iter.Seq2[K, V], f func(K, V) bool) iter.Seq2[K, V]

Filter2 filters the key-value pairs of the sequence based on the given predicate function.

func Flatten

func Flatten[V any](seq iter.Seq[iter.Seq[V]]) iter.Seq[V]

Flatten flattens a sequence of sequences into a single sequence by yielding all elements from each inner sequence.

func Flatten2

func Flatten2[K, V any](seq iter.Seq[iter.Seq2[K, V]]) iter.Seq2[K, V]

Flatten2 flattens a sequence of key-value pair sequences into a single key-value pair sequence.

func Map

func Map[In, Out any](seq iter.Seq[In], f func(In) Out) iter.Seq[Out]

Map applies the given function to each element of the input sequence and returns a new sequence of the results.

func Map2

func Map2[KIn, KOut, VIn, VOut any](seq iter.Seq2[KIn, VIn], f func(KIn, VIn) (KOut, VOut)) iter.Seq2[KOut, VOut]

Map2 applies the given function to each key-value pair of the input sequence and returns a new sequence of the results.

func MapKeys2

func MapKeys2[KIn, KOut, V any](seq iter.Seq2[KIn, V], f func(KIn, V) KOut) iter.Seq2[KOut, V]

MapKeys2 applies the given function to each key of the input key-value pair sequence and returns a new sequence with transformed keys.

func MapValues2

func MapValues2[K, VIn, VOut any](seq iter.Seq2[K, VIn], f func(K, VIn) VOut) iter.Seq2[K, VOut]

MapValues2 applies the given function to each value of the input key-value pair sequence and returns a new sequence with transformed values.

func Reduce

func Reduce[Accum, V any](seq iter.Seq[V], accum Accum, f func(Accum, V) Accum) Accum

Reduce reduces the sequence to a single value by applying the given binary function.

func Reduce2

func Reduce2[Accum, K, V any](seq iter.Seq2[K, V], accum Accum, f func(Accum, K, V) Accum) Accum

Reduce2 reduces the sequence of key-value pairs to a single value by applying the given binary function.

func ReduceOrError

func ReduceOrError[Accum, V any](seq iter.Seq[V], accum Accum, f func(Accum, V) (Accum, error)) (Accum, error)

ReduceOrError reduces the sequence to a single value by applying the given binary function that can return an error.

func ReduceOrError2

func ReduceOrError2[Accum, K, V any](seq iter.Seq2[K, V], accum Accum, f func(Accum, K, V) (Accum, error)) (Accum, error)

ReduceOrError2 reduces the sequence of key-value pairs to a single value by applying the given binary function that can return an error.

func Truncate

func Truncate[V any](seq iter.Seq[V], n int) iter.Seq[V]

Truncate limits the sequence to the first n elements.

func Truncate2

func Truncate2[K, V any](seq iter.Seq2[K, V], n int) iter.Seq2[K, V]

Truncate2 limits the sequence of key-value pairs to the first n elements.

func Zip

func Zip[V1, V2 any](x iter.Seq[V1], y iter.Seq[V2]) iter.Seq[Zipped[V1, V2]]

Zip zips two sequences together, yielding pairs of values along with presence flags.

func Zip2

func Zip2[K1, V1, K2, V2 any](x iter.Seq2[K1, V1], y iter.Seq2[K2, V2]) iter.Seq[Zipped2[K1, V1, K2, V2]]

Zip2 zips two sequences of key-value pairs together, yielding pairs of key-value pairs along with presence flags.

func ZipTruncate

func ZipTruncate[V1, V2 any](x iter.Seq[V1], y iter.Seq[V2]) iter.Seq2[V1, V2]

ZipTruncate zips two sequences together, stopping when the shorter sequence is exhausted.

Types

type Zipped

type Zipped[V1, V2 any] struct {
	V1  V1
	Ok1 bool // whether V1 is present (if not, it will be false)
	V2  V2
	Ok2 bool // whether V2 is present (if not, it will be false)
}

Zipped represents a pair of values from two sequences, along with flags indicating their presence.

type Zipped2

type Zipped2[K1, V1, K2, V2 any] struct {
	K1  K1
	V1  V1
	Ok1 bool // whether K1, V1 are present (if not, they will be false)
	K2  K2
	V2  V2
	Ok2 bool // whether K2, V2 are present (if not, they will be false)
}

Zipped2 represents a pair of key-value pairs from two sequences, along with flags indicating their presence.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL