loop

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: MIT Imports: 12 Imported by: 3

Documentation

Overview

Package loop provides helpers for loop operation and iterator implementations

Index

Constants

This section is empty.

Variables

View Source
var ErrBreak = c.ErrBreak

ErrBreak is the 'break' statement of the For, Track methods

Functions

func Append

func Append[T any, TS ~[]T](next func() (T, bool), out TS) TS

Append collects the elements retrieved by the 'next' function into the specified 'out' slice

func Contains

func Contains[T comparable](next func() (T, bool), example T) bool

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

func Conv added in v0.0.8

func Conv[From, To any](next func() (From, bool), converter func(From) (To, error)) loop.ConvertIter[From, To]

Conv instantiates an iterator that converts elements with a converter and returns them.

func ConvAndReduce added in v0.0.8

func ConvAndReduce[From, To any](next func() (From, bool), converter func(From) (To, error), merger func(To, To) To) (out To, err error)

ConvAndReduce converts each elements and merges them into one

func ConvCheck added in v0.0.8

func ConvCheck[From, To any](next func() (From, bool), converter func(from From) (To, bool, error)) loop.ConvertCheckIter[From, To]

ConvCheck is similar to ConvertFit, but it checks and transforms elements together

func ConvertAndReduce added in v0.0.8

func ConvertAndReduce[From, To any](next func() (From, bool), converter func(From) To, merger func(To, To) To) (out To)

ConvertAndReduce converts each elements and merges them into one

func Filt added in v0.0.8

func Filt[T any](next func() (T, bool), filter func(T) (bool, error)) loop.FiltIter[T]

Filt creates an iterator that checks elements by the 'filter' function and returns successful ones.

func First

func First[T any](next func() (T, bool), predicate func(T) bool) (v T, ok bool)

First returns the first element that satisfies the condition of the 'predicate' function

func FitAndConv added in v0.0.8

func FitAndConv[From, To any](next func() (From, bool), filter func(From) (bool, error), converter func(From) (To, error)) loop.ConvertFitIter[From, To]

FitAndConv returns a stream that filters source elements and converts them

func FitAndFlat added in v0.0.8

func FitAndFlat[From, To any](next func() (From, bool), filter func(From) (bool, error), flattener func(From) ([]To, error)) loop.FlattenFitIter[From, To]

FitAndFlat filters source elements and extracts slices of 'To' by the 'flattener' function

func FitFlatFit added in v0.0.8

func FitFlatFit[From, To any](next func() (From, bool), filterFrom func(From) (bool, error), flattener func(From) ([]To, error), filterTo func(To) (bool, error)) loop.FlattenFitIter[From, To]

FitFlatFit filters source elements, extracts slices of 'To' by the 'flattener' function and filters extracted elements

func Flat added in v0.0.8

func Flat[From, To any](next func() (From, bool), flattener func(From) ([]To, error)) loop.FlatIter[From, To]

Flat instantiates an iterator that extracts slices of 'To' by a flattener from elements of 'From' and flattens as one iterable collection of 'To' elements.

func FlatAndFit added in v0.0.8

func FlatAndFit[From, To any](next func() (From, bool, error), flattener func(From) ([]To, error), filterTo func(To) (bool, error)) loop.FlattenFitIter[From, To]

FlatAndFit extracts slices of 'To' by the 'flattener' function and filters extracted elements

func FlatKeys added in v0.0.8

func FlatKeys[T, K, V any](next func() (T, bool), keysExtractor func(T) ([]K, error), valExtractor func(T) (V, error)) *loop.MultipleKeyValuer[T, K, V]

FlatKeys transforms iterable elements to key/value iterator based on applying key, value extractor to the elements

func FlatValues added in v0.0.8

func FlatValues[T, K, V any](next func() (T, bool), keyExtractor func(T) (K, error), valsExtractor func(T) ([]V, error)) *loop.MultipleKeyValuer[T, K, V]

FlatValues transforms iterable elements to key/value iterator based on applying key, value extractor to the elements

func For

func For[T any](next func() (T, bool), walker func(T) error) error

For applies the 'walker' function for the elements retrieved by the 'next' function. Return the c.ErrBreak to stop

func ForEach

func ForEach[T any](next func() (T, bool), walker func(T))

ForEach applies the 'walker' function to the elements retrieved by the 'next' function

func ForEachFiltered

func ForEachFiltered[T any](next func() (T, bool), walker func(T), predicate func(T) bool)

ForEachFiltered applies the 'walker' function to the elements retrieved by the 'next' function that satisfy the 'predicate' function condition

func Group

func Group[T any, K comparable, V any](next func() (T, bool), keyExtractor func(T) K, valExtractor func(T) V) map[K][]V

Group converts elements retrieved by the 'next' function into a map, extracting a key for each element applying the converter 'keyExtractor'. The keyExtractor converts an element to a key. The valExtractor converts an element to an value.

func GroupByMultiple

func GroupByMultiple[T any, K comparable, V any](next func() (T, bool), keysExtractor func(T) []K, valsExtractor func(T) []V) map[K][]V

GroupByMultiple converts elements retrieved by the 'next' function into a map, extracting multiple keys, values per each element applying the 'keysExtractor' and 'valsExtractor' functions. The keysExtractor retrieves one or more keys per element. The valsExtractor retrieves one or more values per element.

func GroupByMultipleKeys

func GroupByMultipleKeys[T any, K comparable, V any](next func() (T, bool), keysExtractor func(T) []K, valExtractor func(T) V) map[K][]V

GroupByMultipleKeys converts elements retrieved by the 'next' function into a map, extracting multiple keys, one value per each element applying the 'keysExtractor' and 'valExtractor' functions. The keysExtractor retrieves one or more keys per element. The valExtractor converts an element to a value.

func GroupByMultipleValues

func GroupByMultipleValues[T any, K comparable, V any](next func() (T, bool), keyExtractor func(T) K, valsExtractor func(T) []V) map[K][]V

GroupByMultipleValues converts elements retrieved by the 'next' function into a map, extracting one key, multiple values per each element applying the 'keyExtractor' and 'valsExtractor' functions. The keyExtractor converts an element to a key. The valsExtractor retrieves one or more values per element.

func HasAny

func HasAny[T any](next func() (T, bool), predicate func(T) bool) bool

HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful

func New

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

New is the main loop constructor

func Of

func Of[T any](elements ...T) func() (e T, ok bool)

Of wrap the elements by loop function

func OfIndexed added in v0.0.8

func OfIndexed[T any](len int, next func(int) T) func() (T, bool)

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

func Range added in v0.0.8

func Range[T constraints.Integer](from T, toExclusive T) func() (T, bool)

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

func RangeClosed added in v0.0.8

func RangeClosed[T constraints.Integer](from T, toInclusive T) func() (T, bool)

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

func Reduce

func Reduce[T any](next func() (T, bool), merger func(T, T) T) (result T)

Reduce reduces the elements retrieved by the 'next' function into an one using the 'merger' function

func Slice

func Slice[T any](next func() (T, bool)) []T

Slice collects the elements retrieved by the 'next' function into a new slice

func SliceCap

func SliceCap[T any](next func() (T, bool), cap int) (out []T)

SliceCap collects the elements retrieved by the 'next' function into a new slice with predefined capacity

func Sum

func Sum[T c.Summable](next func() (T, bool)) T

Sum returns the sum of all elements

func ToMapResolv

func ToMapResolv[T any, K comparable, V, VR any](next func() (T, bool), keyExtractor func(T) K, valExtractor func(T) V, resolver func(bool, K, VR, V) VR) map[K]VR

ToMapResolv collects key\value elements to a map by iterating over the elements with resolving of duplicated key values

func Track

func Track[I, T any](next func() (I, T, bool), tracker func(I, T) error) error

Track applies the 'tracker' function to position/element pairs retrieved by the 'next' function. Return the c.ErrBreak to stop tracking..

func TrackEach

func TrackEach[I, T any](next func() (I, T, bool), tracker func(I, T))

TrackEach applies the 'tracker' function to position/element pairs retrieved by the 'next' function

Types

type ConvertCheckIter

type ConvertCheckIter[From, To any] struct {
	// contains filtered or unexported fields
}

ConvertCheckIter converts and filters elements at the same time

func ConvertCheck

func ConvertCheck[From, To any](next func() (From, bool), converter func(from From) (To, bool)) ConvertCheckIter[From, To]

ConvertCheck is similar to ConvertFit, but it checks and transforms elements together

func GetValues

func GetValues[T any](next func() (*T, bool)) ConvertCheckIter[*T, T]

GetValues creates an iterator that transform only not nil pointers to the values referenced referenced by those pointers. Nil pointers are ignored.

func (ConvertCheckIter[From, To]) Next

func (c ConvertCheckIter[From, To]) Next() (t To, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

type ConvertFitIter

type ConvertFitIter[From, To any] struct {
	// contains filtered or unexported fields
}

ConvertFitIter iterator implementation that retrieves an element by the 'next' function, converts by the 'converter' and addition checks by the 'filter'. If the filter returns true then the converted element is returned as next.

func ConvertAndFilter

func ConvertAndFilter[From, To any](next func() (From, bool), converter func(From) To, filter func(To) bool) ConvertFitIter[From, To]

ConvertAndFilter additionally filters 'To' elements

func FilterAndConvert

func FilterAndConvert[From, To any](next func() (From, bool), filter func(From) bool, converter func(From) To) ConvertFitIter[From, To]

FilterAndConvert returns a stream that filters source elements and converts them

func FilterConvertFilter

func FilterConvertFilter[From, To any](next func() (From, bool), filter func(From) bool, converter func(From) To, filterTo func(To) bool) ConvertFitIter[From, To]

FilterConvertFilter filters source, converts, and filters converted elements

func (ConvertFitIter[From, To]) For

func (c ConvertFitIter[From, To]) For(walker func(element To) error) error

For takes elements retrieved by the iterator. Can be interrupt by returning ErrBreak

func (ConvertFitIter[From, To]) ForEach

func (c ConvertFitIter[From, To]) ForEach(walker func(element To))

ForEach FlatIter all elements retrieved by the iterator

func (ConvertFitIter[From, To]) Next

func (c ConvertFitIter[From, To]) Next() (t To, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

type ConvertIter

type ConvertIter[From, To any] struct {
	// contains filtered or unexported fields
}

ConvertIter iterator implementation that retrieves an element by the 'next' function and converts by the 'converter'

func Convert

func Convert[From, To any](next func() (From, bool), converter func(From) To) ConvertIter[From, To]

Convert instantiates an iterator that converts elements with a converter and returns them.

func ToValues

func ToValues[T any](next func() (*T, bool)) ConvertIter[*T, T]

ToValues creates an iterator that transform pointers to the values referenced referenced by those pointers. Nil pointers are transformet to zero values.

func (ConvertIter[From, To]) For

func (c ConvertIter[From, To]) For(walker func(element To) error) error

For takes elements retrieved by the iterator. Can be interrupt by returning ErrBreak

func (ConvertIter[From, To]) ForEach

func (c ConvertIter[From, To]) ForEach(walker func(element To))

ForEach FlatIter all elements retrieved by the iterator

func (ConvertIter[From, To]) Next

func (c ConvertIter[From, To]) Next() (t To, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

type FitIter

type FitIter[T any] struct {
	// contains filtered or unexported fields
}

FitIter is the Iterator wrapper that provides filtering of elements by a Predicate.

func Filter

func Filter[T any](next func() (T, bool), filter func(T) bool) FitIter[T]

Filter creates an iterator that checks elements by the 'filter' function and returns successful ones.

func NotNil

func NotNil[T any](next func() (*T, bool)) FitIter[*T]

NotNil creates an iterator that filters nullable elements

func (FitIter[T]) For

func (f FitIter[T]) For(walker func(element T) error) error

For takes elements retrieved by the iterator. Can be interrupt by returning ErrBreak

func (FitIter[T]) ForEach

func (f FitIter[T]) ForEach(walker func(element T))

ForEach FlatIter all elements retrieved by the iterator

func (FitIter[T]) Next

func (f FitIter[T]) Next() (element T, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

type FlatIter

type FlatIter[From, To any] struct {
	// contains filtered or unexported fields
}

FlatIter is the Iterator wrapper that converts an element to a slice and iterates over the elements of that slice. For example, FlatIter can be used to iterate over all the elements of a multi-dimensional array as if it were a one-dimensional array ([][]int -> []int).

func Flatt

func Flatt[From, To any](next func() (From, bool), flattener func(From) []To) FlatIter[From, To]

Flatt instantiates an iterator that extracts slices of 'To' by a flattener from elements of 'From' and flattens as one iterable collection of 'To' elements.

func (*FlatIter[From, To]) For

func (i *FlatIter[From, To]) For(walker func(element To) error) error

For takes elements retrieved by the iterator. Can be interrupt by returning ErrBreak

func (*FlatIter[From, To]) ForEach

func (i *FlatIter[From, To]) ForEach(walker func(element To))

ForEach FlatIter all elements retrieved by the iterator

func (*FlatIter[From, To]) Next

func (i *FlatIter[From, To]) Next() (To, bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

type FlattenFitIter

type FlattenFitIter[From, To any] struct {
	// contains filtered or unexported fields
}

FlattenFitIter is the Iterator wrapper that converts an element to a slice with addition filtering of the element by a Predicate and iterates over the slice.

func FilterAndFlatt

func FilterAndFlatt[From, To any](next func() (From, bool), filter func(From) bool, flattener func(From) []To) FlattenFitIter[From, To]

FilterAndFlatt filters source elements and extracts slices of 'To' by the 'flattener' function

func FilterFlattFilter

func FilterFlattFilter[From, To any](next func() (From, bool), filterFrom func(From) bool, flattener func(From) []To, filterTo func(To) bool) FlattenFitIter[From, To]

FilterFlattFilter filters source elements, extracts slices of 'To' by the 'flattener' function and filters extracted elements

func FlattAndFilter

func FlattAndFilter[From, To any](next func() (From, bool), flattener func(From) []To, filterTo func(To) bool) FlattenFitIter[From, To]

FlattAndFilter extracts slices of 'To' by the 'flattener' function and filters extracted elements

func (*FlattenFitIter[From, To]) For

func (i *FlattenFitIter[From, To]) For(walker func(element To) error) error

For takes elements retrieved by the iterator. Can be interrupt by returning ErrBreak

func (*FlattenFitIter[From, To]) ForEach

func (i *FlattenFitIter[From, To]) ForEach(walker func(element To))

ForEach FlatIter all elements retrieved by the iterator

func (*FlattenFitIter[From, To]) Next

func (i *FlattenFitIter[From, To]) Next() (t To, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

type KeyValuer

type KeyValuer[T, K, V any] struct {
	// contains filtered or unexported fields
}

KeyValuer is the Iterator wrapper that converts an element to a key\value pair and iterates over these pairs

func NewKeyValuer

func NewKeyValuer[T any, K, V any](next func() (T, bool), keyExtractor func(T) K, valsExtractor func(T) V) KeyValuer[T, K, V]

NewKeyValuer creates instance of the KeyValuer

func ToKV

func ToKV[T any, K comparable, V any](next func() (T, bool), keyExtractor func(T) K, valExtractor func(T) V) KeyValuer[T, K, V]

ToKV transforms iterable elements to key/value iterator based on applying key, value extractors to the elements

func (KeyValuer[T, K, V]) Next

func (kv KeyValuer[T, K, V]) Next() (key K, value V, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

func (KeyValuer[T, K, V]) Track

func (kv KeyValuer[T, K, V]) Track(traker func(key K, value V) error) error

Track takes key, value pairs retrieved by the iterator. Can be interrupt by returning ErrBreak

func (KeyValuer[T, K, V]) TrackEach

func (kv KeyValuer[T, K, V]) TrackEach(traker func(key K, value V))

TrackEach takes all key, value pairs retrieved by the iterator

type Looper

type Looper[T any, I interface{ Next() (T, bool) }] interface {
	Loop() I
}

Looper provides an iterable loop function

type MultipleKeyValuer

type MultipleKeyValuer[T, K, V any] struct {
	// contains filtered or unexported fields
}

MultipleKeyValuer is the Iterator wrapper that converts an element to a key\value pair and iterates over these pairs

func FlattKeys

func FlattKeys[T, K, V any](next func() (T, bool), keysExtractor func(T) []K, valExtractor func(T) V) *MultipleKeyValuer[T, K, V]

FlattKeys transforms iterable elements to key/value iterator based on applying key, value extractor to the elements

func FlattValues

func FlattValues[T, K, V any](next func() (T, bool), keyExtractor func(T) K, valsExtractor func(T) []V) *MultipleKeyValuer[T, K, V]

FlattValues transforms iterable elements to key/value iterator based on applying key, value extractor to the elements

func NewMultipleKeyValuer

func NewMultipleKeyValuer[T any, K, V any](next func() (T, bool), keysExtractor func(T) []K, valsExtractor func(T) []V) MultipleKeyValuer[T, K, V]

NewMultipleKeyValuer creates instance of the MultipleKeyValuer

func ToKVs

func ToKVs[T, K, V any](next func() (T, bool), keysExtractor func(T) []K, valsExtractor func(T) []V) *MultipleKeyValuer[T, K, V]

ToKVs transforms iterable elements to key/value iterator based on applying key, value extractor to the elements

func (*MultipleKeyValuer[T, K, V]) Next

func (kv *MultipleKeyValuer[T, K, V]) Next() (key K, value V, ok bool)

Next returns the next element. The ok result indicates whether the element was returned by the iterator. If ok == false, then the iteration must be completed.

func (*MultipleKeyValuer[T, K, V]) Track

func (kv *MultipleKeyValuer[T, K, V]) Track(traker func(key K, value V) error) error

Track takes key, value pairs retrieved by the iterator. Can be interrupt by returning ErrBreak

func (*MultipleKeyValuer[T, K, V]) TrackEach

func (kv *MultipleKeyValuer[T, K, V]) TrackEach(traker func(key K, value V))

TrackEach takes all key, value pairs retrieved by the iterator

Directories

Path Synopsis
Package conv provides loop converation helpers
Package conv provides loop converation helpers
Package convert provides loop converation helpers
Package convert provides loop converation helpers
Package filter provides aliases for loop filtering helpers
Package filter provides aliases for loop filtering helpers
Package first provides short aliases for loop functions for retrieving a first element
Package first provides short aliases for loop functions for retrieving a first element
Package flatt provides short aliases for loop functions
Package flatt provides short aliases for loop functions
Package group provides short aliases for functions that are used to group elements retrieved by a loop
Package group provides short aliases for functions that are used to group elements retrieved by a loop
Package range_ provides alias for the slice.Range function
Package range_ provides alias for the slice.Range function
Package sum provides sum.Of alias
Package sum provides sum.Of alias

Jump to

Keyboard shortcuts

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