seq

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: 8 Imported by: 7

Documentation

Overview

Package seq extends iter.Seq API with convering, filtering, and reducing functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accum

func Accum[T any, S ~seq[T]](first T, seq S, merge func(T, T) T) T

Accum accumulates a value by using the 'first' argument to initialize the accumulator and sequentially applying the 'merge' functon to the accumulator and each element of the 'seq' sequence.

func Accumm

func Accumm[T any, S ~seq[T]](first T, seq S, merge func(T, T) (T, error)) (accumulator T, err error)

Accumm accumulates a value by using the 'first' argument to initialize the accumulator and sequentially applying the 'merge' functon to the accumulator and each element of the 'seq' sequence.

func Append

func Append[S ~seq[T], TS ~[]T, T any](seq S, out TS) TS

Append collects the elements of the 'seq' sequence into the specified 'out' slice.

func Contains

func Contains[S ~seq[T], T comparable](seq S, example T) bool

Contains finds the first element that equal to the example and returns true.

func First

func First[S ~seq[T], T any](seq S, condition func(T) bool) (v T, ok bool)

First returns the first element that satisfies the condition.

func Firstt

func Firstt[S ~seq[T], T any](seq S, condition func(T) (bool, error)) (v T, ok bool, err error)

Firstt returns the first element that satisfies the condition.

func ForEach added in v0.0.15

func ForEach[T any](seq Seq[T], consumer func(T))

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

func Group added in v0.0.15

func Group[S ~seq[T], T any, K comparable, V any](seq S, keyExtractor func(T) K, valExtractor func(T) V) map[K][]V

Group collects the seq elements into a new map. The keyExtractor converts an element to a key. The valExtractor converts an element to a value.

func HasAny

func HasAny[S ~seq[T], T any](seq S, filter func(T) bool) bool

HasAny checks whether the seq contains an element that satisfies the condition.

func Head[S ~seq[T], T any](seq S) (v T, ok bool)

Head returns the first element.

func Reduce

func Reduce[S ~seq[T], T any](seq S, merge func(T, T) T) T

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

func ReduceOK

func ReduceOK[S ~seq[T], T any](seq S, merge func(T, T) 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

func Reducee[S ~seq[T], T any](seq S, merge func(T, T) (T, error)) (T, error)

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

func ReduceeOK

func ReduceeOK[S ~seq[T], T any](seq S, merge func(T, T) (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 Slice

func Slice[S ~seq[T], T any](seq S) []T

Slice collects the elements of the 'seq' sequence into a new slice.

func SliceCap

func SliceCap[S ~seq[T], T any](seq S, capacity int) (out []T)

SliceCap collects the elements of the 'seq' sequence into a new slice with predefined capacity.

func Sum

func Sum[S ~seq[T], T op.Summable](seq S) (out T)

Sum returns the sum of all elements.

Types

type Seq added in v0.0.15

type Seq[T any] seq[T]

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

func Convert

func Convert[S ~seq[From], From, To any](seq S, converter func(From) To) Seq[To]

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

func ConvertNilSafe added in v0.0.17

func ConvertNilSafe[S ~seq[*From], From, To any](seq S, converter func(*From) *To) Seq[*To]

ConvertNilSafe creates a seq that filters not nil elements, converts that ones, filters not nils after converting and returns them.

func ConvertOK added in v0.0.15

func ConvertOK[S ~seq[From], From, To any](seq S, converter func(from From) (To, bool)) Seq[To]

ConvertOK creates an iterator that applies the 'converter' function to each iterable element. The converter may returns a value or ok=false to exclude the value from the sequence.

func Filter

func Filter[S ~seq[T], T any](seq S, filter func(T) bool) Seq[T]

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

func Flat

func Flat[S ~seq[From], STo ~[]To, From any, To any](seq S, flattener func(From) STo) Seq[To]

Flat is used to iterate over a two-dimensional sequence in single dimension form, like:

var arrays iter.Seq[[]int]
...
for e := range seq.Flat(arrays, as.Is) {
    ...
}

func FlatSeq added in v0.0.15

func FlatSeq[S ~seq[From], STo ~seq[To], From any, To any](seq S, flattener func(From) STo) Seq[To]

FlatSeq is used to iterate over a two-dimensional sequence in single dimension form, like:

var arrays iter.Seq[[]int]
...
for e := range s.FlatSeq(arrays, slices.Values) {
    ...
}

func NotNil added in v0.0.17

func NotNil[T any](seq Seq[*T]) Seq[*T]

NotNil returns the seq without nil elements.

func Of

func Of[T any](elements ...T) Seq[T]

Of creates an iterator over the elements.

func OfIndexed added in v0.0.15

func OfIndexed[T any](amount int, getAt func(int) T) Seq[T]

OfIndexed builds a Seq 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 OfNext added in v0.0.15

func OfNext[T any](hasNext func() bool, pushNext func(*T)) Seq[T]

OfNext builds an iterator by iterating elements of a source. The hasNext specifies a predicate that tests existing of a next element in the source. The pushNext copy the element to the next pointer.

func OfNextGet added in v0.0.15

func OfNextGet[T any](hasNext func() bool, getNext func() T) Seq[T]

OfNextGet builds an iterator by iterating elements of a source. The hasNext specifies a predicate that tests existing of a next element in the source. The getNext extracts the element.

func OfSourceNext added in v0.0.15

func OfSourceNext[S, T any](source S, hasNext func(S) bool, pushNext func(S, *T)) Seq[T]

OfSourceNext builds an iterator by iterating elements of the source. The hasNext specifies a predicate that tests existing of a next element in the source. The pushNext copy the element to the next pointer.

func OfSourceNextGet added in v0.0.15

func OfSourceNextGet[S, T any](source S, hasNext func(S) bool, getNext func(S) T) Seq[T]

OfSourceNextGet builds an iterator by iterating elements of the source. The hasNext specifies a predicate that tests existing of a next element in the source. The getNext extracts the element.

func Range added in v0.0.15

func Range[T constraints.Integer | rune](from T, toExclusive T) Seq[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[T]

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

func Series added in v0.0.15

func Series[T any](first T, next func(T) (T, bool)) Seq[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 ~seq[T], T any](n int, seq S) Seq[T]

Skip returns the seq without first n elements.

func SkipWhile added in v0.0.16

func SkipWhile[S ~seq[T], T any](seq S, filter func(T) bool) Seq[T]

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

func Top added in v0.0.15

func Top[S ~seq[T], T any](n int, seq S) Seq[T]

Top returns a sequence of top n elements.

func Union added in v0.0.15

func Union[S ~seq[T], T any](seq ...S) Seq[T]

Union combines several sequences into one.

func While added in v0.0.16

func While[S ~seq[T], T any](seq S, filter func(T) bool) Seq[T]

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

func (Seq[T]) Accum added in v0.0.17

func (s Seq[T]) Accum(first T, merge func(T, T) T) T

Accum accumulates a value by using the 'first' argument to initialize the accumulator and sequentially applying the 'merge' functon to the accumulator and each element of the 'seq' sequence.

func (Seq[T]) Accumm added in v0.0.17

func (s Seq[T]) Accumm(first T, merge func(T, T) (T, error)) (T, error)

Accumm accumulates a value by using the 'first' argument to initialize the accumulator and sequentially applying the 'merge' functon to the accumulator and each element of the 'seq' sequence.

func (Seq[T]) Append added in v0.0.17

func (s Seq[T]) Append(out []T) []T

Append collects the elements of the 'seq' sequence into the specified 'out' slice.

func (Seq[T]) Conv added in v0.0.17

func (s Seq[T]) Conv(converter func(T) (T, error)) SeqE[T]

Conv creates an errorable seq that applies the 'converter' function to the iterable elements.

func (Seq[T]) Convert added in v0.0.17

func (s Seq[T]) Convert(converter func(t T) T) Seq[T]

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

func (Seq[T]) Filt added in v0.0.17

func (s Seq[T]) Filt(filter func(s T) (bool, error)) SeqE[T]

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

func (Seq[T]) Filter added in v0.0.17

func (s Seq[T]) Filter(filter func(s T) bool) Seq[T]

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

func (Seq[T]) First added in v0.0.17

func (s Seq[T]) First(predicate func(T) bool) (v T, ok bool)

First returns the first element that satisfies the condition.

func (Seq[T]) Firstt added in v0.0.17

func (s Seq[T]) Firstt(predicate func(T) (bool, error)) (v T, ok bool, err error)

Firstt returns the first element that satisfies the condition.

func (Seq[T]) ForEach added in v0.0.17

func (s Seq[T]) ForEach(f func(T))

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

func (Seq[T]) HasAny added in v0.0.17

func (s Seq[T]) HasAny(predicate func(T) bool) bool

HasAny checks whether the seq contains an element that satisfies the condition.

func (Seq[T]) Head added in v0.0.17

func (s Seq[T]) Head() (v T, ok bool)

Head returns the first element.

func (Seq[T]) Reduce added in v0.0.17

func (s Seq[T]) Reduce(merge func(a T, b T) T) T

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

func (Seq[T]) ReduceOK added in v0.0.17

func (s Seq[T]) ReduceOK(merge func(T, T) 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 (Seq[T]) Reducee added in v0.0.17

func (s Seq[T]) Reducee(merge func(T, T) (T, error)) (T, error)

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

func (Seq[T]) ReduceeOK added in v0.0.17

func (s Seq[T]) ReduceeOK(merge func(T, T) (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 (Seq[T]) Skip added in v0.0.17

func (s Seq[T]) Skip(n int) Seq[T]

Skip returns the seq without first n elements.

func (Seq[T]) SkipWhile added in v0.0.17

func (s Seq[T]) SkipWhile(filter func(T) bool) Seq[T]

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

func (Seq[T]) Slice added in v0.0.17

func (s Seq[T]) Slice() []T

Slice collects the elements of the 'seq' sequence into a new slice.

func (Seq[T]) Top added in v0.0.17

func (s Seq[T]) Top(n int) Seq[T]

Top returns a sequence of top n elements.

func (Seq[T]) Union added in v0.0.17

func (s Seq[T]) Union(seqences ...seq[T]) Seq[T]

Union combines several sequences into one.

func (Seq[T]) While added in v0.0.17

func (s Seq[T]) While(filter func(T) bool) Seq[T]

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

type Seq2 added in v0.0.15

type Seq2[K, V any] seq2[K, V]

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

func KeyValues[S ~seq[T], T, K, V any](seq S, keyExtractor func(T) K, valsExtractor func(T) []V) Seq2[K, V]

KeyValues converts the seq iterator to a key/value pairs iterator by applying the key, values extractors to each iterable element.

func Of2 added in v0.0.17

func Of2[T any](elements ...T) Seq2[int, T]

Of2 creates an index/value pairs iterator over the elements.

func ToKV added in v0.0.17

func ToKV[S ~seq[T], T, K, V any](seq S, keyExtractor func(T) K, valExtractor func(T) V) Seq2[K, V]

ToKV converts the seq iterator to a key/value pairs iterator by applying the key, value extractors to each iterable element.

func ToSeq2

func ToSeq2[S ~seq[T], T, K, V any](seq S, converter func(T) (K, V)) Seq2[K, V]

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

func (Seq2[K, V]) Conv added in v0.0.17

func (s Seq2[K, V]) Conv(converter func(K, V) (K, V, error)) SeqE[c.KV[K, V]]

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

func (Seq2[K, V]) ConvKey added in v0.0.17

func (s Seq2[K, V]) ConvKey(converter func(K) (K, error)) SeqE[c.KV[K, V]]

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

func (Seq2[K, V]) ConvValue added in v0.0.17

func (s Seq2[K, V]) ConvValue(converter func(V) (V, error)) SeqE[c.KV[K, V]]

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

func (Seq2[K, V]) Convert added in v0.0.17

func (s Seq2[K, V]) Convert(converter func(K, V) (K, V)) Seq2[K, V]

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

func (Seq2[K, V]) ConvertKey added in v0.0.17

func (s Seq2[K, V]) ConvertKey(converter func(K) K) Seq2[K, V]

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

func (Seq2[K, V]) ConvertValue added in v0.0.17

func (s Seq2[K, V]) ConvertValue(converter func(V) V) Seq2[K, V]

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

func (Seq2[K, V]) Filt added in v0.0.17

func (s Seq2[K, V]) Filt(filter func(K, V) (bool, error)) 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 (Seq2[K, V]) Filter added in v0.0.17

func (s Seq2[K, V]) Filter(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 (Seq2[K, V]) FilterKey added in v0.0.17

func (s Seq2[K, V]) FilterKey(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 (Seq2[K, V]) FilterValue added in v0.0.17

func (s Seq2[K, V]) FilterValue(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 (Seq2[K, V]) First added in v0.0.17

func (s Seq2[K, V]) First(condition func(K, V) bool) (K, V, bool)

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

func (Seq2[K, V]) Firstt added in v0.0.17

func (s Seq2[K, V]) Firstt(condition func(K, V) (bool, error)) (K, V, bool, error)

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

func (Seq2[K, V]) HasAny added in v0.0.17

func (s Seq2[K, V]) HasAny(condition func(K, V) bool) bool

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

func (Seq2[K, V]) Head added in v0.0.17

func (s Seq2[K, V]) Head() (K, V, bool)

Head returns the first key\value pair.

func (Seq2[K, V]) Keys added in v0.0.17

func (s Seq2[K, V]) Keys() Seq[K]

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

func (Seq2[K, V]) TrackEach added in v0.0.17

func (s Seq2[K, V]) TrackEach(consumer func(K, V))

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

func (Seq2[K, V]) Union added in v0.0.17

func (s Seq2[K, V]) Union(seqences ...seq2[K, V]) Seq2[K, V]

Union combines several sequences into one.

func (Seq2[K, V]) Values added in v0.0.17

func (s Seq2[K, V]) Values() Seq[V]

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

type SeqE added in v0.0.15

type SeqE[T any] seqE[T]

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 ~seq[From], From, To any](seq S, converter func(From) (To, error)) SeqE[To]

Conv creates an errorable seq that applies the 'converter' function to the iterable elements. The error should be checked at every iteration step, like:

var integers iter.Seq[int]
...
for s, err := range seq.Conv(integers,  strconv.Itoa) {
    if err != nil {
        break
    }
    ...
}

func ConvOK added in v0.0.15

func ConvOK[S ~seq[From], From, To any](seq S, converter func(from From) (To, bool, error)) SeqE[To]

ConvOK creates a iterator that applies the 'converter' function to each iterable element. The converter may returns a value or ok=false to exclude the value from iteration. It may also return an error to abort the iteration.

func Filt

func Filt[S ~seq[T], T any](seq S, filter func(T) (bool, error)) SeqE[T]

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

func Flatt added in v0.0.15

func Flatt[S ~seq[From], STo ~[]To, From any, To any](seq S, flattener func(From) (STo, error)) SeqE[To]

Flatt is used to iterate over a two-dimensional sequence in single dimension form, like:

var (
	input     iter.Seq[[]string]
	flattener func([]string) ([]int, error)
	out       seq.SeqE[int]

)

flattener = convertEveryBy(strconv.Atoi)
out = seq.Flatt(input, flattener)
for i, err := range out {
	if err != nil {
		panic(err)
	}
	...
}

func FlattSeq added in v0.0.15

func FlattSeq[S ~seq[From], STo ~seqE[To], From any, To any](seq S, flattener func(From) STo) SeqE[To]

FlattSeq is used to iterate over a two-dimensional sequence in single dimension form, like:

var (
	input     iter.Seq[[]string]
	flattener func([]string) seq.SeqE[int]
	out       seq.SeqE[int]

)

flattener = convertEveryBy(strconv.Atoi)
out = seq.Flatt(input, flattener)
for i, err := range out {
	if err != nil {
		panic(err)
	}
	...
}

func (SeqE[T]) Accum added in v0.0.17

func (s SeqE[T]) Accum(first T, merge func(T, T) T) (T, error)

Accum accumulates a value by using the 'first' argument to initialize the accumulator and sequentially applying the 'merge' functon to the accumulator and each element of the 'seq' sequence.

func (SeqE[T]) Accumm added in v0.0.17

func (s SeqE[T]) Accumm(first T, merge func(T, T) (T, error)) (T, error)

Accumm accumulates a value by using the 'first' argument to initialize the accumulator and sequentially applying the 'merge' functon to the accumulator and each element of the 'seq' sequence.

func (SeqE[T]) Append added in v0.0.17

func (s SeqE[T]) Append(out []T) ([]T, error)

Append collects the elements of the 'seq' sequence into the specified 'out' slice.

func (SeqE[T]) Conv added in v0.0.17

func (s SeqE[T]) Conv(converter func(T) (T, error)) SeqE[T]

Conv creates an errorable seq that applies the 'converter' function to the collection elements.

func (SeqE[T]) Convert added in v0.0.17

func (s SeqE[T]) Convert(converter func(t T) T) SeqE[T]

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

func (SeqE[T]) Filt added in v0.0.17

func (s SeqE[T]) Filt(filter func(s T) (bool, error)) SeqE[T]

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

func (SeqE[T]) Filter added in v0.0.17

func (s SeqE[T]) Filter(filter func(s T) bool) SeqE[T]

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

func (SeqE[T]) First added in v0.0.17

func (s SeqE[T]) First(predicate func(T) bool) (T, bool, error)

First returns the first element that satisfies the condition.

func (SeqE[T]) Firstt added in v0.0.17

func (s SeqE[T]) Firstt(predicate func(T) (bool, error)) (T, bool, error)

Firstt returns the first element that satisfies the condition.

func (SeqE[T]) ForEach added in v0.0.17

func (s SeqE[T]) ForEach(f func(T)) error

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

func (SeqE[T]) HasAny added in v0.0.17

func (s SeqE[T]) HasAny(predicate func(T) bool) (bool, error)

HasAny checks whether the seq contains an element that satisfies the condition.

func (SeqE[T]) Head added in v0.0.17

func (s SeqE[T]) Head() (T, bool, error)

Head returns the first element.

func (SeqE[T]) Reduce added in v0.0.17

func (s SeqE[T]) Reduce(merge func(a T, b T) T) (T, error)

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

func (SeqE[T]) ReduceOK added in v0.0.17

func (s SeqE[T]) ReduceOK(merge func(T, T) T) (result T, ok bool, err error)

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 (SeqE[T]) Reducee added in v0.0.17

func (s SeqE[T]) Reducee(merge func(T, T) (T, error)) (T, error)

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

func (SeqE[T]) ReduceeOK added in v0.0.17

func (s SeqE[T]) ReduceeOK(merge func(T, T) (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 (SeqE[T]) Skip added in v0.0.17

func (s SeqE[T]) Skip(n int) SeqE[T]

Skip returns the seq without first n elements.

func (SeqE[T]) SkipWhile added in v0.0.17

func (s SeqE[T]) SkipWhile(filter func(T) bool) SeqE[T]

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

func (SeqE[T]) Slice added in v0.0.17

func (s SeqE[T]) Slice() ([]T, error)

Slice collects the elements of the 'seq' sequence into a new slice.

func (SeqE[T]) Top added in v0.0.17

func (s SeqE[T]) Top(n int) SeqE[T]

Top returns a sequence of top n elements.

func (SeqE[T]) Union added in v0.0.17

func (s SeqE[T]) Union(seqences ...seqE[T]) SeqE[T]

Union combines several sequences into one.

func (SeqE[T]) While added in v0.0.17

func (s SeqE[T]) While(filter func(T) bool) SeqE[T]

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

Jump to

Keyboard shortcuts

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