gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/stream/ranger/ranger.go (about) 1 package ranger 2 3 import ( 4 "errors" 5 "gitee.com/sy_183/go-common/generic" 6 "gitee.com/sy_183/go-common/maps" 7 "gitee.com/sy_183/go-common/slice" 8 "gitee.com/sy_183/go-common/stream" 9 "gitee.com/sy_183/go-common/strings" 10 ) 11 12 var EOF = errors.New("EOF") 13 14 func Range[I generic.Integer](size I) stream.Ranger[I] { 15 return func(h stream.Handler[I]) error { 16 for i := I(0); i < size; i++ { 17 if err := h(i); err != nil { 18 return err 19 } 20 } 21 return nil 22 } 23 } 24 25 func Repeat[V any, I generic.Integer](v V, size I) stream.Ranger[V] { 26 return func(h stream.Handler[V]) error { 27 for i := I(0); i < size; i++ { 28 if err := h(v); err != nil { 29 return err 30 } 31 } 32 return nil 33 } 34 } 35 36 func RepeatNew[V any, I generic.Integer](newFn func() V, size I) stream.Ranger[V] { 37 return func(h stream.Handler[V]) error { 38 for i := I(0); i < size; i++ { 39 if err := h(newFn()); err != nil { 40 return err 41 } 42 } 43 return nil 44 } 45 } 46 47 func RepeatClone[V any, I generic.Integer](v V, cloneFn func(V) V, size I) stream.Ranger[V] { 48 return func(h stream.Handler[V]) error { 49 for i := I(0); i < size; i++ { 50 if err := h(cloneFn(v)); err != nil { 51 return err 52 } 53 } 54 return nil 55 } 56 } 57 58 func Elems[E any](elems ...E) stream.Ranger[E] { 59 return Slice(elems) 60 } 61 62 func Slice[E any](s []E) stream.Ranger[E] { 63 return func(h stream.Handler[E]) error { 64 for i := range s { 65 if err := h(s[i]); err != nil { 66 return err 67 } 68 } 69 return nil 70 } 71 } 72 73 func ReverseSlice[E any](s []E) stream.Ranger[E] { 74 return func(h stream.Handler[E]) error { 75 for i := len(s) - 1; i >= 0; i-- { 76 if err := h(s[i]); err != nil { 77 return err 78 } 79 } 80 return nil 81 } 82 } 83 84 func SliceP[E any](s []E) stream.Ranger[*E] { 85 return func(h stream.Handler[*E]) error { 86 for i := range s { 87 if err := h(&s[i]); err != nil { 88 return err 89 } 90 } 91 return nil 92 } 93 } 94 95 func ReverseSliceP[E any](s []E) stream.Ranger[*E] { 96 return func(h stream.Handler[*E]) error { 97 for i := len(s) - 1; i >= 0; i-- { 98 if err := h(&s[i]); err != nil { 99 return err 100 } 101 } 102 return nil 103 } 104 } 105 106 func SliceEntries[E any](s []E) stream.Ranger[slice.Entry[E]] { 107 return func(h stream.Handler[slice.Entry[E]]) error { 108 for i, e := range s { 109 if err := h(slice.NewEntry(i, e)); err != nil { 110 return err 111 } 112 } 113 return nil 114 } 115 } 116 117 func ReversSliceEntries[E any](s []E) stream.Ranger[slice.Entry[E]] { 118 return func(h stream.Handler[slice.Entry[E]]) error { 119 for i := len(s) - 1; i >= 0; i-- { 120 if err := h(slice.NewEntry(i, s[i])); err != nil { 121 return err 122 } 123 } 124 return nil 125 } 126 } 127 128 func SlicePEntries[E any](s []E) stream.Ranger[slice.Entry[*E]] { 129 return func(h stream.Handler[slice.Entry[*E]]) error { 130 for i := range s { 131 if err := h(slice.NewEntry(i, &s[i])); err != nil { 132 return err 133 } 134 } 135 return nil 136 } 137 } 138 139 func ReverseSlicePEntries[E any](s []E) stream.Ranger[slice.Entry[*E]] { 140 return func(h stream.Handler[slice.Entry[*E]]) error { 141 for i := len(s) - 1; i >= 0; i-- { 142 if err := h(slice.NewEntry(i, &s[i])); err != nil { 143 return err 144 } 145 } 146 return nil 147 } 148 } 149 150 func MapEntries[K comparable, V any](m map[K]V) stream.Ranger[maps.Entry[K, V]] { 151 return func(h stream.Handler[maps.Entry[K, V]]) error { 152 for k, v := range m { 153 if err := h(maps.NewEntry(k, v)); err != nil { 154 return err 155 } 156 } 157 return nil 158 } 159 } 160 161 func Keys[K comparable, V any](m map[K]V) stream.Ranger[K] { 162 return func(h stream.Handler[K]) error { 163 for k := range m { 164 if err := h(k); err != nil { 165 return err 166 } 167 } 168 return nil 169 } 170 } 171 172 func Values[K comparable, V any](m map[K]V) stream.Ranger[V] { 173 return func(h stream.Handler[V]) error { 174 for _, v := range m { 175 if err := h(v); err != nil { 176 return err 177 } 178 } 179 return nil 180 } 181 } 182 183 func Chan[O any](ch chan O) stream.Ranger[O] { 184 return func(h stream.Handler[O]) error { 185 for o := range ch { 186 if err := h(o); err != nil { 187 return err 188 } 189 } 190 return nil 191 } 192 } 193 194 func ChanCaches[O any](ch chan O) stream.Ranger[O] { 195 return func(h stream.Handler[O]) error { 196 for { 197 select { 198 case o, ok := <-ch: 199 if !ok { 200 return nil 201 } else if err := h(o); err != nil { 202 return err 203 } 204 default: 205 return nil 206 } 207 } 208 } 209 } 210 211 func Runes(s string) stream.Ranger[rune] { 212 return func(h stream.Handler[rune]) error { 213 for _, r := range s { 214 if err := h(r); err != nil { 215 return err 216 } 217 } 218 return nil 219 } 220 } 221 222 func RuneEntries(s string) stream.Ranger[strings.RuneEntry] { 223 return func(h stream.Handler[strings.RuneEntry]) error { 224 for i, r := range s { 225 if err := h(strings.NewRuneEntry(i, r)); err != nil { 226 return err 227 } 228 } 229 return nil 230 } 231 } 232 233 func Chars(s string) stream.Ranger[byte] { 234 return func(h stream.Handler[byte]) error { 235 for i := 0; i < len(s); i++ { 236 if err := h(s[i]); err != nil { 237 return err 238 } 239 } 240 return nil 241 } 242 } 243 244 func CharEntries(s string) stream.Ranger[strings.CharEntry] { 245 return func(h stream.Handler[strings.CharEntry]) error { 246 for i := 0; i < len(s); i++ { 247 if err := h(strings.NewCharEntry(i, s[i])); err != nil { 248 return err 249 } 250 } 251 return nil 252 } 253 } 254 255 func Iter[V any](iter stream.Iter[V]) stream.Ranger[V] { 256 return func(h stream.Handler[V]) error { 257 for { 258 if v, ok := iter(); ok { 259 if err := h(v); err != nil { 260 return err 261 } 262 } else { 263 return nil 264 } 265 } 266 } 267 } 268 269 func Handle[V any](ranger stream.Ranger[V], h stream.Handler[V], eh stream.ErrorHandler[V]) stream.Ranger[V] { 270 return func(handler stream.Handler[V]) error { 271 return ranger(func(v V) error { 272 if err := h(v); err != nil { 273 if eh != nil { 274 return eh(v, err) 275 } 276 return err 277 } 278 return handler(v) 279 }) 280 } 281 } 282 283 func HandleMap[I any, O any](ranger stream.Ranger[I], hm stream.HandlerMapper[I, O], eh stream.ErrorHandler[I]) stream.Ranger[O] { 284 return func(handler stream.Handler[O]) error { 285 return ranger(func(i I) error { 286 o, err := hm(i) 287 if err != nil { 288 if eh != nil { 289 return eh(i, err) 290 } 291 return err 292 } 293 return handler(o) 294 }) 295 } 296 } 297 298 func Map[I, O any](ranger stream.Ranger[I], mapper stream.Mapper[I, O]) stream.Ranger[O] { 299 return func(h stream.Handler[O]) error { 300 return ranger(func(i I) error { 301 return h(mapper(i)) 302 }) 303 } 304 } 305 306 func FlatMap[I, O any](ranger stream.Ranger[I], mapper stream.Mapper[I, stream.Ranger[O]]) stream.Ranger[O] { 307 return func(h stream.Handler[O]) error { 308 return ranger(func(i I) error { 309 return mapper(i)(func(o O) error { 310 return h(o) 311 }) 312 }) 313 } 314 } 315 316 func Filter[V any](ranger stream.Ranger[V], filter stream.Filter[V]) stream.Ranger[V] { 317 return func(h stream.Handler[V]) error { 318 return ranger(func(v V) error { 319 if filter(v) { 320 return h(v) 321 } 322 return nil 323 }) 324 } 325 } 326 327 func Reject[V any](ranger stream.Ranger[V], reject stream.Filter[V]) stream.Ranger[V] { 328 return func(h stream.Handler[V]) error { 329 return ranger(func(v V) error { 330 if !reject(v) { 331 return h(v) 332 } 333 return nil 334 }) 335 } 336 } 337 338 func Limit[V any, I generic.Integer](limit I, ranger stream.Ranger[V]) stream.Ranger[V] { 339 var count I 340 return func(h stream.Handler[V]) error { 341 if err := ranger(func(v V) error { 342 if count < limit { 343 count++ 344 return h(v) 345 } 346 return EOF 347 }); err != nil { 348 if err == EOF { 349 return nil 350 } 351 return err 352 } 353 return nil 354 } 355 } 356 357 func Skip[V any, I generic.Integer](skip I, ranger stream.Ranger[V]) stream.Ranger[V] { 358 var count I 359 return func(h stream.Handler[V]) error { 360 return ranger(func(v V) error { 361 if count < skip { 362 count++ 363 return nil 364 } 365 return h(v) 366 }) 367 } 368 } 369 370 func Unique[V comparable](ranger stream.Ranger[V]) stream.Ranger[V] { 371 set := make(map[V]struct{}) 372 return func(h stream.Handler[V]) error { 373 return ranger(func(v V) error { 374 if _, has := set[v]; !has { 375 set[v] = struct{}{} 376 return h(v) 377 } 378 return nil 379 }) 380 } 381 } 382 383 func Concat[V any](rangers ...stream.Ranger[V]) stream.Ranger[V] { 384 return func(h stream.Handler[V]) error { 385 for _, ranger := range rangers { 386 if err := ranger(func(v V) error { 387 return h(v) 388 }); err != nil { 389 return err 390 } 391 } 392 return nil 393 } 394 }