github.com/m4gshm/gollections@v0.0.10/collection/immutable/ordered/keys.go (about) 1 package ordered 2 3 import ( 4 "fmt" 5 6 breakStream "github.com/m4gshm/gollections/break/stream" 7 "github.com/m4gshm/gollections/c" 8 "github.com/m4gshm/gollections/slice" 9 "github.com/m4gshm/gollections/slice/iter" 10 "github.com/m4gshm/gollections/stream" 11 ) 12 13 // WrapKeys instantiates MapKeys using the elements as internal storage 14 func WrapKeys[K comparable](elements []K) MapKeys[K] { 15 return MapKeys[K]{elements} 16 } 17 18 // MapKeys is the wrapper for Map'm keys 19 type MapKeys[K comparable] struct { 20 keys []K 21 } 22 23 var ( 24 _ c.Collection[int] = (*MapKeys[int])(nil) 25 _ c.Collection[int] = MapKeys[int]{} 26 _ fmt.Stringer = (*MapKeys[int])(nil) 27 _ fmt.Stringer = MapKeys[int]{} 28 ) 29 30 // Iter creates an iterator and returns as interface 31 func (m MapKeys[K]) Iter() c.Iterator[K] { 32 h := m.Head() 33 return &h 34 } 35 36 // Head creates an iterator and returns as implementation type value 37 func (m MapKeys[K]) Head() slice.Iter[K] { 38 return slice.NewHead(m.keys) 39 } 40 41 // First returns the first element of the collection, an iterator to iterate over the remaining elements, and true\false marker of availability next elements. 42 // If no more elements then ok==false. 43 func (m MapKeys[K]) First() (slice.Iter[K], K, bool) { 44 var ( 45 iterator = m.Head() 46 first, ok = iterator.Next() 47 ) 48 return iterator, first, ok 49 } 50 51 // Len returns amount of elements 52 func (m MapKeys[K]) Len() int { 53 return len(m.keys) 54 } 55 56 // IsEmpty returns true if the collection is empty 57 func (m MapKeys[K]) IsEmpty() bool { 58 return m.Len() == 0 59 } 60 61 // Slice collects the elements to a slice 62 func (m MapKeys[K]) Slice() (out []K) { 63 if keys := m.keys; keys != nil { 64 out = slice.Clone(keys) 65 } 66 return out 67 } 68 69 // Append collects the values to the specified 'out' slice 70 func (m MapKeys[K]) Append(out []K) []K { 71 if keys := m.keys; keys != nil { 72 out = append(out, keys...) 73 } 74 return out 75 } 76 77 // For applies the 'walker' function for every key. Return the c.ErrBreak to stop. 78 func (m MapKeys[K]) For(walker func(K) error) error { 79 return slice.For(m.keys, walker) 80 } 81 82 // ForEach applies the 'walker' function for every element 83 func (m MapKeys[K]) ForEach(walker func(K)) { 84 slice.ForEach(m.keys, walker) 85 } 86 87 // Filter returns a stream consisting of elements that satisfy the condition of the 'predicate' function 88 func (m MapKeys[K]) Filter(filter func(K) bool) stream.Iter[K] { 89 f := iter.Filter(m.keys, filter) 90 return stream.New(f.Next) 91 } 92 93 // Filt returns a breakable stream consisting of elements that satisfy the condition of the 'predicate' function 94 func (m MapKeys[K]) Filt(predicate func(K) (bool, error)) breakStream.Iter[K] { 95 f := iter.Filt(m.keys, predicate) 96 return breakStream.New(f.Next) 97 } 98 99 // Convert returns a stream that applies the 'converter' function to the collection elements 100 func (m MapKeys[K]) Convert(converter func(K) K) stream.Iter[K] { 101 conv := iter.Convert(m.keys, converter) 102 return stream.New(conv.Next) 103 } 104 105 // Conv returns a breakable stream that applies the 'converter' function to the collection elements 106 func (m MapKeys[K]) Conv(converter func(K) (K, error)) breakStream.Iter[K] { 107 conv := iter.Conv(m.keys, converter) 108 return breakStream.New(conv.Next) 109 } 110 111 // Reduce reduces the elements into an one using the 'merge' function 112 func (m MapKeys[K]) Reduce(merge func(K, K) K) K { 113 return slice.Reduce(m.keys, merge) 114 } 115 116 // HasAny finds the first element that satisfies the 'predicate' function condition and returns true if successful 117 func (m MapKeys[K]) HasAny(predicate func(K) bool) bool { 118 return slice.HasAny(m.keys, predicate) 119 } 120 121 // String returns string representation of the collection 122 func (m MapKeys[K]) String() string { 123 return slice.ToString(m.Slice()) 124 } 125 126 // Get returns an element by the index, otherwise, if the provided index is ouf of the collection len, returns zero T and false in the second result 127 func (m MapKeys[K]) Get(index int) (K, bool) { 128 return slice.Gett(m.keys, index) 129 }