-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathevent-driven.go
208 lines (168 loc) · 3.31 KB
/
event-driven.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
package main
import (
"log"
"math/rand"
"sync"
)
func main() {
generator := func(done <-chan interface{}, upperLimit int) <-chan interface{} {
valueStream := make(chan interface{})
go func() {
defer close(valueStream)
for {
select {
case <-done:
return
case valueStream <- rand.Intn(upperLimit):
}
}
}()
return valueStream
}
take := func(
done <-chan interface{},
valueStream <-chan interface{},
num int,
) <-chan interface{} {
takeStream := make(chan interface{})
go func() {
defer close(takeStream)
for i := 0; i < num; i++ {
select {
case <-done:
return
case takeStream <- <-valueStream:
}
}
}()
return takeStream
}
orDone := func(
done <-chan interface{},
valueStream <-chan interface{},
) <-chan interface{} {
doneStream := make(chan interface{})
go func() {
defer close(doneStream)
for {
select {
case <-done:
return
case val, ok := <-valueStream:
if ok == false {
return
}
select {
case doneStream <- val:
case <-done:
}
}
}
}()
return doneStream
}
copier := func(
done <-chan interface{},
valueStream <-chan interface{},
) (_, _ <-chan interface{}) {
out1 := make(chan interface{})
out2 := make(chan interface{})
go func() {
defer close(out1)
defer close(out2)
for val := range orDone(done, valueStream) {
var out1, out2 = out1, out2
for i := 0; i < 2; i++ {
select {
case <-done:
return
case out1 <- val:
out1 = nil
case out2 <- val:
out2 = nil
}
}
}
}()
return out1, out2
}
filter := func(
done <-chan interface{},
valueStream <-chan interface{},
condition func(interface{}) bool,
) <-chan interface{} {
filterStream := make(chan interface{})
go func() {
defer close(filterStream)
for val := range orDone(done, valueStream) {
if condition(val) {
select {
case <-done:
case filterStream <- val:
}
}
}
}()
return filterStream
}
merger := func(
done <-chan interface{},
channels ...<-chan interface{},
) <-chan interface{} {
mergeStream := make(chan interface{})
var wg sync.WaitGroup
multiplex := func(c <-chan interface{}) {
defer wg.Done()
for i := range c {
select {
case <-done:
return
case mergeStream <- i:
}
}
}
wg.Add(len(channels))
for _, c := range channels {
go multiplex(c)
}
go func() {
defer close(mergeStream)
wg.Wait()
}()
return mergeStream
}
toList := func(
done <-chan interface{},
valueStream <-chan interface{},
) []interface{} {
var list []interface{}
for v := range valueStream {
select {
case <-done:
default:
list = append(list, v)
}
}
return list
}
// FILTERS
isEven := func(val interface{}) bool {
return val.(int)%2 == 0
}
isLess50 := func(val interface{}) bool {
return val.(int) < 50
}
isGreater500 := func(val interface{}) bool {
return val.(int) > 500
}
// START
done := make(chan interface{})
defer close(done)
genPipe := generator(done, 1000)
filterPipe := filter(done, genPipe, isEven)
takePipe := take(done, filterPipe, 50)
a, b := copier(done, takePipe)
c := merger(done, filter(done, a, isLess50), filter(done, b, isGreater500))
output := toList(done, c)
log.Println(output)
}