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