github.com/enetx/g@v1.0.80/set_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 value in the sequence 10 // and a boolean indicating whether the value is valid. 11 // When the sequence is over, next returns the zero V 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 the zero V 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 SeqSet[V]) Pull() (func() (V, bool), func()) { return iter.Pull(iter.Seq[V](seq)) } 25 26 // Inspect creates a new iterator that wraps around the current iterator 27 // and allows inspecting each element as it passes through. 28 func (seq SeqSet[V]) Inspect(fn func(v V)) SeqSet[V] { return inspectSet(seq, fn) } 29 30 // Collect gathers all elements from the iterator into a Set. 31 func (seq SeqSet[V]) Collect() Set[V] { 32 collection := NewSet[V]() 33 34 seq(func(v V) bool { 35 collection.Add(v) 36 return true 37 }) 38 39 return collection 40 } 41 42 // Chain concatenates the current iterator with other iterators, returning a new iterator. 43 // 44 // The function creates a new iterator that combines the elements of the current iterator 45 // with elements from the provided iterators in the order they are given. 46 // 47 // Params: 48 // 49 // - seqs ([]SeqSet[V]): Other iterators to be concatenated with the current iterator. 50 // 51 // Returns: 52 // 53 // - SeqSet[V]: A new iterator containing elements from the current iterator and the provided iterators. 54 // 55 // Example usage: 56 // 57 // iter1 := g.SetOf(1, 2, 3).Iter() 58 // iter2 := g.SetOf(4, 5, 6).Iter() 59 // iter1.Chain(iter2).Collect().Print() 60 // 61 // Output: Set{3, 4, 5, 6, 1, 2} // The output order may vary as the Set type is not ordered. 62 // 63 // The resulting iterator will contain elements from both iterators. 64 func (seq SeqSet[V]) Chain(seqs ...SeqSet[V]) SeqSet[V] { 65 return chainSet(append([]SeqSet[V]{seq}, seqs...)...) 66 } 67 68 // Count consumes the iterator, counting the number of iterations and returning it. 69 func (seq SeqSet[V]) Count() Int { return countSet(seq) } 70 71 // ForEach iterates through all elements and applies the given function to each. 72 // 73 // The function applies the provided function to each element of the iterator. 74 // 75 // Params: 76 // 77 // - fn (func(V)): The function to apply to each element. 78 // 79 // Example usage: 80 // 81 // iter := g.SetOf(1, 2, 3).Iter() 82 // iter.ForEach(func(val V) { 83 // fmt.Println(val) // Replace this with the function logic you need. 84 // }) 85 // 86 // The provided function will be applied to each element in the iterator. 87 func (seq SeqSet[V]) ForEach(fn func(v V)) { 88 seq(func(v V) bool { 89 fn(v) 90 return true 91 }) 92 } 93 94 // The iteration will stop when the provided function returns false for an element. 95 func (seq SeqSet[V]) Range(fn func(v V) bool) { 96 seq(func(v V) bool { 97 return fn(v) 98 }) 99 } 100 101 // Filter returns a new iterator containing only the elements that satisfy the provided function. 102 // 103 // The function applies the provided function to each element of the iterator. 104 // If the function returns true for an element, that element is included in the resulting iterator. 105 // 106 // Parameters: 107 // 108 // - fn (func(V) bool): The function to be applied to each element of the iterator 109 // to determine if it should be included in the result. 110 // 111 // Returns: 112 // 113 // - SeqSet[V]: A new iterator containing the elements that satisfy the given condition. 114 // 115 // Example usage: 116 // 117 // set := g.SetOf(1, 2, 3, 4, 5) 118 // even := set.Iter(). 119 // Filter( 120 // func(val int) bool { 121 // return val%2 == 0 122 // }). 123 // Collect() 124 // even.Print() 125 // 126 // Output: Set{2, 4} // The output order may vary as the Set type is not ordered. 127 // 128 // The resulting iterator will contain only the elements that satisfy the provided function. 129 func (seq SeqSet[V]) Filter(fn func(V) bool) SeqSet[V] { return filterSet(seq, fn) } 130 131 // Exclude returns a new iterator excluding elements that satisfy the provided function. 132 // 133 // The function applies the provided function to each element of the iterator. 134 // If the function returns true for an element, that element is excluded from the resulting iterator. 135 // 136 // Parameters: 137 // 138 // - fn (func(V) bool): The function to be applied to each element of the iterator 139 // to determine if it should be excluded from the result. 140 // 141 // Returns: 142 // 143 // - SeqSet[V]: A new iterator containing the elements that do not satisfy the given condition. 144 // 145 // Example usage: 146 // 147 // set := g.SetOf(1, 2, 3, 4, 5) 148 // notEven := set.Iter(). 149 // Exclude( 150 // func(val int) bool { 151 // return val%2 == 0 152 // }). 153 // Collect() 154 // notEven.Print() 155 // 156 // Output: Set{1, 3, 5} // The output order may vary as the Set type is not ordered. 157 // 158 // The resulting iterator will contain only the elements that do not satisfy the provided function. 159 func (seq SeqSet[V]) Exclude(fn func(V) bool) SeqSet[V] { return exclude(seq, fn) } 160 161 // Map transforms each element in the iterator using the given function. 162 // 163 // The function creates a new iterator by applying the provided function to each element 164 // of the original iterator. 165 // 166 // Params: 167 // 168 // - fn (func(V) V): The function used to transform elements. 169 // 170 // Returns: 171 // 172 // - SeqSet[V]: A new iterator containing elements transformed by the provided function. 173 // 174 // Example usage: 175 // 176 // set := g.SetOf(1, 2, 3) 177 // doubled := set.Iter(). 178 // Map( 179 // func(val int) int { 180 // return val * 2 181 // }). 182 // Collect() 183 // doubled.Print() 184 // 185 // Output: Set{2, 4, 6} // The output order may vary as the Set type is not ordered. 186 // 187 // The resulting iterator will contain elements transformed by the provided function. 188 func (seq SeqSet[V]) Map(transform func(V) V) SeqSet[V] { return mapSet(seq, transform) } 189 190 func ToSeqSet[V comparable](slice Set[V]) SeqSet[V] { 191 return func(yield func(V) bool) { 192 for v := range slice { 193 if !yield(v) { 194 return 195 } 196 } 197 } 198 } 199 200 func inspectSet[V comparable](seq SeqSet[V], fn func(V)) SeqSet[V] { 201 return func(yield func(V) bool) { 202 seq(func(v V) bool { 203 fn(v) 204 return yield(v) 205 }) 206 } 207 } 208 209 func chainSet[V comparable](seqs ...SeqSet[V]) SeqSet[V] { 210 return func(yield func(V) bool) { 211 for _, seq := range seqs { 212 seq(func(v V) bool { 213 return yield(v) 214 }) 215 } 216 } 217 } 218 219 func mapSet[V, U comparable](seq SeqSet[V], fn func(V) U) SeqSet[U] { 220 return func(yield func(U) bool) { 221 seq(func(v V) bool { 222 return yield(fn(v)) 223 }) 224 } 225 } 226 227 func filterSet[V comparable](seq SeqSet[V], fn func(V) bool) SeqSet[V] { 228 return func(yield func(V) bool) { 229 seq(func(v V) bool { 230 if fn(v) { 231 return yield(v) 232 } 233 return true 234 }) 235 } 236 } 237 238 func exclude[V comparable](seq SeqSet[V], fn func(V) bool) SeqSet[V] { 239 return filterSet(seq, func(v V) bool { return !fn(v) }) 240 } 241 242 func difference[V comparable](seq SeqSet[V], other Set[V]) SeqSet[V] { 243 return func(yield func(V) bool) { 244 seq(func(v V) bool { 245 if other.Contains(v) { 246 return true 247 } 248 return yield(v) 249 }) 250 } 251 } 252 253 func intersection[V comparable](seq SeqSet[V], other Set[V]) SeqSet[V] { 254 return func(yield func(V) bool) { 255 seq(func(v V) bool { 256 if other.Contains(v) { 257 return yield(v) 258 } 259 return true 260 }) 261 } 262 } 263 264 func countSet[V comparable](seq SeqSet[V]) Int { 265 var counter Int 266 seq(func(V) bool { 267 counter++ 268 return true 269 }) 270 271 return counter 272 }