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: 4

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 Conv added in v0.0.17

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

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

func ConvKey added in v0.0.17

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

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

func ConvValue added in v0.0.17

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

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

func Convert

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

Convert creates an iterator that applies the 'converter' function to each iterable element.

func ConvertKey added in v0.0.17

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

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

func ConvertValue added in v0.0.17

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

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

func Filt added in v0.0.17

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

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) seq.Seq2[K, V]

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

func FilterKey added in v0.0.17

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

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

func FilterValue added in v0.0.17

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

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

func First added in v0.0.15

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 added in v0.0.17

func Firstt[S ~Seq2[K, V], K, V any](seq S, filter 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 added in v0.0.17

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

HasAny checks whether the seq contains an element 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 Keys

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

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

func Map

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

Map collects key\value elements into a new map by iterating over the elements.

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 Of

func Of[K, V any](elements ...c.KV[K, V]) seq.Seq2[K, V]

Of creates an key/value pairs iterator over the elements.

func OfIndexed added in v0.0.15

func OfIndexed[T any](amount int, getAt func(int) T) seq.Seq2[int, T]

OfIndexed builds an indexed Seq2 iterator by extracting elements from an indexed source. the len is length ot the source. the getAt retrieves an element by its index from the source.

func OfIndexedKV added in v0.0.17

func OfIndexedKV[K, V any](amount int, getAt func(int) (K, V)) seq.Seq2[K, V]

OfIndexedKV builds an indexed Seq2 iterator by extracting key\value pairs from an indexed source. the len is length ot the source. the getAt retrieves a key\value pair by its index from the source.

func OfIndexedPair added in v0.0.17

func OfIndexedPair[K, V any](amount int, getKey func(int) K, getValue func(int) V) seq.Seq2[K, V]

OfIndexedPair builds an indexed Seq2 iterator by extracting key\value pairs from an indexed source. the len is length ot the source. the getKey retrieves a key by its index from the source. the getValue retrieves a value by its index from the source.

func OfMap

func OfMap[K comparable, V any](elements map[K]V) seq.Seq2[K, V]

OfMap creates an key/value pairs iterator over the elements map.

func Range added in v0.0.15

func Range[T constraints.Integer | rune](from T, toExclusive T) seq.Seq2[int, T]

Range creates a sequence that generates integers in the range defined by from and to exclusive.

func RangeClosed added in v0.0.15

func RangeClosed[T constraints.Integer | rune](from T, toInclusive T) seq.Seq2[int, T]

RangeClosed creates a sequence that generates integers in the range defined by from and to inclusive

func Reduce added in v0.0.16

func Reduce[S ~Seq2[K, V], K, V, T any](seq S, merge func(prev *T, k K, v V) T) T

Reduce reduces the elements of the seq into one using the 'merge' function.

func ReduceOK added in v0.0.16

func ReduceOK[S ~Seq2[K, V], K, V, T any](seq S, merge func(prev *T, k K, v V) T) (result T, ok bool)

ReduceOK reduces the elements of the seq into one using the 'merge' function. Returns ok==false if the seq returns ok=false at the first call (no more elements).

func Reducee added in v0.0.16

func Reducee[S ~Seq2[K, V], K, V, T any](seq S, merge func(prev *T, k K, v V) (T, error)) (T, error)

Reducee reduces the elements of the seq into one using the 'merge' function.

func ReduceeOK added in v0.0.16

func ReduceeOK[S ~Seq2[K, V], K, V, T any](seq S, merge func(prev *T, k K, v V) (T, error)) (result T, ok bool, err error)

ReduceeOK reduces the elements of the seq into one using the 'merge' function. Returns ok==false if the seq returns ok=false at the first call (no more elements).

func Series added in v0.0.15

func Series[T any](first T, next func(int, T) (T, bool)) seq.Seq2[int, T]

Series makes a sequence by applying the 'next' function to the previous step generated value.

func Skip added in v0.0.15

func Skip[S ~Seq2[K, V], K, V any](n int, seq S) seq.Seq2[K, V]

Skip returns the seq without first n elements.

func SkipWhile added in v0.0.16

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

SkipWhile returns a sequence without first elements of the seq that dont'math the filter.

func ToSeq added in v0.0.16

func ToSeq[S ~Seq2[K, V], T, K, V any](seq S, converter func(K, V) T) seq.Seq[T]

ToSeq converts an iterator of key/value pairs elements to an iterator of single elements by applying the 'converter' function to each iterable pair.

func Top added in v0.0.15

func Top[S ~Seq2[K, V], K, V any](n int, seq S) S

Top returns a sequence of top n key\value pairs.

func TrackEach added in v0.0.15

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

TrackEach applies the 'consumer' function to the seq elements.

func Union added in v0.0.15

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

Union combines several sequences into one.

func Values

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

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

func While added in v0.0.16

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

While cuts tail elements of the seq that don't match the filter.

Types

type Seq2 added in v0.0.15

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.

Jump to

Keyboard shortcuts

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