github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/collection/immutable/keys.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 // WrapKeys instantiates MapKeys using the elements as internal storage 14 func WrapKeys[K comparable, V any](elements map[K]V) MapKeys[K, V] { 15 return MapKeys[K, V]{elements} 16 } 17 18 // MapKeys is the container reveal keys of a map and hides values 19 type MapKeys[K comparable, V any] struct { 20 elements map[K]V 21 } 22 23 var ( 24 _ collection.Collection[int] = (*MapKeys[int, any])(nil) 25 _ collection.Collection[int] = MapKeys[int, any]{} 26 _ fmt.Stringer = (*MapKeys[int, any])(nil) 27 _ fmt.Stringer = MapKeys[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 MapKeys[K, V]) All(consumer func(K) bool) { 32 map_.TrackKeysWhile(m.elements, consumer) 33 } 34 35 // Loop creates a loop to iterate through the collection. 36 func (m MapKeys[K, V]) Loop() loop.Loop[K] { 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 MapKeys[K, V]) Head() map_.KeyIter[K, V] { 44 return map_.NewKeyIter(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 MapKeys[K, V]) First() (map_.KeyIter[K, V], K, 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 MapKeys[K, V]) Len() int { 60 return len(m.elements) 61 } 62 63 // IsEmpty returns true if the collection is empty 64 func (m MapKeys[K, V]) IsEmpty() bool { 65 return m.Len() == 0 66 } 67 68 // Slice collects the elements to a slice 69 func (m MapKeys[K, V]) Slice() []K { 70 return map_.Keys(m.elements) 71 } 72 73 // Append collects the values to the specified 'out' slice 74 func (m MapKeys[K, V]) Append(out []K) []K { 75 return map_.AppendKeys(m.elements, out) 76 } 77 78 // For applies the 'consumer' function for every key until the consumer returns the c.Break to stop. 79 func (m MapKeys[K, V]) For(consumer func(K) error) error { 80 return map_.ForKeys(m.elements, consumer) 81 } 82 83 // ForEach applies the 'consumer' function for every key 84 func (m MapKeys[K, V]) ForEach(consumer func(K)) { 85 map_.ForEachKey(m.elements, consumer) 86 } 87 88 // Filter returns a loop consisting of elements that satisfy the condition of the 'predicate' function 89 func (m MapKeys[K, V]) Filter(filter func(K) bool) loop.Loop[K] { 90 return loop.Filter(m.Loop(), filter) 91 } 92 93 // Filt returns a breakable loop consisting of elements that satisfy the condition of the 'predicate' function 94 func (m MapKeys[K, V]) Filt(predicate func(K) (bool, error)) breakLoop.Loop[K] { 95 return loop.Filt(m.Loop(), predicate) 96 } 97 98 // Convert returns a loop that applies the 'converter' function to the collection elements 99 func (m MapKeys[K, V]) Convert(converter func(K) K) loop.Loop[K] { 100 return loop.Convert(m.Loop(), converter) 101 } 102 103 // Conv returns a breakable loop that applies the 'converter' function to the collection elements 104 func (m MapKeys[K, V]) Conv(converter func(K) (K, error)) breakLoop.Loop[K] { 105 return loop.Conv(m.Loop(), converter) 106 } 107 108 // Reduce reduces the elements into an one using the 'merge' function 109 func (m MapKeys[K, V]) Reduce(merge func(K, K) K) K { 110 k, _ := map_.Reduce(m.elements, func(k1, k2 K, _, _ V) (rk K, rv V) { 111 return merge(k1, k2), rv 112 }) 113 return k 114 } 115 116 // HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful 117 func (m MapKeys[K, V]) HasAny(predicate func(K) bool) bool { 118 return map_.HasAny(m.elements, func(k K, _ V) bool { 119 return predicate(k) 120 }) 121 } 122 123 func (m MapKeys[K, V]) String() string { 124 return slice.ToString(m.Slice()) 125 }