loop

package
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: May 26, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package loop provides helpers for loop operation over key/value pairs and iterator implementations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Conv added in v0.0.8

func Conv[K, V any, KOUT, VOUT any](next func() (K, V, bool), converter func(K, V) (KOUT, VOUT, error)) loop.ConvertIter[K, V, KOUT, VOUT]

Conv creates an iterator that applies a transformer to iterable key\values.

func Filt added in v0.0.8

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

Filt creates an iterator that checks elements by a filter and returns successful ones

func First added in v0.0.8

func First[K, V any](next func() (K, V, bool), predicate func(K, V) bool) (K, V, bool)

First returns the first key/value pair that satisfies the condition of the 'predicate' function

func Firstt added in v0.0.8

func Firstt[K, V any](next func() (K, V, bool), predicate func(K, V) (bool, error)) (K, V, bool, error)

Firstt returns the first key/value pair that satisfies the condition of the 'predicate' function

func Group

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

Group collects sets of values grouped by keys obtained by passing a key/value iterator

func HasAny

func HasAny[K, V any](next func() (K, V, bool), predicate func(K, V) bool) bool

HasAny finds the first key/value pair that satisfies the 'predicate' function condition and returns true if successful

func HasAnyy added in v0.0.8

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

HasAnyy finds the first key/value pair that satisfies the 'predicate' function condition and returns true if successful

func Reduce

func Reduce[K, V any](next func() (K, V, bool), merge func(K, K, V, V) (K, V)) (rk K, rv V)

Reduce reduces the key/value pairs retrieved by the 'next' function into an one pair using the 'merge' function

func Reducee added in v0.0.8

func Reducee[K, V any](next func() (K, V, bool), merge func(K, K, V, V) (K, V, error)) (rk K, rv V, err error)

Reducee reduces the key/value pairs retrieved by the 'next' function into an one pair using the 'merge' function

func ToMap

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

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

func ToMapResolv

func ToMapResolv[K comparable, V, VR any](next func() (K, V, bool), 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 ToSlice

func ToSlice[K, V, T any](next func() (K, V, bool), converter func(K, V) T) []T

ToSlice collects key\value elements to a slice by iterating over the elements

Types

type ConvertIter

type ConvertIter[K, V any, K2, V2 any, C func(K, V) (K2, V2)] struct {
	// contains filtered or unexported fields
}

ConvertIter is the iterator wrapper implementation applying a converter to all iterable key/value elements.

func Convert

func Convert[K, V any, k2, v2 any](next func() (K, V, bool), converter func(K, V) (k2, v2)) ConvertIter[K, V, k2, v2, func(K, V) (k2, v2)]

Convert creates an iterator that applies a transformer to iterable key\values.

func (ConvertIter[K, V, K2, V2, C]) Next

func (i ConvertIter[K, V, K2, V2, C]) Next() (k2 K2, v2 V2, ok bool)

Next returns the next key/value pair. The ok result indicates whether an pair was returned by the iterator. If ok == false, then the iteration must be completed.

func (ConvertIter[K, V, K2, V2, C]) Track

func (i ConvertIter[K, V, K2, V2, C]) Track(traker func(key K2, value V2) error) error

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

func (ConvertIter[K, V, K2, V2, C]) TrackEach

func (i ConvertIter[K, V, K2, V2, C]) TrackEach(traker func(key K2, value V2))

TrackEach takes all key, value pairs retrieved by the iterator

type FilterIter added in v0.0.9

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

FilterIter is the KVIterator wrapper that provides filtering of key/value elements by a Predicate.

func Filter

func Filter[K, V any](next func() (K, V, bool), filter func(K, V) bool) FilterIter[K, V]

Filter creates an iterator that checks elements by a filter and returns successful ones

func (FilterIter[K, V]) Next added in v0.0.9

func (f FilterIter[K, V]) Next() (key K, value V, ok bool)

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

func (FilterIter[K, V]) Track added in v0.0.9

func (f FilterIter[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 (FilterIter[K, V]) TrackEach added in v0.0.9

func (f FilterIter[K, V]) TrackEach(traker func(key K, value V))

TrackEach takes all key, value pairs retrieved by the iterator

type Iter

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

Iter - universal key\value iterator implementation

func NewIter

func NewIter[S, K, V any](source S, hasNext func(S) bool, getNext func(S) (K, V, error)) Iter[S, K, V]

NewIter creates an Iter instance that loops over key\value elements of a source. The hasNext specifies a predicate that tests existing of a next element in the source. The getNext extracts the one.

func (*Iter[S, K, V]) Error

func (i *Iter[S, K, V]) Error() error

Error implements kv.KVIteratorBreakable

func (*Iter[S, K, V]) Next

func (i *Iter[S, K, V]) Next() (K, V, bool)

Next implements kv.KVIterator

func (*Iter[S, K, V]) Track

func (i *Iter[S, 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 (*Iter[S, K, V]) TrackEach

func (i *Iter[S, K, V]) TrackEach(traker func(key K, value V))

TrackEach takes all key, value pairs retrieved by the iterator

type Looper

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

Looper provides an iterable loop function

Directories

Path Synopsis
Package group provides short aliases for functions thath are used to group key/value pairs retrieved by a loop
Package group provides short aliases for functions thath are used to group key/value pairs retrieved by a loop

Jump to

Keyboard shortcuts

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