seq2

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package seq2 extends iter.Seq2 API with convering, filtering, and reducing functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendMapResolv

func AppendMapResolv[S ~Seq2[K, V], K comparable, V, VR any](seq S, resolver func(exists bool, key K, valResolv VR, val V) VR, dest map[K]VR) map[K]VR

AppendMapResolv collects key\value elements into the 'dest' map by iterating over the elements with resolving of duplicated key values.

func AppendMapResolvOrder

func AppendMapResolvOrder[S ~Seq2[K, V], K comparable, V, VR any](seq S, resolver func(exists bool, key K, valResolv VR, val V) VR, order []K, dest map[K]VR) ([]K, map[K]VR)

AppendMapResolvOrder collects key\value elements into the 'dest' map by iterating over the elements with resolving of duplicated key values Additionally populates the 'order' slice by the keys ordered by the time they were added and the resolved key\value map.

func First

func First[S ~Seq2[K, V], K, V any](seq S, condition func(K, V) bool) (k K, v V, ok bool)

First returns the first key\value pair that satisfies the condition.

func Firstt

func Firstt[S ~Seq2[K, V], K, V any](seq S, condition func(K, V) (bool, error)) (k K, v V, ok bool, err error)

Firstt returns the first key\value pair that satisfies the condition.

func Group

func Group[S ~Seq2[K, V], K comparable, V any](seq S) map[K][]V

Group collects the elements of the 'seq' sequence into a new map.

func HasAny

func HasAny[S ~Seq2[K, V], K, V any](seq S, condition func(K, V) bool) bool

HasAny checks whether the seq contains a key\value pair that satisfies the condition.

func Head[S ~Seq2[K, V], K, V any](seq S) (k K, v V, ok bool)

Head returns the first key\value pair.

func MapResolv

func MapResolv[S ~Seq2[K, V], K comparable, V, VR any](seq S, resolver func(exists bool, key K, valResolv VR, val V) VR) map[K]VR

MapResolv collects key\value elements into a new map by iterating over the elements with resolving of duplicated key values.

func MapResolvOrder

func MapResolvOrder[S ~Seq2[K, V], K comparable, V, VR any](seq S, resolver func(exists bool, key K, valResolv VR, val V) VR) ([]K, map[K]VR)

MapResolvOrder collects key\value elements into a new map by iterating over the elements with resolving of duplicated key values. Returns a slice with the keys ordered by the time they were added and the resolved key\value map.

func TrackEach

func TrackEach[S ~Seq2[K, V], K, V any](seq S, consumer func(K, V))

TrackEach applies the 'consumer' function to the seq key\value pairs.

Types

type Seq

type Seq[T any] = func(func(T) bool)

Seq is an iterator-function that allows to iterate over elements of a sequence, such as slice.

func Keys

func Keys[S ~Seq2[K, V], K, V any](seq S) Seq[K]

Keys converts a key/value pairs iterator to an iterator of just keys.

func Values

func Values[S ~Seq2[K, V], K, V any](seq S) Seq[V]

Values converts a key/value pairs iterator to an iterator of just values.

type Seq2

type Seq2[K, V any] = func(func(K, V) bool)

Seq2 is an iterator-function that allows to iterate over key/value pairs of a sequence, such as slice or map. It is used to iterate over slice index/value pairs or map key/value pairs.

func Convert

func Convert[S ~Seq2[Kfrom, Vfrom], Kfrom, Vfrom, Kto, Vto any](seq S, converter func(Kfrom, Vfrom) (Kto, Vto)) Seq2[Kto, Vto]

Convert creates an iterator that applies the 'converter' function to each iterable key\value pair.

func ConvertKey

func ConvertKey[S ~Seq2[Kfrom, V], Kfrom, Kto, V any](seq S, converter func(Kfrom) Kto) Seq2[Kto, V]

ConvertKey returns a seq that applies the 'converter' function to keys.

func ConvertValue

func ConvertValue[S ~Seq2[K, Vfrom], K, Vfrom, Vto any](seq S, converter func(Vfrom) Vto) Seq2[K, Vto]

ConvertValue returns a seq that applies the 'converter' function to values.

func Filt

func Filt[S ~Seq2[K, V], K, V any](seq S, filter func(K, V) (bool, error)) Seq2[c.KV[K, V], error]

Filt creates an erroreable iterator that iterates only those key\value pairs for which the 'filter' function returns true.

func Filter

func Filter[S ~Seq2[K, V], K, V any](seq S, filter func(K, V) bool) Seq2[K, V]

Filter creates an iterator that iterates only those elements for which the 'filter' function returns true.

func FilterKey

func FilterKey[S ~Seq2[K, V], K, V any](seq S, filter func(K) bool) Seq2[K, V]

FilterKey returns a seq consisting of key/value pairs where the key satisfies the condition of the 'filter' function.

func FilterValue

func FilterValue[S ~Seq2[K, V], K, V any](seq S, filter func(V) bool) Seq2[K, V]

FilterValue returns a seq consisting of key/value pairs where the value satisfies the condition of the 'filter' function.

func Union

func Union[S ~Seq2[K, V], K, V any](seq ...S) Seq2[K, V]

Union combines several sequences into one.

type SeqE

type SeqE[T any] = Seq2[T, error]

SeqE is a specific iterator form that allows to retrieve a value with an error as second parameter of the iterator. It is used as a result of applying functions like seq.Conv, which may throw an error during iteration. At each iteration step, it is necessary to check for the occurrence of an error.

for e, err := range seqence {
    if err != nil {
        break
    }
    ...
}

func Conv

func Conv[S ~Seq2[Kfrom, Vfrom], Kfrom, Vfrom, Kto, Vto any](seq S, converter func(Kfrom, Vfrom) (Kto, Vto, error)) SeqE[c.KV[Kto, Vto]]

Conv creates an errorable seq that applies the 'converter' function to the iterable key\value pairs.

func ConvKey

func ConvKey[S ~Seq2[Kfrom, V], Kfrom, Kto, V any](seq S, converter func(Kfrom) (Kto, error)) SeqE[c.KV[Kto, V]]

ConvKey returns a seq that applies the 'converter' function to keys.

func ConvValue

func ConvValue[S ~Seq2[K, Vfrom], K, Vfrom, Vto any](seq S, converter func(Vfrom) (Vto, error)) SeqE[c.KV[K, Vto]]

ConvValue returns a seq that applies the 'converter' function to values.

Jump to

Keyboard shortcuts

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