github.com/searKing/golang/go@v1.2.74/container/slice/stream.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package slice 6 7 import ( 8 "github.com/searKing/golang/go/util/optional" 9 ) 10 11 type Stream struct { 12 s interface{} 13 } 14 15 func NewStream() *Stream { 16 return &Stream{} 17 } 18 func (stream *Stream) WithSlice(s interface{}) *Stream { 19 stream.s = s 20 return stream 21 } 22 23 func (stream *Stream) Value() interface{} { 24 return stream.s 25 } 26 27 func (stream *Stream) Filter(f func(interface{}) bool) *Stream { 28 return stream.WithSlice(FilterFunc(stream.s, f)) 29 } 30 31 func (stream *Stream) Map(f func(interface{}) interface{}) *Stream { 32 return stream.WithSlice(MapFunc(stream.s, f)) 33 } 34 35 func (stream *Stream) Distinct(f func(interface{}, interface{}) int) *Stream { 36 return stream.WithSlice(DistinctFunc(stream.s, f)) 37 } 38 39 func (stream *Stream) Sorted(f func(interface{}, interface{}) int) *Stream { 40 return stream.WithSlice(SortedFunc(stream.s, f)) 41 } 42 43 func (stream *Stream) Peek(f func(interface{})) *Stream { 44 return stream.WithSlice(PeekFunc(stream.s, f)) 45 } 46 47 func (stream *Stream) Limit(maxSize int) *Stream { 48 return stream.WithSlice(LimitFunc(stream.s, maxSize)) 49 } 50 51 func (stream *Stream) Skip(n int) *Stream { 52 return stream.WithSlice(SkipFunc(stream.s, n)) 53 } 54 55 func (stream *Stream) TakeWhile(f func(interface{}) bool) *Stream { 56 return stream.WithSlice(TakeWhileFunc(stream.s, f)) 57 } 58 59 func (stream *Stream) TakeUntil(f func(interface{}) bool) *Stream { 60 return stream.WithSlice(TakeUntilFunc(stream.s, f)) 61 } 62 63 func (stream *Stream) DropWhile(f func(interface{}) bool) *Stream { 64 return stream.WithSlice(DropWhileFunc(stream.s, f)) 65 } 66 67 func (stream *Stream) DropUntil(f func(interface{}) bool) *Stream { 68 return stream.WithSlice(DropUntilFunc(stream.s, f)) 69 } 70 71 func (stream *Stream) ForEach(f func(interface{})) { 72 ForEachFunc(stream.s, f) 73 } 74 75 func (stream *Stream) ForEachOrdered(f func(interface{})) { 76 ForEachOrderedFunc(stream.s, f) 77 } 78 79 func (stream *Stream) ToSlice(ifStringAsRune ...bool) interface{} { 80 return ToSliceFunc(stream.s) 81 } 82 83 func (stream *Stream) Reduce(f func(left, right interface{}) interface{}) *optional.Optional { 84 return optional.OfNillable(ReduceFunc(stream.s, f)) 85 } 86 87 func (stream *Stream) Min(f func(interface{}, interface{}) int) *optional.Optional { 88 return optional.OfNillable(MinFunc(stream.s, f)) 89 } 90 91 func (stream *Stream) Max(f func(interface{}, interface{}) int) *optional.Optional { 92 return optional.OfNillable(MaxFunc(stream.s, f)) 93 } 94 95 func (stream *Stream) Count(ifStringAsRune ...bool) int { 96 return CountFunc(stream.s) 97 } 98 99 func (stream *Stream) AnyMatch(f func(interface{}) bool) bool { 100 return AnyMatchFunc(stream.s, f) 101 } 102 103 func (stream *Stream) AllMatch(f func(interface{}) bool) bool { 104 return AllMatchFunc(stream.s, f) 105 } 106 107 func (stream *Stream) NoneMatch(f func(interface{}) bool) bool { 108 return NoneMatchFunc(stream.s, f) 109 } 110 111 func (stream *Stream) FindFirst(f func(interface{}) bool) *optional.Optional { 112 return optional.OfNillable(FindFirstFunc(stream.s, f)) 113 } 114 115 func (stream *Stream) FindFirstIndex(f func(interface{}) bool) int { 116 return FindFirstIndexFunc(stream.s, f) 117 } 118 119 func (stream *Stream) FindAny(f func(interface{}) bool) *optional.Optional { 120 return optional.OfNillable(FindAnyFunc(stream.s, f)) 121 } 122 123 func (stream *Stream) FindAnyIndex(f func(interface{}) bool) int { 124 return FindAnyIndexFunc(stream.s, f) 125 } 126 127 func (stream *Stream) Empty(ifStringAsRune ...bool) interface{} { 128 return EmptyFunc(stream.s) 129 } 130 131 func (stream *Stream) Of(ifStringAsRune ...bool) *Stream { 132 return stream.WithSlice(Of(stream.s)) 133 } 134 135 func (stream *Stream) Concat(s2 *Stream) *Stream { 136 return stream.WithSlice(ConcatFunc(stream.s, s2.s)) 137 } 138 func (stream *Stream) ConcatWithValue(v interface{}) *Stream { 139 return stream.Concat(NewStream().WithSlice(v)) 140 } 141 142 //grammar surger for count 143 func (stream *Stream) Size() int { 144 return stream.Count() 145 } 146 147 func (stream *Stream) Length() int { 148 return stream.Count() 149 }