loop

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2024 License: MIT Imports: 11 Imported by: 2

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

func Accum[T any](first T, next func() (T, bool, error), merge func(T, T) T) (accumulator T, err 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 retrieved by the 'next' function.

func Accumm added in v0.0.13

func Accumm[T any](first T, next func() (T, bool, error), 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 retrieved by the 'next' function.

func All added in v0.0.13

func All[T any](next func() (T, bool, error), consumer func(T, error) 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 added in v0.0.8

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

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

func Contains

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

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

func ConvAndReduce added in v0.0.8

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

ConvAndReduce converts each elements and merges them into one

func ConvertAndReduce added in v0.0.8

func ConvertAndReduce[From, To any](next func() (From, bool, error), converter func(From) To, merge func(To, To) To) (out To, err error)

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, error), keysExtractor func(T) K) breakkvloop.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, error), keysExtractor func(T) []K) breakkvloop.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, error), 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, error), 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, error), valsExtractor func(T) []V) breakkvloop.Loop[T, V]

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

func ExtraValss added in v0.0.10

func ExtraValss[T, V any](next func() (T, bool, error), 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, error), valueExtractor func(T) V) breakkvloop.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, error), 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 First

func First[T any](next func() (T, bool, error), predicate func(T) bool) (out T, ok bool, err error)

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

func Firstt added in v0.0.8

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

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

func For

func For[T any](next func() (T, bool, error), 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 ForFiltered

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

ForFiltered 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, error), keyExtractor func(T) K, valExtractor func(T) V) (map[K][]V, error)

Group converts elements retrieved by the 'next' function into a new 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 a value.

func GroupByMultiple

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

GroupByMultiple converts elements retrieved by the 'next' function into a new 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, error), keysExtractor func(T) []K, valExtractor func(T) V) (map[K][]V, error)

GroupByMultipleKeys converts elements retrieved by the 'next' function into a new 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, error), keyExtractor func(T) K, valsExtractor func(T) []V) (map[K][]V, error)

GroupByMultipleValues converts elements retrieved by the 'next' function into a new 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, error), keyExtractor func(T) (K, error), valExtractor func(T) (V, error)) (map[K][]V, error)

Groupp converts elements retrieved by the 'next' function into a new 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 a value.

func HasAny

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

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

func HasAnyy added in v0.0.8

func HasAnyy[T any](next func() (T, bool, error), predicate func(T) (bool, error)) (bool, error)

HasAnyy 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, error), keyExtractor func(T) K, valExtractor func(T) V) breakkvloop.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, error), 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, error), keyExtractor func(T) K, valsExtractor func(T) []V) breakkvloop.Loop[K, V]

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

func KeyValuess added in v0.0.10

func KeyValuess[T, K, V any](next func() (T, bool, error), 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, value extractor to the elements

func KeysValue added in v0.0.10

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

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

func KeysValuee added in v0.0.10

func KeysValuee[T, K, V any](next func() (T, bool, error), 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 key, value extractor to the elements

func KeysValues added in v0.0.10

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

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

func Map added in v0.0.14

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

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

func MapResolvv added in v0.0.14

func MapResolvv[T any, K comparable, V, VR any](
	next func() (T, bool, error), keyExtractor func(T) (K, error), valExtractor func(T) (V, error),
	resolver func(bool, K, VR, V) (VR, error),
) (m map[K]VR, err error)

MapResolvv collects key\value elements into a new map by iterating over the elements with resolving of duplicated key values

func Mapp added in v0.0.14

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

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

func Of

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

Of wrap the elements by loop function.

func Reduce

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

Reduce reduces the elements retrieved by the 'next' function into an one using the 'merge' function. If the 'next' function returns ok=false at the first call, the zero value of 'T' type is returned.

func ReduceOK added in v0.0.13

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

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

func Reducee added in v0.0.8

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

Reducee reduces the elements retrieved by the 'next' function into an one using the 'merge' function. If the 'next' function returns ok=false at the first call, the zero value of 'T' type is returned.

func ReduceeOK added in v0.0.13

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

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

func Slice

func Slice[T any](next func() (T, bool, error)) (out []T, err error)

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

func SliceCap added in v0.0.8

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

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, error)) (T, error)

Sum returns the sum of all elements

func To

func To[T any](next func() (T, bool, error), errConsumer func(error)) func() (T, bool)

To transforms a breakable loop to a simple loop. The errConsumer is a function that is called when an error occurs.

func Track

func Track[I, T any](next func() (I, T, bool, error), 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.

Types

type Loop added in v0.0.12

type Loop[T any] func() (element T, ok bool, err error)

Loop is a function that returns the next element, ok==false if there are no more elements or an error if something is wrong.

func Conv

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

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

func ConvOK added in v0.0.13

func ConvOK[From, To any](next func() (From, bool, error), converter func(from From) (to To, ok bool, err error)) Loop[To]

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

func Convert

func Convert[From, To any](next func() (From, bool, error), 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, error), converter func(From) (To, error), filter func(To) (bool, error)) Loop[To]

ConvertAndFilter additionally filters 'To' elements

func ConvertOK added in v0.0.13

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

ConvertOK creates a loop that applies the 'converter' function to iterable elements. The converter may returns a value or ok=false to exclude the value from the loop.

func Crank added in v0.0.12

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

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

func Filt

func Filt[T any](next func() (T, bool, error), filter func(T) (bool, error)) 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, error), filter func(From) (bool, error), converter func(From) (To, error)) 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, error), filter func(From) (bool, error), flattener func(From) ([]To, error)) 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, error), filterFrom func(From) (bool, error), flattener func(From) ([]To, error), filterTo func(To) (bool, error)) Loop[To]

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

func Filter

func Filter[T any](next func() (T, bool, error), 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, error), 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, error), 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, error), filter func(From) (bool, error), converter func(From) (To, error), filterTo func(To) (bool, error)) 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, error), 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 Flat

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

Flat converts a two-dimensional loop in an one-dimensional one.

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)) 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, error), flattener func(From) ([]To, error)) Loop[To]

Flatt converts a two-dimensional loop in an one-dimensional one.

func FlattAndFilter

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

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

func From

func From[T any](next func() (T, bool)) Loop[T]

From wrap the next loop to a breakable loop

func New

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

New is the main breakable loop constructor

func NoNilPtrVal added in v0.0.10

func NoNilPtrVal[T any](next func() (*T, bool, error)) 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, error)) Loop[*T]

NotNil creates a loop that filters nullable elements.

func PtrVal added in v0.0.10

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

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

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 (Loop[T]) Accum added in v0.0.13

func (next Loop[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 retrieved by the 'next' function.

func (Loop[T]) Accumm added in v0.0.13

func (next Loop[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 retrieved by the 'next' function.

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

func (next Loop[T]) All(consumer func(T, error) 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, error)

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

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

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

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

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

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

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]) HasAny added in v0.0.12

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

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, error)

Reduce reduces the elements retrieved by the 'next' function into an one using the 'merge' function. If the 'next' function returns ok=false at the first call, the zero value of 'T' type is returned.

func (Loop[T]) ReduceOK added in v0.0.13

func (next Loop[T]) ReduceOK(merge func(T, T) T) (result T, ok bool, err error)

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

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. If the 'next' function returns ok=false at the first call, the zero value of 'T' type is returned.

func (Loop[T]) ReduceeOK added in v0.0.13

func (next Loop[T]) ReduceeOK(merge func(T, T) (T, error)) (result T, ok bool, err error)

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

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

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

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, error)

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

Jump to

Keyboard shortcuts

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