loop

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 5, 2023 License: MIT Imports: 5 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 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 Reduce

func Reduce[K, V any](next func() (K, V, bool), merge func(K, V, K, 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 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 any](next func() (K, V, bool)) []c.KV[K, V]

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), by 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 FitKV

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

FitKV 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) FitKV[K, V]

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

func (FitKV[K, V]) Next

func (f FitKV[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 (FitKV[K, V]) Track

func (f FitKV[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 (FitKV[K, V]) TrackEach

func (f FitKV[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