gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/stream/iter/iter.go (about)

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