github.com/m4gshm/gollections@v0.0.10/kv/iface.go (about) 1 package kv 2 3 import "github.com/m4gshm/gollections/c" 4 5 // Iterator provides iterate over key/value pairs 6 type Iterator[K, V any] interface { 7 // Next returns the next key/value pair. 8 // The ok result indicates whether the element was returned by the iterator. 9 // If ok == false, then the iteration must be completed. 10 Next() (key K, value V, ok bool) 11 c.TrackLoop[K, V] 12 c.TrackEachLoop[K, V] 13 } 14 15 // IterFor extends an iterator type by a 'Start' function implementation 16 type IterFor[K, V any, I Iterator[K, V]] interface { 17 // Start is used with for loop construct. 18 // Returns the iterator itself, the first key/value pair, and ok == false if the iteration must be completed. 19 // 20 // var i IterFor = ... 21 // for i, k, v, ok := i.Start(); ok; k, v, ok = i.Next() { 22 // _ = val 23 // } 24 Start() (iterator I, key K, value V, ok bool) 25 } 26 27 // Iterable is an iterator supplier interface 28 type Iterable[K, V any] interface { 29 Iter() Iterator[K, V] 30 } 31 32 // Collection is the base interface of associative collections 33 type Collection[K comparable, V any, M map[K]V | map[K][]V] interface { 34 c.TrackLoop[K, V] 35 c.TrackEachLoop[K, V] 36 Iterable[K, V] 37 c.MapFactory[K, V, M] 38 39 Reduce(merger func(K, K, V, V) (K, V)) (K, V) 40 } 41 42 // Convertable provides limited kit of map transformation methods 43 type Convertable[K, V, KVStream, KVStreamBreakable any] interface { 44 Convert(converter func(K, V) (K, V)) KVStream 45 Conv(converter func(K, V) (K, V, error)) KVStreamBreakable 46 47 ConvertKey(converter func(K) K) KVStream 48 ConvertValue(converter func(V) V) KVStream 49 50 ConvKey(converter func(K) (K, error)) KVStreamBreakable 51 ConvValue(converter func(V) (V, error)) KVStreamBreakable 52 } 53 54 // Filterable provides limited kit of filering methods 55 type Filterable[K, V, KVStream, KVStreamBreakable any] interface { 56 Filter(predicate func(K, V) bool) KVStream 57 Filt(predicate func(K, V) (bool, error)) KVStreamBreakable 58 59 FilterKey(predicate func(K) bool) KVStream 60 FilterValue(predicate func(V) bool) KVStream 61 62 FiltKey(predicate func(K) (bool, error)) KVStreamBreakable 63 FiltValue(predicate func(V) (bool, error)) KVStreamBreakable 64 }