github.com/enetx/g@v1.0.80/map_iter.go (about) 1 package g 2 3 import "iter" 4 5 // Pull converts the “push-style” iterator sequence seq 6 // into a “pull-style” iterator accessed by the two functions 7 // next and stop. 8 // 9 // Next returns the next pair in the sequence 10 // and a boolean indicating whether the pair is valid. 11 // When the sequence is over, next returns a pair of zero values and false. 12 // It is valid to call next after reaching the end of the sequence 13 // or after calling stop. These calls will continue 14 // to return a pair of zero values and false. 15 // 16 // Stop ends the iteration. It must be called when the caller is 17 // no longer interested in next values and next has not yet 18 // signaled that the sequence is over (with a false boolean return). 19 // It is valid to call stop multiple times and when next has 20 // already returned false. 21 // 22 // It is an error to call next or stop from multiple goroutines 23 // simultaneously. 24 func (seq SeqMap[K, V]) Pull() (func() (K, V, bool), func()) { return iter.Pull2(iter.Seq2[K, V](seq)) } 25 26 // Take returns a new iterator with the first n elements. 27 // The function creates a new iterator containing the first n elements from the original iterator. 28 func (seq SeqMap[K, V]) Take(n uint) SeqMap[K, V] { return takeMap(seq, n) } 29 30 // Keys returns an iterator containing all the keys in the ordered Map. 31 func (seq SeqMap[K, V]) Keys() SeqSlice[K] { return keysMap(seq) } 32 33 // Values returns an iterator containing all the values in the ordered Map. 34 func (seq SeqMap[K, V]) Values() SeqSlice[V] { return valuesMap(seq) } 35 36 // Chain creates a new iterator by concatenating the current iterator with other iterators. 37 // 38 // The function concatenates the key-value pairs from the current iterator with the key-value pairs from the provided iterators, 39 // producing a new iterator containing all concatenated elements. 40 // 41 // Params: 42 // 43 // - seqs ([]SeqMap[K, V]): Other iterators to be concatenated with the current iterator. 44 // 45 // Returns: 46 // 47 // - SeqMap[K, V]: A new iterator containing elements from the current iterator and the provided iterators. 48 // 49 // Example usage: 50 // 51 // iter1 := g.NewMap[int, string]().Set(1, "a").Iter() 52 // iter2 := g.NewMap[int, string]().Set(2, "b").Iter() 53 // 54 // // Concatenating iterators and collecting the result. 55 // iter1.Chain(iter2).Collect().Print() 56 // 57 // Output: Map{1:a, 2:b} // The output order may vary as Map is not ordered. 58 // 59 // The resulting iterator will contain elements from both iterators. 60 func (seq SeqMap[K, V]) Chain(seqs ...SeqMap[K, V]) SeqMap[K, V] { 61 return chainMap(append([]SeqMap[K, V]{seq}, seqs...)...) 62 } 63 64 // Count consumes the iterator, counting the number of iterations and returning it. 65 func (seq SeqMap[K, V]) Count() Int { return countMap(seq) } 66 67 // Collect collects all key-value pairs from the iterator and returns a Map. 68 func (seq SeqMap[K, V]) Collect() Map[K, V] { 69 collection := NewMap[K, V]() 70 71 seq(func(k K, v V) bool { 72 collection.Set(k, v) 73 return true 74 }) 75 76 return collection 77 } 78 79 // Exclude returns a new iterator excluding elements that satisfy the provided function. 80 // 81 // This function creates a new iterator excluding key-value pairs for which the provided function returns true. 82 // It iterates through the current iterator, applying the function to each key-value pair. 83 // If the function returns true for a key-value pair, it will be excluded from the resulting iterator. 84 // 85 // Params: 86 // 87 // - fn (func(K, V) bool): The function applied to each key-value pair to determine exclusion. 88 // 89 // Returns: 90 // 91 // - SeqMap[K, V]: An iterator excluding elements that satisfy the given function. 92 // 93 // Example usage: 94 // 95 // m := g.NewMap[int, int](). 96 // Set(1, 1). 97 // Set(2, 2). 98 // Set(3, 3). 99 // Set(4, 4). 100 // Set(5, 5) 101 // 102 // notEven := m.Iter(). 103 // Exclude( 104 // func(k, v int) bool { 105 // return v%2 == 0 106 // }). 107 // Collect() 108 // notEven.Print() 109 // 110 // Output: Map{1:1, 3:3, 5:5} // The output order may vary as Map is not ordered. 111 // 112 // The resulting iterator will exclude elements for which the function returns true. 113 func (seq SeqMap[K, V]) Exclude(fn func(K, V) bool) SeqMap[K, V] { return excludeMap(seq, fn) } 114 115 // Filter returns a new iterator containing only the elements that satisfy the provided function. 116 // 117 // This function creates a new iterator containing key-value pairs for which the provided function returns true. 118 // It iterates through the current iterator, applying the function to each key-value pair. 119 // If the function returns true for a key-value pair, it will be included in the resulting iterator. 120 // 121 // Params: 122 // 123 // - fn (func(K, V) bool): The function applied to each key-value pair to determine inclusion. 124 // 125 // Returns: 126 // 127 // - SeqMap[K, V]: An iterator containing elements that satisfy the given function. 128 // 129 // m := g.NewMap[int, int](). 130 // Set(1, 1). 131 // Set(2, 2). 132 // Set(3, 3). 133 // Set(4, 4). 134 // Set(5, 5) 135 // 136 // even := m.Iter(). 137 // Filter( 138 // func(k, v int) bool { 139 // return v%2 == 0 140 // }). 141 // Collect() 142 // even.Print() 143 // 144 // Output: Map{2:2, 4:4} // The output order may vary as Map is not ordered. 145 // 146 // The resulting iterator will contain elements for which the function returns true. 147 func (seq SeqMap[K, V]) Filter(fn func(K, V) bool) SeqMap[K, V] { return filterMap(seq, fn) } 148 149 // The resulting Option may contain the first element that satisfies the condition, or None if not found. 150 func (seq SeqMap[K, V]) Find(fn func(k K, v V) bool) Option[Pair[K, V]] { return findMap(seq, fn) } 151 152 // ForEach iterates through all elements and applies the given function to each key-value pair. 153 // 154 // This function traverses the entire iterator and applies the provided function to each key-value pair. 155 // It iterates through the current iterator, executing the function on each key-value pair. 156 // 157 // Params: 158 // 159 // - fn (func(K, V)): The function to be applied to each key-value pair in the iterator. 160 // 161 // Example usage: 162 // 163 // m := g.NewMap[int, int](). 164 // Set(1, 1). 165 // Set(2, 2). 166 // Set(3, 3). 167 // Set(4, 4). 168 // Set(5, 5) 169 // 170 // mmap := m.Iter(). 171 // Map( 172 // func(k, v int) (int, int) { 173 // return k * k, v * v 174 // }). 175 // Collect() 176 // 177 // mmap.Print() 178 // 179 // Output: Map{1:1, 4:4, 9:9, 16:16, 25:25} // The output order may vary as Map is not ordered. 180 // 181 // The function fn will be executed for each key-value pair in the iterator. 182 func (seq SeqMap[K, V]) ForEach(fn func(k K, v V)) { 183 seq(func(k K, v V) bool { 184 fn(k, v) 185 return true 186 }) 187 } 188 189 // Inspect creates a new iterator that wraps around the current iterator 190 // and allows inspecting each key-value pair as it passes through. 191 func (seq SeqMap[K, V]) Inspect(fn func(k K, v V)) SeqMap[K, V] { return inspectMap(seq, fn) } 192 193 // Map creates a new iterator by applying the given function to each key-value pair. 194 // 195 // This function generates a new iterator by traversing the current iterator and applying the provided 196 // function to each key-value pair. It transforms the key-value pairs according to the given function. 197 // 198 // Params: 199 // 200 // - fn (func(K, V) (K, V)): The function to be applied to each key-value pair in the iterator. 201 // It takes a key-value pair and returns a new transformed key-value pair. 202 // 203 // Returns: 204 // 205 // - SeqMap[K, V]: A new iterator containing key-value pairs transformed by the provided function. 206 // 207 // Example usage: 208 // 209 // m := g.NewMap[int, int](). 210 // Set(1, 1). 211 // Set(2, 2). 212 // Set(3, 3). 213 // Set(4, 4). 214 // Set(5, 5) 215 // 216 // mmap := m.Iter(). 217 // Map( 218 // func(k, v int) (int, int) { 219 // return k * k, v * v 220 // }). 221 // Collect() 222 // 223 // mmap.Print() 224 // 225 // Output: Map{1:1, 4:4, 9:9, 16:16, 25:25} // The output order may vary as Map is not ordered. 226 // 227 // The resulting iterator will contain key-value pairs transformed by the given function. 228 func (seq SeqMap[K, V]) Map(transform func(K, V) (K, V)) SeqMap[K, V] { return mapMap(seq, transform) } 229 230 // The iteration will stop when the provided function returns false for an element. 231 func (seq SeqMap[K, V]) Range(fn func(k K, v V) bool) { 232 seq(func(k K, v V) bool { 233 return fn(k, v) 234 }) 235 } 236 237 func ToSeqMap[K comparable, V any](hashmap map[K]V) SeqMap[K, V] { 238 return func(yield func(K, V) bool) { 239 for k, v := range hashmap { 240 if !yield(k, v) { 241 return 242 } 243 } 244 } 245 } 246 247 func chainMap[K comparable, V any](seqs ...SeqMap[K, V]) SeqMap[K, V] { 248 return func(yield func(K, V) bool) { 249 for _, seq := range seqs { 250 seq(func(k K, v V) bool { 251 return yield(k, v) 252 }) 253 } 254 } 255 } 256 257 func mapMap[K comparable, V any](seq SeqMap[K, V], fn func(K, V) (K, V)) SeqMap[K, V] { 258 return func(yield func(K, V) bool) { 259 seq(func(k K, v V) bool { 260 return yield(fn(k, v)) 261 }) 262 } 263 } 264 265 func filterMap[K comparable, V any](seq SeqMap[K, V], fn func(K, V) bool) SeqMap[K, V] { 266 return func(yield func(K, V) bool) { 267 seq(func(k K, v V) bool { 268 if fn(k, v) { 269 return yield(k, v) 270 } 271 return true 272 }) 273 } 274 } 275 276 func excludeMap[K comparable, V any](s SeqMap[K, V], fn func(K, V) bool) SeqMap[K, V] { 277 return filterMap(s, func(k K, v V) bool { return !fn(k, v) }) 278 } 279 280 func inspectMap[K comparable, V any](seq SeqMap[K, V], fn func(K, V)) SeqMap[K, V] { 281 return func(yield func(K, V) bool) { 282 seq(func(k K, v V) bool { 283 fn(k, v) 284 return yield(k, v) 285 }) 286 } 287 } 288 289 func keysMap[K comparable, V any](seq SeqMap[K, V]) SeqSlice[K] { 290 return func(yield func(K) bool) { 291 seq(func(k K, _ V) bool { 292 return yield(k) 293 }) 294 } 295 } 296 297 func valuesMap[K comparable, V any](seq SeqMap[K, V]) SeqSlice[V] { 298 return func(yield func(V) bool) { 299 seq(func(_ K, v V) bool { 300 return yield(v) 301 }) 302 } 303 } 304 305 func findMap[K comparable, V any](seq SeqMap[K, V], fn func(K, V) bool) (r Option[Pair[K, V]]) { 306 seq(func(k K, v V) bool { 307 if !fn(k, v) { 308 return true 309 } 310 r = Some(Pair[K, V]{k, v}) 311 return false 312 }) 313 314 return r 315 } 316 317 func takeMap[K comparable, V any](seq SeqMap[K, V], n uint) SeqMap[K, V] { 318 return func(yield func(K, V) bool) { 319 seq(func(k K, v V) bool { 320 if n == 0 { 321 return false 322 } 323 n-- 324 return yield(k, v) 325 }) 326 } 327 } 328 329 func countMap[K comparable, V any](seq SeqMap[K, V]) Int { 330 var counter Int 331 seq(func(K, V) bool { 332 counter++ 333 return true 334 }) 335 336 return counter 337 }