loop

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 14 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 Break = c.Break

Break is the 'break' statement of the For, Track methods.

Functions

func All added in v0.0.12

func All[T any](next func() (T, bool), consumer func(T) bool)

All is an adapter for the next function for iterating by `for ... range`. Supported since go 1.22 with GOEXPERIMENT=rangefunc enabled.

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)) breakloop.Loop[To]

Conv creates a loop that applies the 'converter' function to iterable elements.

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)) breakloop.Loop[To]

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

func ConvS added in v0.0.12

func ConvS[FS ~[]From, From, To any](elements FS, converter func(From) (To, error)) breakloop.Loop[To]

ConvS creates a loop that applies the 'converter' function to the 'elements' slice.

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

func ExtraKey[T, K any](next func() (T, bool), keysExtractor func(T) K) kvloop.Loop[K, T]

ExtraKey transforms a loop to the key/value loop based on applying key extractor to the elements

func ExtraKeys added in v0.0.10

func ExtraKeys[T, K any](next func() (T, bool), keysExtractor func(T) []K) kvloop.Loop[K, T]

ExtraKeys transforms a loop to the key/value loop based on applying key extractor to the elements

func ExtraKeyss added in v0.0.10

func ExtraKeyss[T, K any](next func() (T, bool), keyExtractor func(T) (K, error)) breakkvloop.Loop[K, T]

ExtraKeyss transforms a loop to the key/value loop based on applying key extractor to the elements

func ExtraKeyy added in v0.0.10

func ExtraKeyy[T, K any](next func() (T, bool), keyExtractor func(T) (K, error)) breakkvloop.Loop[K, T]

ExtraKeyy transforms a loop to the key/value loop based on applying key extractor to the elements

func ExtraVals added in v0.0.10

func ExtraVals[T, V any](next func() (T, bool), valsExtractor func(T) []V) kvloop.Loop[T, V]

ExtraVals transforms a loop to the key/value loop based on applying values extractor to the elements

func ExtraValss added in v0.0.10

func ExtraValss[T, V any](next func() (T, bool), valsExtractor func(T) ([]V, error)) breakkvloop.Loop[T, V]

ExtraValss transforms a loop to the key/value loop based on applying values extractor to the elements

func ExtraValue added in v0.0.10

func ExtraValue[T, V any](next func() (T, bool), valueExtractor func(T) V) kvloop.Loop[T, V]

ExtraValue transforms a loop to the key/value loop based on applying value extractor to the elements

func ExtraValuee added in v0.0.10

func ExtraValuee[T, V any](next func() (T, bool), valExtractor func(T) (V, error)) breakkvloop.Loop[T, V]

ExtraValuee transforms a loop to the key/value loop based on applying value extractor to the elements

func Filt added in v0.0.8

func Filt[T any](next func() (T, bool), filter func(T) (bool, error)) breakloop.Loop[T]

Filt creates a loop that checks elements by the 'filter' function and returns successful ones.

func FiltAndConv added in v0.0.9

func FiltAndConv[From, To any](next func() (From, bool), filter func(From) (bool, error), converter func(From) (To, error)) breakloop.Loop[To]

FiltAndConv creates a loop that filters source elements and converts them

func FiltAndFlat added in v0.0.9

func FiltAndFlat[From, To any](next func() (From, bool), filter func(From) (bool, error), flattener func(From) ([]To, error)) breakloop.Loop[To]

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

func FiltFlattFilt added in v0.0.9

func FiltFlattFilt[From, To any](next func() (From, bool), filterFrom func(From) (bool, error), flattener func(From) ([]To, error), filterTo func(To) (bool, error)) breakloop.Loop[To]

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

func FiltS added in v0.0.12

func FiltS[TS ~[]T, T any](elements TS, filter func(T) (bool, error)) breakloop.Loop[T]

FiltS creates a loop that checks slice elements by the 'filter' function and returns successful ones.

func First deprecated

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

Deprecated: First is deprecated. Will be replaced by rance-over function iterator. First returns the first element that satisfies the condition of the 'predicate' function

func Firstt added in v0.0.12

func Firstt[T any](next func() (T, bool), predicate func(T) (bool, error)) (v T, ok bool, err error)

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

func FlatAndFilt added in v0.0.9

func FlatAndFilt[From, To any](next func() (From, bool, error), flattener func(From) ([]To, error), filterTo func(To) (bool, error)) breakloop.Loop[To]

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

func Flatt

func Flatt[From, To any](next func() (From, bool), flattener func(From) ([]To, error)) breakloop.Loop[To]

Flatt creates a loop that extracts slices of 'To' by the 'flattener' function from iterable elements of 'From' and flattens as one iterable collection of 'To' elements.

func FlattS added in v0.0.12

func FlattS[FS ~[]From, From, To any](elements FS, flattener func(From) ([]To, error)) breakloop.Loop[To]

FlattS creates a loop that extracts slices of 'To' by the 'flattener' function from the elements of 'From' and flattens as one iterable collection of 'To' elements.

func For

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

For applies the 'consumer' function for the elements retrieved by the 'next' function until the consumer returns the c.Break to stop.

func ForEach

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

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

func ForEachFiltered

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

ForEachFiltered applies the 'consumer' 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 Groupp added in v0.0.10

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

Groupp 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 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 KeyValue added in v0.0.10

func KeyValue[T any, K, V any](next func() (T, bool), keyExtractor func(T) K, valExtractor func(T) V) kvloop.Loop[K, V]

KeyValue transforms a loop to the key/value loop based on applying key, value extractors to the elements

func KeyValuee added in v0.0.10

func KeyValuee[T any, K, V any](next func() (T, bool), keyExtractor func(T) (K, error), valExtractor func(T) (V, error)) breakkvloop.Loop[K, V]

KeyValuee transforms a loop to the key/value loop based on applying key, value extractors to the elements

func KeyValues added in v0.0.10

func KeyValues[T, K, V any](next func() (T, bool), keyExtractor func(T) K, valsExtractor func(T) []V) kvloop.Loop[K, V]

KeyValues transforms a loop to the key/value loop based on applying key, values extractor to the elements

func KeyValuess added in v0.0.10

func KeyValuess[T, K, V any](next func() (T, bool), keyExtractor func(T) (K, error), valsExtractor func(T) ([]V, error)) breakkvloop.Loop[K, V]

KeyValuess transforms a loop to the key/value loop based on applying key, values extractor to the elements

func KeysValue added in v0.0.10

func KeysValue[T, K, V any](next func() (T, bool), keysExtractor func(T) []K, valExtractor func(T) V) kvloop.Loop[K, V]

KeysValue transforms a loop to the key/value loop based on applying keys, value extractor to the elements

func KeysValuee added in v0.0.10

func KeysValuee[T, K, V any](next func() (T, bool), keysExtractor func(T) ([]K, error), valExtractor func(T) (V, error)) breakkvloop.Loop[K, V]

KeysValuee transforms a loop to the key/value loop based on applying keys, value extractor to the elements

func KeysValues added in v0.0.10

func KeysValues[T, K, V any](next func() (T, bool), keysExtractor func(T) []K, valsExtractor func(T) []V) kvloop.Loop[K, V]

KeysValues transforms a loop to the key/value loop based on applying multiple keys, values extractor to the elements

func Reduce

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

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

func Reducee added in v0.0.12

func Reducee[T any](next func() (T, bool), merge func(T, T) (T, error)) (result T, err error)

Reducee reduces the elements retrieved by the 'next' function into an one pair using the 'merge' 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 ToMap added in v0.0.10

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

ToMap collects key\value elements to a map by iterating over the 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 ToMapp added in v0.0.10

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

ToMapp collects key\value elements to a map by iterating over the elements

func Track

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

Track applies the 'consumer' function to position/element pairs retrieved by the 'next' function until the consumer returns the c.Break to stop.tracking.

func TrackEach

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

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

Types

type Loop added in v0.0.12

type Loop[T any] func() (T, bool)

Loop is a function that returns the next element or false if there are no more elements.

func Convert

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

Convert creates a loop that applies the 'converter' function to iterable elements.

func ConvertAndFilter

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

ConvertAndFilter additionally filters 'To' elements

func ConvertCheck

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

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

func ConvertS added in v0.0.12

func ConvertS[FS ~[]From, From, To any](elements FS, converter func(From) To) Loop[To]

ConvertS creates a loop that applies the 'converter' function to the 'elements' slice.

func Crank added in v0.0.12

func Crank[T any](next func() (T, bool)) (n Loop[T], t T, ok bool)

Crank rertieves a next element from the 'next' function, returns the function, element, successfully flag.

func Filter

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

Filter creates a loop that checks elements by the 'filter' function and returns successful ones.

func FilterAndConvert

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

FilterAndConvert creates a loop that filters source elements and converts them

func FilterAndFlat added in v0.0.9

func FilterAndFlat[From, To any](next func() (From, bool), filter func(From) bool, flattener func(From) []To) Loop[To]

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

func FilterConvertFilter

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

FilterConvertFilter filters source, converts, and filters converted elements

func FilterFlatFilter added in v0.0.9

func FilterFlatFilter[From, To any](next func() (From, bool), filterFrom func(From) bool, flattener func(From) []To, filterTo func(To) bool) Loop[To]

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

func FilterS added in v0.0.12

func FilterS[TS ~[]T, T any](elements TS, filter func(T) bool) Loop[T]

FilterS creates a loop that checks slice elements by the 'filter' function and returns successful ones.

func Flat added in v0.0.8

func Flat[From, To any](next func() (From, bool), flattener func(From) []To) Loop[To]

Flat creates a loop that extracts slices of 'To' by the 'flattener' function from iterable elements of 'From' and flattens as one iterable collection of 'To' elements.

func FlatS added in v0.0.12

func FlatS[FS ~[]From, From, To any](elements FS, flattener func(From) []To) Loop[To]

FlatS creates a loop that extracts slices of 'To' by the 'flattener' function from the elements of 'From' and flattens as one iterable collection of 'To' elements.

func FlattAndFilter

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

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

func New

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

New makes a loop from an abstract source

func NoNilPtrVal added in v0.0.10

func NoNilPtrVal[T any](next func() (*T, bool)) Loop[T]

NoNilPtrVal creates a loop that transform only not nil pointers to the values referenced referenced by those pointers. Nil pointers are ignored.

func NotNil

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

NotNil creates a loop that filters nullable elements

func Of

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

Of wrap the elements by loop function.

func OfIndexed added in v0.0.8

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

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

func PtrVal[T any](next func() (*T, bool)) Loop[T]

PtrVal creates a loop that transform pointers to the values referenced by those pointers. Nil pointers are transformet to zero values.

func Range added in v0.0.8

func Range[T constraints.Integer | rune](from T, toExclusive T) Loop[T]

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 | rune](from T, toInclusive T) Loop[T]

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

func S added in v0.0.12

func S[TS ~[]T, T any](elements TS) Loop[T]

S wrap the elements by loop function.

func Sequence added in v0.0.12

func Sequence[T any](first T, next func(T) (T, bool)) Loop[T]

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

func (Loop[T]) All added in v0.0.12

func (next Loop[T]) All(consumer func(T) bool)

All is used to iterate through the loop using `for ... range`. Supported since go 1.22 with GOEXPERIMENT=rangefunc enabled.

func (Loop[T]) Append added in v0.0.12

func (next Loop[T]) Append(out []T) []T

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

func (Loop[T]) Conv added in v0.0.12

func (next Loop[T]) Conv(converter func(T) (T, error)) loop.Loop[T]

Conv creates a loop that applies the 'converter' function to iterable elements.

func (Loop[T]) Convert added in v0.0.12

func (next Loop[T]) Convert(converter func(T) T) Loop[T]

Convert creates a loop that applies the 'converter' function to iterable elements.

func (Loop[T]) Crank added in v0.0.12

func (next Loop[T]) Crank() (Loop[T], T, bool)

Crank rertieves a next element from the 'next' function, returns the function, element, successfully flag.

func (Loop[T]) Filt added in v0.0.12

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

Filt creates a loop that checks elements by the 'filter' function and returns successful ones.

func (Loop[T]) Filter added in v0.0.12

func (next Loop[T]) Filter(filter func(T) bool) Loop[T]

Filter creates a loop that checks elements by the 'filter' function and returns successful ones.

func (Loop[T]) First deprecated added in v0.0.12

func (next Loop[T]) First(predicate func(T) bool) (T, bool)

Deprecated: First is deprecated. Will be replaced by rance-over function iterator. First returns the first element that satisfies the condition of the 'predicate' function

func (Loop[T]) For added in v0.0.12

func (next Loop[T]) For(consumer func(T) error) error

For applies the 'consumer' function for the elements retrieved by the 'next' function until the consumer returns the c.Break to stop.

func (Loop[T]) ForEach added in v0.0.12

func (next Loop[T]) ForEach(consumer func(T))

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

func (Loop[T]) ForEachFiltered added in v0.0.12

func (next Loop[T]) ForEachFiltered(predicate func(T) bool, consumer func(T))

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

func (Loop[T]) HasAny added in v0.0.12

func (next Loop[T]) HasAny(predicate func(T) bool) bool

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

func (Loop[T]) Reduce added in v0.0.12

func (next Loop[T]) Reduce(merge func(T, T) T) T

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

func (Loop[T]) Reducee added in v0.0.12

func (next Loop[T]) Reducee(merge func(T, T) (T, error)) (T, error)

Reducee reduces the elements retrieved by the 'next' function into an one using the 'merge' function

func (Loop[T]) Slice added in v0.0.12

func (next Loop[T]) Slice() []T

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

func (Loop[T]) SliceCap added in v0.0.12

func (next Loop[T]) SliceCap(cap int) []T

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

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 flat provides short aliases for loop functions
Package flat 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