-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
130 lines (109 loc) · 2.25 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
package gg
type Iterator[T any] func() (item T, ok bool)
type Fn[T, R any] func(T) R
type Accumulator[U, T any] func(U, T) U
type KeyValue[K comparable, V any] struct {
Key K
Value V
}
type Stream[T any] struct {
Iterate func() Iterator[T]
}
func FromSlice[T any](src []T) Stream[T] {
srcLen := len(src)
return Stream[T]{
Iterate: func() Iterator[T] {
index := 0
return func() (item T, ok bool) {
ok = index < srcLen
if ok {
item = src[index]
index++
}
return
}
},
}
}
func FromMap[K comparable, V any](src map[K]V) Stream[KeyValue[K, V]] {
return Stream[KeyValue[K, V]]{
Iterate: func() Iterator[KeyValue[K, V]] {
var keys []K
for k, _ := range src {
keys = append(keys, k)
}
srcLen := len(keys)
index := 0
return func() (item KeyValue[K, V], ok bool) {
ok = index < srcLen
if ok {
item = KeyValue[K, V]{
Key: keys[index],
Value: src[keys[index]],
}
index++
}
return
}
},
}
}
func Map[T, R any](fn Fn[T, R], s Stream[T]) Stream[R] {
return Stream[R]{
Iterate: func() Iterator[R] {
next := s.Iterate()
return func() (item R, ok bool) {
it, ok := next()
if ok {
item = fn(it)
}
return
}
},
}
}
func Filter[T any](predicate func(T) bool, s Stream[T]) Stream[T] {
return Stream[T]{
Iterate: func() Iterator[T] {
next := s.Iterate()
return func() (item T, ok bool) {
for item, ok = next(); ok; item, ok = next() {
if predicate(item) {
return
}
}
return
}
},
}
}
func Reduce[U, T any](seed U, accumulator Accumulator[U, T], s Stream[T]) U {
next := s.Iterate()
result := seed
for current, ok := next(); ok; current, ok = next() {
result = accumulator(result, current)
}
return result
}
func (s Stream[T]) ForEach(action func(int, T)) {
next := s.Iterate()
index := 0
for item, ok := next(); ok; item, ok = next() {
action(index, item)
index++
}
}
func ToSlice[T any](s Stream[T]) []T {
var result []T
s.ForEach(func(_ int, it T) {
result = append(result, it)
})
return result
}
func ToMap[K comparable, V any](s Stream[KeyValue[K, V]]) map[K]V {
result := map[K]V{}
s.ForEach(func(_ int, kv KeyValue[K, V]) {
result[kv.Key] = kv.Value
})
return result
}