-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmain.go
41 lines (35 loc) · 780 Bytes
/
main.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
package main
import (
"context"
"fmt"
"github.com/reactivex/rxgo/v2"
"math/rand"
"time"
)
func main() {
producerFunc := func(ctx context.Context, next chan<- rxgo.Item) {
for _, i := range []int{1, 2} {
next <- rxgo.Item{
V: i,
}
}
}
mapFunc := func(_ context.Context, i interface{}) (interface{}, error) {
time.Sleep(time.Duration(rand.Int31()))
return i.(int)*2 + 1, nil
}
reducerFunc := func(_ context.Context, acc interface{}, elem interface{}) (interface{}, error) {
if acc == nil {
return elem, nil
}
return acc.(int) + elem.(int), nil
}
observable := rxgo.Create([]rxgo.Producer{
producerFunc,
}).
Map(mapFunc, rxgo.WithCPUPool()).
Reduce(reducerFunc)
for item := range observable.Observe() {
fmt.Println(item.V)
}
}