github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/kv/loop/loop.go (about) 1 package loop 2 3 import ( 4 breakkvloop "github.com/m4gshm/gollections/break/kv/loop" 5 breakMapFilter "github.com/m4gshm/gollections/break/kv/predicate" 6 breakMapConvert "github.com/m4gshm/gollections/break/map_/convert" 7 "github.com/m4gshm/gollections/kv/convert" 8 kvPredicate "github.com/m4gshm/gollections/kv/predicate" 9 ) 10 11 // Loop is a function that returns the next key, value or false if there are no more elements. 12 type Loop[K, V any] func() (K, V, bool) 13 14 // All is used to iterate through the loop using `for ... range`. Supported since go 1.22 with GOEXPERIMENT=rangefunc enabled. 15 func (next Loop[K, V]) All(consumer func(key K, value V) bool) { 16 All(next, consumer) 17 } 18 19 // Track applies the 'consumer' function to position/element pairs retrieved by the 'next' function until the consumer returns the c.Break to stop. 20 func (next Loop[K, V]) Track(consumer func(K, V) error) error { 21 return Track(next, consumer) 22 } 23 24 // First returns the first element that satisfies the condition of the 'predicate' function. 25 func (next Loop[K, V]) First(predicate func(K, V) bool) (K, V, bool) { 26 return First(next, predicate) 27 } 28 29 // Reduce reduces the elements retrieved by the 'next' function into an one using the 'merge' function. 30 func (next Loop[K, V]) Reduce(merge func(K, K, V, V) (K, V)) (K, V) { 31 return Reduce(next, merge) 32 } 33 34 // Reducee reduces the elements retrieved by the 'next' function into an one using the 'merge' function. 35 func (next Loop[K, V]) Reducee(merge func(K, K, V, V) (K, V, error)) (K, V, error) { 36 return Reducee(next, merge) 37 } 38 39 // HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful 40 func (next Loop[K, V]) HasAny(predicate func(K, V) bool) bool { 41 return HasAny(next, predicate) 42 } 43 44 // Filt creates a loop that checks elements by the 'filter' function and returns successful ones. 45 func (next Loop[K, V]) Filt(filter func(K, V) (bool, error)) breakkvloop.Loop[K, V] { 46 return Filt(next, filter) 47 } 48 49 // Filter creates a loop that checks elements by the 'filter' function and returns successful ones. 50 func (next Loop[K, V]) Filter(filter func(K, V) bool) Loop[K, V] { 51 return Filter(next, filter) 52 } 53 54 // Convert creates a loop that applies the 'converter' function to iterable key\values. 55 func (next Loop[K, V]) Convert(converter func(K, V) (K, V)) Loop[K, V] { 56 return Convert(next, converter) 57 } 58 59 // Conv creates a loop that applies the 'converter' function to iterable key\values. 60 func (next Loop[K, V]) Conv(converter func(K, V) (K, V, error)) breakkvloop.Loop[K, V] { 61 return Conv(next, converter) 62 } 63 64 // FilterKey returns a loop consisting of key/value pairs where the key satisfies the condition of the 'predicate' function 65 func (next Loop[K, V]) FilterKey(predicate func(K) bool) Loop[K, V] { 66 return Filter(next, kvPredicate.Key[V](predicate)) 67 } 68 69 // FiltKey returns a loop consisting of key/value pairs where the key satisfies the condition of the 'predicate' function 70 func (next Loop[K, V]) FiltKey(predicate func(K) (bool, error)) breakkvloop.Loop[K, V] { 71 return Filt(next, breakMapFilter.Key[V](predicate)) 72 } 73 74 // ConvertKey returns a loop that applies the 'converter' function to keys of the map 75 func (next Loop[K, V]) ConvertKey(by func(K) K) Loop[K, V] { 76 return Convert(next, convert.Key[V](by)) 77 } 78 79 // ConvKey returns a loop that applies the 'converter' function to keys of the map 80 func (next Loop[K, V]) ConvKey(converter func(K) (K, error)) breakkvloop.Loop[K, V] { 81 return Conv(next, breakMapConvert.Key[V](converter)) 82 } 83 84 // FilterValue returns a loop consisting of key/value pairs where the value satisfies the condition of the 'predicate' function 85 func (next Loop[K, V]) FilterValue(predicate func(V) bool) Loop[K, V] { 86 return Filter(next, kvPredicate.Value[K](predicate)) 87 } 88 89 // FiltValue returns a breakable loop consisting of key/value pairs where the value satisfies the condition of the 'predicate' function 90 func (next Loop[K, V]) FiltValue(predicate func(V) (bool, error)) breakkvloop.Loop[K, V] { 91 return Filt(next, breakMapFilter.Value[K](predicate)) 92 } 93 94 // ConvertValue returns a loop that applies the 'converter' function to values of the map 95 func (next Loop[K, V]) ConvertValue(converter func(V) V) Loop[K, V] { 96 return Convert(next, convert.Value[K](converter)) 97 } 98 99 // ConvValue returns a breakable loop that applies the 'converter' function to values of the map 100 func (next Loop[K, V]) ConvValue(converter func(V) (V, error)) breakkvloop.Loop[K, V] { 101 return Conv(next, breakMapConvert.Value[K](converter)) 102 } 103 104 // Crank rertieves a next element from the 'next' function, returns the function, element, successfully flag. 105 func (next Loop[K, V]) Crank() (Loop[K, V], K, V, bool) { 106 return Crank(next) 107 }