github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/immutable/values.go (about) 1 package immutable 2 3 import ( 4 "fmt" 5 6 breakLoop "github.com/m4gshm/gollections/break/loop" 7 "github.com/m4gshm/gollections/collection" 8 "github.com/m4gshm/gollections/loop" 9 "github.com/m4gshm/gollections/map_" 10 "github.com/m4gshm/gollections/slice" 11 ) 12 13 // WrapVal instantiates MapValues using elements as internal storage. 14 func WrapVal[K comparable, V any](elements map[K]V) MapValues[K, V] { 15 return MapValues[K, V]{elements} 16 } 17 18 // MapValues is the wrapper for Map'm values. 19 type MapValues[K comparable, V any] struct { 20 elements map[K]V 21 } 22 23 var ( 24 _ collection.Collection[any] = (*MapValues[int, any])(nil) 25 _ collection.Collection[any] = MapValues[int, any]{} 26 _ fmt.Stringer = (*MapValues[int, any])(nil) 27 _ fmt.Stringer = MapValues[int, any]{} 28 ) 29 30 // All is used to iterate through the collection using `for ... range`. Supported since go 1.22 with GOEXPERIMENT=rangefunc enabled. 31 func (m MapValues[K, V]) All(consumer func(V) bool) { 32 map_.TrackValuesWhile(m.elements, consumer) 33 } 34 35 // Loop creates a loop to iterate through the collection. 36 func (m MapValues[K, V]) Loop() loop.Loop[V] { 37 h := m.Head() 38 return (&h).Next 39 } 40 41 // Deprecated: Head is deprecated. Will be replaced by rance-over function iterator. 42 // Head creates an iterator to iterate through the collection. 43 func (m MapValues[K, V]) Head() map_.ValIter[K, V] { 44 return map_.NewValIter(m.elements) 45 } 46 47 // Deprecated: First is deprecated. Will be replaced by rance-over function iterator. 48 // First returns the first element of the collection, an iterator to iterate over the remaining elements, and true\false marker of availability next elements. 49 // If no more elements then ok==false. 50 func (m MapValues[K, V]) First() (map_.ValIter[K, V], V, bool) { 51 var ( 52 iterator = m.Head() 53 first, ok = iterator.Next() 54 ) 55 return iterator, first, ok 56 } 57 58 // Len returns amount of elements 59 func (m MapValues[K, V]) Len() int { 60 return len(m.elements) 61 } 62 63 // IsEmpty returns true if the collection is empty 64 func (m MapValues[K, V]) IsEmpty() bool { 65 return m.Len() == 0 66 } 67 68 // Slice collects the values to a slice 69 func (m MapValues[K, V]) Slice() []V { 70 return map_.Values(m.elements) 71 } 72 73 // Append collects the values to the specified 'out' slice 74 func (m MapValues[K, V]) Append(out []V) []V { 75 return map_.AppendValues(m.elements, out) 76 } 77 78 // For applies the 'consumer' function for collection values until the consumer returns the c.Break to stop. 79 func (m MapValues[K, V]) For(consumer func(V) error) error { 80 return map_.ForValues(m.elements, consumer) 81 } 82 83 // ForEach applies the 'consumer' function for every value of the collection 84 func (m MapValues[K, V]) ForEach(consumer func(V)) { 85 map_.ForEachValue(m.elements, consumer) 86 } 87 88 // Filter returns a loop consisting of elements that satisfy the condition of the 'predicate' function 89 func (m MapValues[K, V]) Filter(filter func(V) bool) loop.Loop[V] { 90 h := m.Head() 91 return loop.Filter(h.Next, filter) 92 } 93 94 // Filt returns a breakable loop consisting of elements that satisfy the condition of the 'predicate' function 95 func (m MapValues[K, V]) Filt(predicate func(V) (bool, error)) breakLoop.Loop[V] { 96 return loop.Filt(m.Loop(), predicate) 97 } 98 99 // Convert returns a loop that applies the 'converter' function to the collection elements 100 func (m MapValues[K, V]) Convert(converter func(V) V) loop.Loop[V] { 101 return loop.Convert(m.Loop(), converter) 102 } 103 104 // Conv returns a breakable loop that applies the 'converter' function to the collection elements 105 func (m MapValues[K, V]) Conv(converter func(V) (V, error)) breakLoop.Loop[V] { 106 return loop.Conv(m.Loop(), converter) 107 } 108 109 // Reduce reduces the elements into an one using the 'merge' function 110 func (m MapValues[K, V]) Reduce(merge func(V, V) V) V { 111 _, v := map_.Reduce(m.elements, func(_, _ K, v1, v2 V) (k K, v V) { 112 return k, merge(v1, v2) 113 }) 114 return v 115 } 116 117 // HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful 118 func (m MapValues[K, V]) HasAny(predicate func(V) bool) bool { 119 return map_.HasAny(m.elements, func(_ K, v V) bool { 120 return predicate(v) 121 }) 122 } 123 124 // Sort creates a vector with sorted the values 125 func (m MapValues[K, V]) Sort(comparer slice.Comparer[V]) Vector[V] { 126 return WrapVector(slice.Sort(m.Slice(), comparer)) 127 } 128 129 func (m MapValues[K, V]) String() string { 130 return slice.ToString(m.Slice()) 131 }