-
Notifications
You must be signed in to change notification settings - Fork 11
/
example_test.go
539 lines (523 loc) · 11.7 KB
/
example_test.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package stream_test
import (
"fmt"
"reflect"
"sort"
"testing"
"github.com/youthlin/stream"
"github.com/youthlin/stream/types"
)
func ExampleSlice() {
var ints = []int{1, 2, 3}
fmt.Printf("%#v\n", stream.Slice(ints))
var str = []string{"abc", "###"}
fmt.Printf("%#v\n", stream.Slice(str))
// Output:
// []types.T{1, 2, 3}
// []types.T{"abc", "###"}
}
func ExampleEntries() {
var m1 = map[int]string{
1: "a",
2: "b",
3: "c",
}
entries := stream.Entries(m1)
sort.Slice(entries, func(i, j int) bool {
return entries[i].First.(int) < entries[j].First.(int)
})
fmt.Printf("%v\n", entries)
stream.Of(stream.Slice(entries)...).ReduceWith(map[string]int{}, func(acc types.R, e types.T) types.R {
pair := e.(types.Pair)
(acc.(map[string]int))[pair.Second.(string)] = pair.First.(int)
return acc
})
// Output:
// [{1 a} {2 b} {3 c}]
}
func ExampleOf() {
fmt.Println(stream.Of().Count())
fmt.Println(stream.Of(1).Count())
fmt.Println(stream.Of("a", "b").Count())
var s = []int{1, 2, 3, 4}
stream.Of(stream.Slice(s)...).ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 0
// 1
// 2
// 1,2,3,4,
}
func ExampleOfInts() {
var ints = []int{1, 2, 3, 4}
stream.OfInts(ints...).ForEach(func(e types.T) {
fmt.Printf("%d,", e)
})
// Output:
// 1,2,3,4,
}
func ExampleOfInt64s() {
var ints = []int64{1, 2, 3, 4}
stream.OfInt64s(ints...).ForEach(func(e types.T) {
fmt.Printf("%d(%T),", e, e)
})
// Output:
// 1(int64),2(int64),3(int64),4(int64),
}
func ExampleOfFloat32s() {
var ints = []float32{1, 2, 3, 4}
stream.OfFloat32s(ints...).ForEach(func(e types.T) {
fmt.Printf("%v(%T),", e, e)
})
// Output:
// 1(float32),2(float32),3(float32),4(float32),
}
func ExampleOfFloat64s() {
var ints = []float64{1, 2, 3, 4}
stream.OfFloat64s(ints...).ForEach(func(e types.T) {
fmt.Printf("%v(%T),", e, e)
})
// Output:
// 1(float64),2(float64),3(float64),4(float64),
}
func ExampleOfStrings() {
var ints = []string{"a", "b", "c"}
stream.OfStrings(ints...).ForEach(func(e types.T) {
fmt.Printf("%v(%T),", e, e)
})
// Output:
// a(string),b(string),c(string),
}
func ExampleOfSlice() {
var intArr = []int{1, 2, 3, 4}
stream.OfSlice(intArr).ForEach(func(e types.T) {
fmt.Printf("%d,", e)
})
var nilArr []int
stream.OfSlice(nilArr).ForEach(func(e types.T) {
fmt.Printf("should not print")
})
var strArr = []string{"a", "b"}
stream.OfSlice(strArr).
Map(func(e types.T) types.R {
return fmt.Sprintf("<%s>", e)
}).
ForEach(func(e types.T) {
fmt.Printf("%s,", e)
})
// Output:
// 1,2,3,4,<a>,<b>,
}
func ExampleOfMap() {
var m1 = map[int]string{
3: "c",
2: "b",
1: "a",
}
s := stream.OfMap(m1).
Map(func(e types.T) types.R {
p := e.(types.Pair)
p.First, p.Second = p.Second, p.First
return p
}).
Sorted(func(left types.T, right types.T) int {
p1 := left.(types.Pair)
p2 := right.(types.Pair)
return p1.Second.(int) - p2.Second.(int)
}).
ToSlice()
fmt.Println(s)
stream.OfMap(nil).ForEach(func(e types.T) {
fmt.Println("not print")
})
// Output:
// [{a 1} {b 2} {c 3}]
}
func ExampleIterate() {
// 0 1 1 2 3 5 8
// | | next
// | \curr(start)
// \prev
var prev = 0
stream.Iterate(1, func(t types.T) types.T {
curr := t.(int)
next := prev + curr
prev = curr
return next
}).
Limit(6).
ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 1,1,2,3,5,8,
}
func ExampleGenerate() {
// fibonacci 斐波那契数列 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368
var fibonacci = func() types.Supplier {
// -1 1 【0】 1 1 2 3
a := -1
b := 1
return func() types.T {
n := a + b
a = b
b = n
return n
}
}
stream.Generate(fibonacci()).Limit(20).ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,
}
func ExampleRepeat() {
count := stream.Repeat("a").Peek(func(t types.T) {
fmt.Printf("%s", t)
}).Limit(10).Count()
fmt.Printf("\n%d\n", count)
// Output:
// aaaaaaaaaa
// 10
}
func ExampleRepeatN() {
stream.RepeatN(1.0, 3).ForEach(func(t types.T) {
fmt.Printf("<%T,%v>", t, t)
})
// Output:
// <float64,1><float64,1><float64,1>
}
func ExampleIntRange() {
stream.IntRange(0, 5).
ForEach(func(t types.T) {
fmt.Println(t)
})
// Output:
// 0
// 1
// 2
// 3
// 4
}
func ExampleIntRangeStep() {
stream.IntRangeStep(0, 10, 2).ForEach(func(t types.T) {
fmt.Println(t)
})
// Output:
// 0
// 2
// 4
// 6
// 8
}
func ExampleIntRangeStep_negStep() {
stream.IntRangeStep(5, 0, -1).ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 5,4,3,2,1,
}
func ExampleIntRangeStep_zeroStep() {
stream.IntRangeStep(0, 5, 0).
Limit(10).ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 0,0,0,0,0,0,0,0,0,0,
}
func ExampleInt64Range() {
stream.Int64Range(int64(0), int64(10)).ForEach(func(t types.T) {
fmt.Printf("%d", t)
})
// Output:
// 0123456789
}
func ExampleInt64RangeStep() {
stream.Int64RangeStep(int64(0), int64(10), 3).ForEach(func(t types.T) {
fmt.Printf("%d", t)
})
// Output:
// 0369
}
func ExampleStream_Filter() {
stream.Of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9).
Filter(func(e types.T) bool {
return e.(int)%3 == 0
}).
ForEach(func(e types.T) {
fmt.Println(e)
})
// Output:
// 0
// 3
// 6
// 9
}
func ExampleStream_Map() {
stream.IntRange(0, 5).
Map(func(t types.T) types.R {
return fmt.Sprintf("<%d>", t.(int))
}).
ForEach(func(t types.T) {
fmt.Printf("%v", t)
})
// Output:
// <0><1><2><3><4>
}
func ExampleStream_FlatMap() {
stream.Of([]int{0, 2, 4, 6, 8}, []int{1, 3, 5, 7, 9}).
FlatMap(func(t types.T) stream.Stream {
return stream.Of(stream.Slice(t)...)
}).
ForEach(func(t types.T) {
fmt.Printf("%d", t)
})
// Output:
// 0246813579
}
func ExampleStream_Peek() {
stream.Of(1, 2, 3, 4, 5).Peek(func(t types.T) {
fmt.Printf("%d,", t)
}).Count()
// Output:
// 1,2,3,4,5,
}
func ExampleStream_Peek_peekIsNotTerminalAction() {
stream.Of(1, 2, 3, 4, 5).Peek(func(t types.T) {
fmt.Printf("%d,", t)
})
stream.Of(1, 2, 3, 4, 5).Peek(func(t types.T) {
fmt.Printf("%d,", t)
}).Count()
// Output:
// 1,2,3,4,5,
}
func ExampleStream_Distinct() {
fmt.Println(stream.RepeatN(1, 10).Distinct(func(t types.T) int {
return t.(int)
}).Count())
// Output:
// 1
}
func ExampleStream_Sorted() {
stream.IntRange(1, 10).
Sorted(types.ReverseOrder(types.IntComparator)).
ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 9,8,7,6,5,4,3,2,1,
}
func ExampleStream_Sorted_int64() {
stream.Int64RangeStep(100, 0, -10).
Peek(func(t types.T) {
fmt.Printf("%d,", t)
}).
Sorted(types.Int64Comparator).
ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 100,90,80,70,60,50,40,30,20,10,10,20,30,40,50,60,70,80,90,100,
}
func ExampleStream_Limit() {
fmt.Println(stream.Repeat(nil).Limit(100).Count())
// Output:
// 100
}
func ExampleStream_Skip() {
stream.IntRange(0, 10).Skip(5).ForEach(func(t types.T) {
fmt.Printf("%d,", t)
})
// Output:
// 5,6,7,8,9,
}
func ExampleStream_ForEach() {
stream.Of("hello", "world").ForEach(func(t types.T) {
fmt.Println(t)
})
// Output:
// hello
// world
}
func ExampleStream_ToSlice() {
slice := stream.Of(1, 2, 3).ToSlice()
fmt.Printf("%#v\n", slice)
// Output:
// []types.T{1, 2, 3}
}
func ExampleStream_ToElementSlice() {
slice := stream.Of(1, 2, 3).ToElementSlice(0)
fmt.Printf("%#v\n", slice)
// Output:
// []int{1, 2, 3}
}
func ExampleStream_ToSliceOf() {
slice := stream.Of(1, 2, 3).ToSliceOf(reflect.TypeOf(0))
fmt.Printf("%#v\n", slice)
// Output:
// []int{1, 2, 3}
}
func ExampleStream_AllMatch() {
allMatch := stream.IntRange(0, 10).AllMatch(func(t types.T) bool {
i, ok := t.(int)
return ok && i >= 0 && i < 10
})
fmt.Println(allMatch)
allMatch = stream.IntRange(0, 10).AllMatch(func(t types.T) bool {
i, ok := t.(int)
return ok && i > 0 && i < 10
})
fmt.Println(allMatch)
noElementSoResultShouldBeFalse := stream.Of().AnyMatch(func(t types.T) bool {
return true
})
fmt.Println(noElementSoResultShouldBeFalse)
// Output:
// true
// false
// false
}
func ExampleStream_NoneMatch() {
isStr := func(t types.T) bool {
_, ok := t.(string)
return ok
}
noneIsStr := stream.IntRange(0, 10).NoneMatch(isStr)
fmt.Println(noneIsStr) // true
noneIsStr = stream.Of(1, 2, "a", "b").NoneMatch(isStr)
fmt.Println(noneIsStr) // false
noElementSoResultShouldBeTrue := stream.Of().NoneMatch(func(t types.T) bool {
return true
})
fmt.Println(noElementSoResultShouldBeTrue)
// Output:
// true
// false
// true
}
func ExampleStream_AnyMatch() {
hasStr := stream.Of(0, 1, "a", "b").AnyMatch(func(t types.T) bool {
_, ok := t.(string)
return ok
})
hasInt := stream.Of(0, 1, "a", "b").AnyMatch(func(t types.T) bool {
_, ok := t.(int)
return ok
})
fmt.Println(hasStr, hasInt)
noElementSoResultShouldBeFalse := stream.Of().AnyMatch(func(t types.T) bool {
return true
})
fmt.Println(noElementSoResultShouldBeFalse)
// Output:
// true true
// false
}
func ExampleStream_Reduce() {
fmt.Println(stream.Of().Reduce(func(acc types.T, t types.T) types.T {
return acc
}).IsPresent())
// Output:
// false
}
func ExampleStream_ReduceFrom() {
sum := stream.IntRange(1, 101).ReduceFrom(0, func(acc types.T, t types.T) types.T {
return acc.(int) + t.(int)
})
fmt.Println(sum)
mul := stream.IntRange(1, 5).ReduceFrom(1, func(acc types.T, t types.T) types.T {
return acc.(int) * t.(int)
})
fmt.Println(mul)
// Output:
// 5050
// 24
}
func ExampleStream_ReduceWith() {
slice := stream.IntRange(0, 10).ReduceWith(make([]int, 0, 10), func(acc types.R, t types.T) types.R {
return append(acc.([]int), t.(int))
})
fmt.Printf("%#v", slice)
// Output:
// []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
}
func ExampleStream_ReduceBy() {
ints := stream.IntRange(0, 10).ReduceBy(func(sizeMayNegative int64) types.R {
if sizeMayNegative >= 0 {
return make([]int, 0, sizeMayNegative)
}
fmt.Printf("IntRange: unknown size\n")
return make([]int, 0)
}, func(acc types.R, e types.T) types.R {
result := acc.([]int)
result = append(result, e.(int))
return result
}).([]int)
fmt.Printf("%v\n", ints)
int64s := stream.OfInts(ints...).ReduceBy(func(sizeMayNegative int64) types.R {
if sizeMayNegative >= 0 {
fmt.Printf("size=%d\n", sizeMayNegative)
return make([]int64, 0, sizeMayNegative)
}
return make([]int64, 0)
}, func(acc types.R, e types.T) types.R {
result := acc.([]int64)
result = append(result, int64(e.(int)))
return result
}).([]int64)
fmt.Printf("%v\n", int64s)
// Output:
// IntRange: unknown size
// [0 1 2 3 4 5 6 7 8 9]
// size=10
// [0 1 2 3 4 5 6 7 8 9]
}
func ExampleStream_Count() {
fmt.Println(stream.Of().Count())
fmt.Println(stream.Of(1).Count())
fmt.Println(stream.Of(1, 2).Count())
// Output:
// 0
// 1
// 2
}
type person struct {
name string
age int
}
func TestStruct(t *testing.T) {
stream.Of(&person{
name: "Bob",
age: 18,
}, &person{
name: "Alice",
age: 20,
}).Map(func(e types.T) types.R {
p := e.(*person)
p.age++
t.Logf("%#v->%#v,", e, *p)
return e
}).Sorted(types.ReverseOrder(func(left, right types.T) int {
l := left.(*person)
r := right.(*person)
return l.age - r.age
})).ForEach(func(e types.T) {
t.Log(e)
})
// Output:
// &streams_test.person{name:"Bob", age:19}->streams_test.person{name:"Bob", age:19},
// &streams_test.person{name:"Alice", age:21}->streams_test.person{name:"Alice", age:21},
// &{Alice 21}
// &{Bob 19}
}
func TestToMap(t *testing.T) {
m := stream.IntRange(0, 10).ReduceWith(make(map[int]int), func(acc types.R, t types.T) types.R {
acc.(map[int]int)[t.(int)] = t.(int) * 10
return acc
})
t.Log(m)
// Output:
// map[0:0 1:10 2:20 3:30 4:40 5:50 6:60 7:70 8:80 9:90]
}