-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
375 lines (298 loc) · 8.58 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package stream
import (
"bytes"
"encoding/gob"
"github.com/khaosles/giz/slice"
"golang.org/x/exp/constraints"
)
/*
@File: Stream.go
@Author: khaosles
@Time: 2023/8/13 13:38
@Desc:
*/
// A StreamI should implements methods:
//type StreamI[T any] interface {
//
// // part methods of Java Stream Specification.
// Distinct() StreamI[T]
// Filter(predicate func(item T) bool) StreamI[T]
// FlatMap(mapper func(item T) StreamI[T]) StreamI[T]
// Map(mapper func(item T) T) StreamI[T]
// Peek(consumer func(item T)) StreamI[T]
//
// Sorted(less func(a, b T) bool) StreamI[T]
// Max(less func(a, b T) bool) (T, bool)
// Min(less func(a, b T) bool) (T, bool)
//
// Limit(maxSize int) StreamI[T]
// Skip(n int) StreamI[T]
//
// AllMatch(predicate func(item T) bool) bool
// AnyMatch(predicate func(item T) bool) bool
// NoneMatch(predicate func(item T) bool) bool
// ForEach(consumer func(item T))
// Reduce(init T, accumulator func(a, b T) T) T
// Count() int
//
// FindFirst() (T, bool)
//
// ToSlice() []T
//
// // part of methods custom extension
// Reverse() StreamI[T]
// Range(start, end int) StreamI[T]
// Concat(Streams ...StreamI[T]) StreamI[T]
//}
type Stream[T any] struct {
source []T
}
// Of creates a Stream whose elements are the specified values.
func Of[T any](elems ...T) Stream[T] {
return FromSlice(elems)
}
// Generate Stream where each element is generated by the provided generater function
func Generate[T any](generator func() func() (item T, ok bool)) Stream[T] {
source := make([]T, 0)
var zeroValue T
for next, item, ok := generator(), zeroValue, true; ok; {
item, ok = next()
if ok {
source = append(source, item)
}
}
return FromSlice(source)
}
// FromSlice creates Stream from slice.
func FromSlice[T any](source []T) Stream[T] {
return Stream[T]{source: source}
}
// FromChannel creates Stream from channel.
func FromChannel[T any](source <-chan T) Stream[T] {
s := make([]T, 0)
for v := range source {
s = append(s, v)
}
return FromSlice(s)
}
// FromRange creates a number Stream from start to end. both start and end are included. [start, end]
func FromRange[T constraints.Integer | constraints.Float](start, end, step T) Stream[T] {
if end < start {
panic("Stream.FromRange: param start should be before param end")
} else if step <= 0 {
panic("Stream.FromRange: param step should be positive")
}
l := int((end-start)/step) + 1
source := make([]T, l)
for i := 0; i < l; i++ {
source[i] = start + (T(i) * step)
}
return FromSlice(source)
}
// Concat creates a lazily concatenated Stream whose elements are all the elements of the first Stream followed by all the elements of the second Stream.
func Concat[T any](a, b Stream[T]) Stream[T] {
source := make([]T, 0)
source = append(source, a.source...)
source = append(source, b.source...)
return FromSlice(source)
}
// Distinct returns a Stream that removes the duplicated items.
func (s Stream[T]) Distinct() Stream[T] {
source := make([]T, 0)
distinct := map[string]bool{}
for _, v := range s.source {
k := hashKey(v)
if _, ok := distinct[k]; !ok {
distinct[k] = true
source = append(source, v)
}
}
return FromSlice(source)
}
func hashKey(data any) string {
buffer := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buffer)
err := encoder.Encode(data)
if err != nil {
panic("Stream.hashKey: get hashkey failed")
}
return buffer.String()
}
// Filter returns a Stream consisting of the elements of this Stream that match the given predicate.
func (s Stream[T]) Filter(predicate func(item T) bool) Stream[T] {
source := make([]T, 0)
for _, v := range s.source {
if predicate(v) {
source = append(source, v)
}
}
return FromSlice(source)
}
// Map returns a Stream consisting of the elements of this Stream that apply the given function to elements of Stream.
func (s Stream[T]) Map(mapper func(item T) T) Stream[T] {
source := make([]T, s.Count())
for i, v := range s.source {
source[i] = mapper(v)
}
return FromSlice(source)
}
// Peek returns a Stream consisting of the elements of this Stream, additionally performing the provided action on each element as elements are consumed from the resulting Stream.
func (s Stream[T]) Peek(consumer func(item T)) Stream[T] {
for _, v := range s.source {
consumer(v)
}
return s
}
// Skip returns a Stream consisting of the remaining elements of this Stream after discarding the first n elements of the Stream.
// If this Stream contains fewer than n elements then an empty Stream will be returned.
func (s Stream[T]) Skip(n int) Stream[T] {
if n <= 0 {
return s
}
source := make([]T, 0)
l := len(s.source)
if n > l {
return FromSlice(source)
}
for i := n; i < l; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// Limit returns a Stream consisting of the elements of this Stream, truncated to be no longer than maxSize in length.
func (s Stream[T]) Limit(maxSize int) Stream[T] {
if s.source == nil {
return s
}
if maxSize < 0 {
return FromSlice([]T{})
}
source := make([]T, 0, maxSize)
for i := 0; i < len(s.source) && i < maxSize; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// AllMatch returns whether all elements of this Stream match the provided predicate.
func (s Stream[T]) AllMatch(predicate func(item T) bool) bool {
for _, v := range s.source {
if !predicate(v) {
return false
}
}
return true
}
// AnyMatch returns whether any elements of this Stream match the provided predicate.
func (s Stream[T]) AnyMatch(predicate func(item T) bool) bool {
for _, v := range s.source {
if predicate(v) {
return true
}
}
return false
}
// NoneMatch returns whether no elements of this Stream match the provided predicate.
func (s Stream[T]) NoneMatch(predicate func(item T) bool) bool {
return !s.AnyMatch(predicate)
}
// ForEach performs an action for each element of this Stream.
func (s Stream[T]) ForEach(action func(item T)) {
for _, v := range s.source {
action(v)
}
}
// Reduce performs a reduction on the elements of this Stream, using an associative accumulation function, and returns an Optional describing the reduced value, if any.
func (s Stream[T]) Reduce(initial T, accumulator func(a, b T) T) T {
for _, v := range s.source {
initial = accumulator(initial, v)
}
return initial
}
// Count returns the count of elements in the Stream.
func (s Stream[T]) Count() int {
return len(s.source)
}
// FindFirst returns the first element of this Stream and true, or zero value and false if the Stream is empty.
func (s Stream[T]) FindFirst() (T, bool) {
var result T
if s.source == nil || len(s.source) == 0 {
return result, false
}
return s.source[0], true
}
// FindLast returns the last element of this Stream and true, or zero value and false if the Stream is empty.
func (s Stream[T]) FindLast() (T, bool) {
var result T
if s.source == nil || len(s.source) == 0 {
return result, false
}
return s.source[len(s.source)-1], true
}
// Reverse returns a Stream whose elements are reverse order of given Stream.
func (s Stream[T]) Reverse() Stream[T] {
l := len(s.source)
source := make([]T, l)
for i := 0; i < l; i++ {
source[i] = s.source[l-1-i]
}
return FromSlice(source)
}
// Range returns a Stream whose elements are in the range from start(included) to end(excluded) original Stream.
func (s Stream[T]) Range(start, end int) Stream[T] {
if start < 0 {
start = 0
}
if end < 0 {
end = 0
}
if start >= end {
return FromSlice([]T{})
}
source := make([]T, 0)
if end > len(s.source) {
end = len(s.source)
}
for i := start; i < end; i++ {
source = append(source, s.source[i])
}
return FromSlice(source)
}
// Sorted returns a Stream consisting of the elements of this Stream, sorted according to the provided less function.
func (s Stream[T]) Sorted(less func(a, b T) bool) Stream[T] {
source := []T{}
source = append(source, s.source...)
slice.SortBy(source, less)
return FromSlice(source)
}
// Max returns the maximum element of this Stream according to the provided less function.
// less: a > b
func (s Stream[T]) Max(less func(a, b T) bool) (T, bool) {
var max T
if len(s.source) == 0 {
return max, false
}
for i, v := range s.source {
if less(v, max) || i == 0 {
max = v
}
}
return max, true
}
// Min returns the minimum element of this Stream according to the provided less function.
// less: a < b
func (s Stream[T]) Min(less func(a, b T) bool) (T, bool) {
var min T
if len(s.source) == 0 {
return min, false
}
for i, v := range s.source {
if less(v, min) || i == 0 {
min = v
}
}
return min, true
}
// ToSlice return the elements in the Stream.
func (s Stream[T]) ToSlice() []T {
return s.source
}