iterutils

package
v0.0.0-...-c676e8e Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](iter iter.Seq[T], predicate Predicate[T]) bool

All returns true if all elements in the iter satisfy the predicate and false otherwise. It returns false at the first element that does not satisfy the predicate, and does not check the rest of the elements. If the iter is empty, it returns true. If the iter is nil, it panics.

func All2

func All2[K comparable, V any](iter iter.Seq2[K, V], predicate Predicate2[K, V]) bool

All2 returns true if all key-value pairs in the iter satisfy the predicate and false otherwise. It returns false at the first element that does not satisfy the predicate, and does not check the rest of the elements. If the iter is empty, it returns true. If the iter is nil, it panics.

func Any

func Any[T any](iter iter.Seq[T], predicate Predicate[T]) bool

Any returns true if at least one element in the iter satisfies the predicate and false otherwise. It returns true at the first element that satisfies the predicate, and does not check the rest of the elements. If the iter is empty, it returns false. If the iter is nil, it panics.

func Any2

func Any2[K comparable, V any](iter iter.Seq2[K, V], predicate Predicate2[K, V]) bool

Any2 returns true if at least one key-value pair in the iter satisfies the predicate and false otherwise. It returns true at the first element that satisfies the predicate, and does not check the rest of the elements. If the iter is empty, it returns false. If the iter is nil, it panics.

func Append

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

Append appends the element to the input iterator. If the input iterator is nil, it panics.

func Append2

func Append2[K comparable, V any](iter iter.Seq2[K, V], k K, v V) iter.Seq2[K, V]

Append2 appends the key-value pair to the input iterator. If the input iterator is nil, it panics.

func Concat

func Concat[T any](iters ...iter.Seq[T]) iter.Seq[T]

Concat concatenates the input iterators and returns a new iterator with the elements of the input iterators. If the input iterators are nil, it panics.

func Concat2

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

Concat2 concatenates the input iterators and returns a new iterator with the key-value pairs of the input iterators. If the input iterators are nil, it panics.

func Contains

func Contains[T comparable](iter iter.Seq[T], item T) bool

Contains returns true if the item is in the iter and false otherwise. If the iter is nil, it panics.

func Count

func Count[T any](iter iter.Seq[T], predicate Predicate[T]) int

Count returns the number of elements in the iter that satisfy the predicate. If the iter is nil, it panics.

func Count2

func Count2[K comparable, V any](iter iter.Seq2[K, V], predicate Predicate2[K, V]) int

Count2 returns the number of key-value pairs in the iter that satisfy the predicate. If the iter is nil, it panics.

func Equal

func Equal[T comparable](iter1, iter2 iter.Seq[T]) bool

Equal returns true if the values in the two iters are equal and false otherwise. If either one of the iters is nil, it panics.

func Equal2

func Equal2[K comparable, V comparable](iter1, iter2 iter.Seq2[K, V]) bool

Equal2 returns true if the key-value pairs in the two iters are equal and false otherwise. If either one of the iters is nil, it panics.

func Filter

func Filter[T any](iter iter.Seq[T], predicate Predicate[T]) iter.Seq[T]

Filter returns a new iterator with the elements that satisfy the predicate. If the input iterator is nil, it panics.

func Filter2

func Filter2[K comparable, V any](iter iter.Seq2[K, V], predicate Predicate2[K, V]) iter.Seq2[K, V]

Filter2 returns a new iterator with the key-value pairs that satisfy the predicate. If the input iterator is nil, it panics.

func Find

func Find[T any](iter iter.Seq[T], predicate Predicate[T]) (T, bool)

Find returns the first element that satisfies the predicate and a boolean. that indicates if such an element was found. If the iter is nil, it panics.

func Find2

func Find2[K comparable, V any](iter iter.Seq2[K, V], predicate Predicate2[K, V]) (K, V, bool)

Find2 returns the first key-value pair that satisfies the predicate and a boolean. that indicates if such a pair was found. If the iter is nil, it panics.

func First

func First[T any](iter iter.Seq[T]) T

First returns the first element in the iter. If the iter is empty or nil, it panics.

func First2

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

First2 returns the first key-value pair in the iter. If the iter is empty or nil, it panics.

func ForEach

func ForEach[T any](iter iter.Seq[T], f func(T))

ForEach applies the function to each element in the iter. If the iter is nil, it panics.

func ForEach2

func ForEach2[K comparable, V any](iter iter.Seq2[K, V], f func(K, V))

ForEach2 applies the function to each key-value pair in the iter. If the iter is nil, it panics.

func IsEmpty

func IsEmpty[T any](iter iter.Seq[T]) bool

IsEmpty returns true if the iter is empty and false otherwise. If the iter is nil, it panics.

func IsEmpty2

func IsEmpty2[K comparable, V any](iter iter.Seq2[K, V]) bool

IsEmpty2 returns true if the iter is empty and false otherwise. If the iter is nil, it panics.

func IsValidKey

func IsValidKey[K comparable, V any](iter iter.Seq2[K, V], key K) bool

IsValidKey returns true if the key is in the iter and false otherwise. If the iter is nil, it panics.

func Keys

func Keys[K comparable, V any](iter iter.Seq2[K, V]) iter.Seq[K]

Keys returns a new iterator with the keys of the input iterator. If the input iterator is nil, it panics.

func Last

func Last[T any](iter iter.Seq[T]) T

Last returns the last element in the iter. If the iter is empty or nil, it panics.

func Last2

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

Last2 returns the last key-value pair in the iter. If the iter is empty or nil, it panics.

func Len

func Len[T any](iter iter.Seq[T]) int

Len returns the number of elements in the iter. If the iter is nil, it panics.

func Len2

func Len2[K comparable, V any](iter iter.Seq2[K, V]) int

Len2 returns the number of key-value pairs in the iter. If the iter is nil, it panics.

func Map

func Map[T, U any](iter iter.Seq[T], f func(T) U) iter.Seq[U]

Map applies the function to each element of the input iterator and returns a new iterator with the results. If the input iterator is nil, it panics.

func Map12

func Map12[K comparable, V, U any](iter iter.Seq[V], f func(V) (K, U)) iter.Seq2[K, U]

Map12 applies the function to each element of the input iterator and returns a new iterator with the resulting key-value pairs. If the input iterator is nil, it panics.

func Map2

func Map2[K, L comparable, V, U any](iter iter.Seq2[K, V], f func(K, V) (L, U)) iter.Seq2[L, U]

Map2 applies the function to each key-value pair of the input iterator and returns a new iterator with the results. If the input iterator is nil, it panics.

func Map21

func Map21[K comparable, V, U any](iter iter.Seq2[K, V], f func(K, V) U) iter.Seq[U]

Map21 applies the function to each key-value pair of the input iterator and returns a new iterator with the resulting values. If the input iterator is nil, it panics.

func Max

func Max[T cmp.Ordered](iter iter.Seq[T]) T

Max returns the maximum element in the iter. If the iter is empty or nil, it panics.

func Max2

func Max2[K comparable, V cmp.Ordered](iter iter.Seq2[K, V]) (K, V)

Max2 returns the maximum key-value pair in the iter. If the iter is empty or nil, it panics.

func Middle

func Middle[T any](iter iter.Seq[T]) T

Middle returns the middle element of the iter. If the iter has an even number of elements, it returns the first element of the second half. If the iter is empty or nil, it panics.

func Middle2

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

Middle2 returns the middle key-value pair of the iter. If the iter has an even number of elements, it returns the first key-value pair of the second half. If the iter is empty or nil, it panics.

func Min

func Min[T cmp.Ordered](iter iter.Seq[T]) T

Min returns the minimum element in the iter. If the iter is empty or nil, it panics.

func Min2

func Min2[K comparable, V cmp.Ordered](iter iter.Seq2[K, V]) (K, V)

Min2 returns the minimum key-value pair in the iter. If the iter is empty or nil, it panics.

func NewFromFunc

func NewFromFunc[T any](f func() T, n int) iter.Seq[T]

NewFromFunc returns a new iterator that yields the values returned by the given function. The function takes no arguments. The length of the iterator is n

func NewFromFunc2

func NewFromFunc2[K comparable, V any](f func() (K, V), n int) iter.Seq2[K, V]

NewFromFunc2 returns a new iterator that yields the key-value pairs returned by the given function. The function takes no arguments. The length of the iterator is n.

func NewFromFuncIterations

func NewFromFuncIterations[T any](f func(int) T, n int) iter.Seq[T]

NewFromFuncIterations returns a new iterator that yields the values returned by the given function. The function takes the current iteration number as an argument. The length of the iterator is n.

func NewFromFuncIterations2

func NewFromFuncIterations2[K comparable, V any](f func(int) (K, V), n int) iter.Seq2[K, V]

NewFromFuncIterations2 returns a new iterator that yields the key-value pairs returned by the given function. The function takes the current iteration number as an argument. The length of the iterator is n.

func NewFromMap

func NewFromMap[K comparable, V any](m map[K]V) iter.Seq2[K, V]

NewFromMap returns a new iterator that yields the key-value pairs of the given map.

func NewFromRepeat

func NewFromRepeat[T any](value T, count int) iter.Seq[T]

NewFromRepeat returns a new iterator that yields the same value a given number of times.

func NewFromSlice

func NewFromSlice[T any](slice []T) iter.Seq[T]

NewFromSlice returns a new iterator that yields the elements of the given slice.

func NewFromSlice2

func NewFromSlice2[T any](slice []T) iter.Seq2[int, T]

NewFromSlice2 returns a new iterator that yields the key-value pairs of the given slice, where keys are the indices of the elements.

func None

func None[T any](iter iter.Seq[T], predicate Predicate[T]) bool

None returns true if none of the elements in the iter satisfy the predicate and false otherwise. It returns false at the first element that satisfies the predicate, and does not check the rest of the elements. If the iter is empty, it returns true. If the iter is nil, it panics.

func None2

func None2[K comparable, V any](iter iter.Seq2[K, V], predicate Predicate2[K, V]) bool

None2 returns true if none of the key-value pairs in the iter satisfy the predicate and false otherwise. It returns false at the first element that satisfies the predicate, and does not check the rest of the elements. If the iter is empty, it returns true. If the iter is nil, it panics.

func Product

func Product[T types.Number](iter iter.Seq[T]) T

Product returns the product of all elements in the iter. If the iter is nil, it panics.

func Product2

func Product2[K comparable, V types.Number](iter iter.Seq2[K, V]) V

Product2 returns the product of all values in the iter. If the iter is nil, it panics.

func Reduce

func Reduce[T any](iter iter.Seq[T], f func(T, T) T, initialValue T) T

Reduce applies the function to each element in the iter and returns the accumulated value. If the iter is nil, it panics.

func Reduce2

func Reduce2[K comparable, V any](iter iter.Seq2[K, V], f func(V, V, K) V, initialValue V) V

Reduce2 applies the function to each key-value pair in the iter and returns the accumulated value. If the iter is nil, it panics.

func RemoveKey

func RemoveKey[K comparable, V any](iter iter.Seq2[K, V], key K) iter.Seq2[K, V]

RemoveKey returns a new iterator with the key-value pairs of the input iterator, removing the element with the given key. If the input iterator is nil, it panics.

func RemoveNth

func RemoveNth[T any](iter iter.Seq[T], index int) iter.Seq[T]

RemoveNth returns a new iterator with the elements of the input iterator, removing the element at the given index. If the input iterator is nil, it panics. If the index is out of bounds, the result will be the same as the input iterator.

func Skip

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

Skip returns a new iterator with the elements of the input iterator, skipping the first n elements. If the input iterator is nil, it panics. If n is greater than the length of the input iterator, the result will be an empty iterator.

func Skip2

func Skip2[K comparable, V any](iter iter.Seq2[K, V], n int) iter.Seq2[K, V]

Skip2 returns a new iterator with the key-value pairs of the input iterator, skipping the first n elements. If the input iterator is nil, it panics. If n is greater than the length of the input iterator, the result will be an empty iterator.

func Sort

func Sort[T cmp.Ordered](iter iter.Seq[T], less func(T, T) bool) iter.Seq[T]

Sort sorts the elements of the input iterator and returns a new iterator with the sorted elements. If the input iterator is nil, it panics.

func Sort2

func Sort2[K comparable, V cmp.Ordered](iter iter.Seq2[K, V], less func(K, V, K, V) bool) iter.Seq2[K, V]

Sort2 sorts the key-value pairs of the input iterator and returns a new iterator with the sorted key-value pairs. If the input iterator is nil, it panics.

func Split

func Split[T any](i iter.Seq[T], predicate func(T) bool) iter.Seq[iter.Seq[T]]

Split splits the elements of the input iterator into smaller iterators, based on the given predicate. If the input iterator is nil, it panics.

func Split2

func Split2[K comparable, V any](i iter.Seq2[K, V], predicate func(K, V) bool) iter.Seq[iter.Seq2[K, V]]

Split2 splits the key-value pairs of the input iterator into smaller iterators, based on the given predicate. If the input iterator is nil, it panics.

func Sum

func Sum[T types.Summable](iter iter.Seq[T]) T

Sum returns the sum of all elements in the iter. If the iter is nil, it panics.

func Sum2

func Sum2[K comparable, V types.Summable](iter iter.Seq2[K, V]) V

Sum2 returns the sum of all values in the iter. If the iter is nil, it panics.

func Swap

func Swap[T any](iter iter.Seq[T], i, j int) iter.Seq[T]

Swap swaps the elements at the given indices in the input iterator. If the input iterator is nil, it panics. If the indices are out of bounds, it panics.

func Swap2

func Swap2[K comparable, V any](iter iter.Seq2[K, V], i, j int) iter.Seq2[K, V]

Swap2 swaps the key-value pairs at the given indices in the input iterator. If the input iterator is nil, it panics. If the indices are out of bounds, it panics.

func SwapByKey

func SwapByKey[K comparable, V any](iter iter.Seq2[K, V], key1, key2 K) iter.Seq2[K, V]

SwapByKey swaps the values of the key-value pairs with the given keys in the input iterator. If the input iterator is nil, it panics. If the keys are not present in the input iterator, it panics.

func ToMap

func ToMap[K comparable, V any](iter iter.Seq2[K, V]) map[K]V

ToMap collects the key-value pairs of the given iterator into a map. If the iterator is infinite, this function will not return. If the iterator is nil, this function will panic.

func ToMapN

func ToMapN[K comparable, V any](iter iter.Seq2[K, V], n uint) map[K]V

ToMapN collects the first n key-value pairs of the given iterator into a map. If n is greater than the length of the iterator, this function will return the whole iterator. If the iterator is nil, this function will panic.

func ToSlice

func ToSlice[T any](iter iter.Seq[T]) []T

ToSlice collects the elements of the given iterator into a slice. If the iterator is infinite, this function will not return. If the iterator is nil, this function will panic.

func ToSlice2

func ToSlice2[K types.Integer, V any](iter iter.Seq2[K, V]) []V

ToSlice2 collects the key-value pairs of the given iterator into a slice. The keys will be the indices of the elements. If the iterator is infinite, this function will not return. If the iterator is nil, this function will panic.

func ToSliceN

func ToSliceN[T any](iter iter.Seq[T], n uint) []T

ToSliceN collects the first n elements of the given iterator into a slice. If n is greater than the length of the iterator, the function will return the whole iterator. If the iterator is nil, this function will panic.

func Values

func Values[K comparable, V any](iter iter.Seq2[K, V]) iter.Seq[V]

Values returns a new iterator with the values of the input iterator. If the input iterator is nil, it panics.

func Zip

func Zip[T, U, V any](iter1 iter.Seq[T], iter2 iter.Seq[U], f func(T, U) V) iter.Seq[V]

Zip applies the function to each pair of elements from the two input iterators and returns a new iterator with the results. If the input iterators are nil, it panics. If the input iterators have different lengths, the result will have the length of the shortest iterator.

Types

type Predicate

type Predicate[T any] func(T) bool

Predicate is a function that takes a value and returns a boolean

type Predicate2

type Predicate2[K comparable, V any] func(K, V) bool

Predicate2 is a function that takes a key and a value and returns a boolean

Jump to

Keyboard shortcuts

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