iterutils

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2025 License: BSD-3-Clause Imports: 3 Imported by: 0

README

iterutils

Go Reference

Utilities for working with Go iterators. This library needs at least Go 1.23 because it uses the iter package in the standard library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](seq iter.Seq[T], pred Pred[T]) bool

All returns true if all values satisfy pred. If seq is empty returns true.

func Any

func Any[T any](seq iter.Seq[T], pred Pred[T]) bool

Any returns true if at least one value satisfies pred. If seq is empty returns false.

func Bool

func Bool[T bool](v bool) bool

Pred function for bool values

func Chain

func Chain[T any](seqs ...iter.Seq[T]) iter.Seq[T]

Chain returns an iter.Seq that combines the given sequences.

func Contains

func Contains[T any](seq iter.Seq[T], value T, eq Eq[T]) bool

Contains returns wether the value is contained in the sequence.

func Count

func Count[T constraints.Integer](start T) iter.Seq[T]

Count returns an iter.Seq that counts from the start value upward.

func Create

func Create[T any](fn CreateFunc[T]) iter.Seq[T]

Create returns a new iter.Seq with the values created bei the given CreateFunc function.

Example: create an endless sequence with random ints.

func RandomInt(n int) iter.Seq[int] {
  if n == 0 {
    return Create(func() (int, bool) { return rand.Int(), false })
  }
  return Create(func() (int, bool) { return rand.IntN(n), false })
}

func Cycle

func Cycle[T any](v T, vs ...T) iter.Seq[T]

Cycle returns an iter.Seq that cycles through the given values.

func DropWhile

func DropWhile[T any](seq iter.Seq[T], pred Pred[T]) iter.Seq[T]

DropWhile drops values from seq as long as pred returns true and returns the rest in a new iter.Seq.

func Enumerate

func Enumerate[T any](seq iter.Seq[T], start int) iter.Seq2[int, T]

Enumerate returns an iter.Seq2 with all values and their position in the sequence.

func Equ

func Equ[T comparable](v1, v2 T) bool

Eq function for comparable values

func Equal

func Equal[T any](seq1, seq2 iter.Seq[T], eq Eq[T]) bool

Equal returns true if both sequences contain the same values in the same order.

func Filter

func Filter[T any](seq iter.Seq[T], pred Pred[T]) iter.Seq[T]

Filter returns a new iter.Seq with all values from seq for which pred returns true.

func Find

func Find[T any](seq iter.Seq[T], value T, eq Eq[T]) iter.Seq2[int, T]

Find returns an iter.Seq2 with all found values and their position in the sequence.

func First

func First[U, V any](seq iter.Seq2[U, V]) iter.Seq[U]

First returns an iter.Seq with the first values of the pairs in an iter.Seq2.

func Map

func Map[T, U any](seq iter.Seq[T], fn MapFunc[T, U]) iter.Seq[U]

Map maps values in a sequence using a mapping function.

func Map2

func Map2[T, U, V any](seq1 iter.Seq[T], seq2 iter.Seq[U], fn Map2Func[T, U, V]) iter.Seq[V]

Map2 maps values in two sequences using a mapping function. It stops when the shortest sequence is exhausted.

func Map3

func Map3[T, U, V, W any](seq1 iter.Seq[T], seq2 iter.Seq[U], seq3 iter.Seq[V], fn Map3Func[T, U, V, W]) iter.Seq[W]

Map3 maps values in three sequences using a mapping function. It stops when the shortest sequence is exhausted.

func Max

func Max[T any](seq iter.Seq[T], cmp Cmp[T]) T

Max returns the maximum value. If seq is empty returns the zero value of T.

func Merge

func Merge[U, V any](seq1 iter.Seq[U], seq2 iter.Seq[V]) iter.Seq2[U, V]

Merge returns an iter.Seq2 combining two [iter.Seq]s. It stops when the shortest sequence is exhausted.

func Min

func Min[T any](seq iter.Seq[T], cmp Cmp[T]) T

Min returns the minimum value. If seq is empty returns the zero value of T.

func New

func New[T any](vs ...T) iter.Seq[T]

New returns a new iter.Seq from the given values.

func Purge

func Purge[T any](seq iter.Seq[T], pred Pred[T]) iter.Seq[T]

Purge returns a new iter.Seq with all values from seq for which pred returns false.

func Reduce

func Reduce[T, U any](seq iter.Seq[T], init U, fn ReduceFunc[T, U]) U

Reduce reduces seq to a single value by accumulating the values in seq using function fn. The first argument to fn is the accumulated value (starting with init).

func Repeat

func Repeat[T any](v T, n int) iter.Seq[T]

Repeat returns an iter.Seq that returns v n times (n == 0 -> no limit).

func Second

func Second[U, V any](seq iter.Seq2[U, V]) iter.Seq[V]

Second returns an iter.Seq with the second values of the pairs in an iter.Seq2.

func Split

func Split[U, V any](seq iter.Seq2[U, V]) (iter.Seq[U], iter.Seq[V])

Split returns two [iter.Seq]s (see First and Second).

func TakeWhile

func TakeWhile[T any](seq iter.Seq[T], pred Pred[T]) iter.Seq[T]

TakeWhile takes values from seq as long as pred returns true and returns them in a new iter.Seq.

func Unique

func Unique[T any](seq iter.Seq[T], cmp Cmp[T]) iter.Seq[T]

Unique returns the unique values from seq in a new iter.Seq.

func Unzip

func Unzip[U, V any](seq iter.Seq[Tuple[U, V]]) (iter.Seq[U], iter.Seq[V])

Unzip unzips a sequence into two.

func Unzip3

func Unzip3[U, V, W any](seq iter.Seq[Tuple3[U, V, W]]) (iter.Seq[U], iter.Seq[V], iter.Seq[W])

Unzip3 unzips a sequence into three.

func Zip

func Zip[U, V any](seq1 iter.Seq[U], seq2 iter.Seq[V]) iter.Seq[Tuple[U, V]]

Zip zips two sequences into one. It stops when the shortest sequence is exhausted.

func Zip3

func Zip3[U, V, W any](seq1 iter.Seq[U], seq2 iter.Seq[V], seq3 iter.Seq[W]) iter.Seq[Tuple3[U, V, W]]

Zip3 zips three sequences into one. It stops when the shortest sequence is exhausted.

Types

type Cmp

type Cmp[T any] func(T, T) int

Compare function.

Returns -1 if the first value is less then the second, 0 if both are equal, 1 if the first value is greater then the second.

type CreateFunc

type CreateFunc[T any] func() (T, bool)

Function type for use with Create. To stop the creation of more values, the bool return value must be true.

type Eq

type Eq[T any] func(T, T) bool

Equal function

type ExtremaS

type ExtremaS[T any] struct {
	Min T
	Max T
}

func Extrema

func Extrema[T any](seq iter.Seq[T], cmp Cmp[T]) ExtremaS[T]

Extrema returns the minimum and maximum values. If seq is empty returns the zero value of T for min and max.

type Map2Func

type Map2Func[T, U, V any] func(x T, y U) V

A Map2Func maps two values to another value.

type Map3Func

type Map3Func[T, U, V, W any] func(x T, y U, z V) W

A Map3Func maps three values to another value.

type MapFunc

type MapFunc[T, U any] func(x T) U

A MapFunc maps a value to another value which may have a different type.

type Pred

type Pred[T any] func(T) bool

Predicate function

type ReduceFunc

type ReduceFunc[T, U any] func(acc U, v T) U

Reduce function

type Tuple

type Tuple[U, V any] struct {
	A U
	B V
}

type Tuple3

type Tuple3[U, V, W any] struct {
	A U
	B V
	C W
}

Jump to

Keyboard shortcuts

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