seqe

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: 5 Imported by: 1

Documentation

Overview

Package seqe provides convering, filtering, and reducing operations for the seq.SeqE interface.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accum

func Accum[T any, S ~SeqE[T]](first T, seq S, 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 Accumm

func Accumm[T any, S ~SeqE[T]](first T, seq S, 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 Append

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

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

func Contains

func Contains[S ~SeqE[T], T comparable](seq S, example T) (contains bool, err error)

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

func Conv

func Conv[S ~SeqE[From], From, To any](seq S, converter func(From) (To, error)) seq.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.Seq2[int, error]
...
for s, err := range seqe.Conv(integers, strconv.Itoa) {
    if err != nil {
        break
    }
    ...
}

func ConvOK

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

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

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

func ConvertNilSafe added in v0.0.17

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

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

func ConvertOK

func ConvertOK[S ~SeqE[From], From, To any](seq S, converter func(from From) (To, bool)) seq.SeqE[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 Filt

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

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

func Filter

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

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

func First

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

First returns the first element that satisfies the condition.

func Firstt

func Firstt[S ~SeqE[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 Flat

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

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

var arrays seq.SeqE[[]int]
...
for e, err := range seqe.Flat(arrays, as.Is) {
	if err != nil {
		panic(err)
	}
}

func FlatSeq

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

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

var arrays seq.SeqE[[]int]
...
for e, err := range seqe.FlatSeq(arrays, slices.Values) {
	if err != nil {
		panic(err)
	}
}

func Flatt

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

func FlattSeq[S ~SeqE[From], STo ~SeqE[To], From any, To any](seq S, flattener func(From) STo) seq.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 ForEach

func ForEach[T any](seq SeqE[T], consumer func(T)) error

ForEach applies the 'consumer' function to the seq elements

func Group

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

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 ~SeqE[T], T any](seq S, condition func(T) bool) (bool, error)

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

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

Head returns the first element.

func NotNil added in v0.0.17

func NotNil[T any](seq SeqE[*T]) seq.SeqE[*T]

NotNil returns the seq without nil elements.

func OfIndexed

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

OfIndexed builds a SeqE 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

func OfNext[T any](hasNext func() bool, pushNext func(*T) error) seq.SeqE[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

func OfNextGet[T any](hasNext func() bool, getNext func() (T, error)) seq.SeqE[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

func OfSourceNext[S, T any](source S, hasNext func(S) bool, pushNext func(S, *T) error) seq.SeqE[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

func OfSourceNextGet[S, T any](source S, hasNext func(S) bool, getNext func(S) (T, error)) seq.SeqE[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 Reduce

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

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

func ReduceOK

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

func Reducee[S ~SeqE[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 ~SeqE[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 Skip

func Skip[S ~SeqE[T], T any](n int, seq S) seq.SeqE[T]

Skip returns the seq without first n elements.

func SkipWhile added in v0.0.17

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

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

func Slice

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

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

func SliceCap

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

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

func Sum

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

Sum returns the sum of all elements.

func Top

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

Top returns a sequence of top n elements.

func Union

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

Union combines several sequences into one.

func While added in v0.0.17

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

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

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.

type SeqE

type SeqE[T any] = func(func(T, error) bool)

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
    }
    ...
}

Jump to

Keyboard shortcuts

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