github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/kv/collection/iface.go (about) 1 package collection 2 3 import ( 4 breakloop "github.com/m4gshm/gollections/break/kv/loop" 5 "github.com/m4gshm/gollections/c" 6 "github.com/m4gshm/gollections/kv/loop" 7 ) 8 9 // Iterator provides iterate over key/value pairs 10 type Iterator[K, V any] interface { 11 // Next returns the next key/value pair. 12 // The ok result indicates whether the element was returned by the iterator. 13 // If ok == false, then the iteration must be completed. 14 Next() (key K, value V, ok bool) 15 c.Track[K, V] 16 c.TrackEach[K, V] 17 } 18 19 // Iterable is an iterator supplier interface 20 type Iterable[K, V any] interface { 21 Loop() loop.Loop[K, V] 22 } 23 24 // Collection is the base interface of associative collections 25 type Collection[K comparable, V any, M map[K]V | map[K][]V] interface { 26 c.Track[K, V] 27 c.TrackEach[K, V] 28 Iterable[K, V] 29 c.MapFactory[K, V, M] 30 31 Reduce(merger func(K, K, V, V) (K, V)) (K, V) 32 HasAny(func(K, V) bool) bool 33 All(consumer func(K, V) bool) 34 } 35 36 // Convertable provides limited kit of map transformation methods 37 type Convertable[K, V any] interface { 38 Convert(converter func(K, V) (K, V)) loop.Loop[K, V] 39 Conv(converter func(K, V) (K, V, error)) breakloop.Loop[K, V] 40 41 ConvertKey(converter func(K) K) loop.Loop[K, V] 42 ConvertValue(converter func(V) V) loop.Loop[K, V] 43 44 ConvKey(converter func(K) (K, error)) breakloop.Loop[K, V] 45 ConvValue(converter func(V) (V, error)) breakloop.Loop[K, V] 46 } 47 48 // Filterable provides limited kit of filering methods 49 type Filterable[K, V any] interface { 50 Filter(predicate func(K, V) bool) loop.Loop[K, V] 51 Filt(predicate func(K, V) (bool, error)) breakloop.Loop[K, V] 52 53 FilterKey(predicate func(K) bool) loop.Loop[K, V] 54 FilterValue(predicate func(V) bool) loop.Loop[K, V] 55 56 FiltKey(predicate func(K) (bool, error)) breakloop.Loop[K, V] 57 FiltValue(predicate func(V) (bool, error)) breakloop.Loop[K, V] 58 }