generated from devnw/oss-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (71 loc) · 1.31 KB
/
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
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
package main
import (
"fmt"
"math/rand"
)
func main() {
// Create the pipeline
output := MultiplyFloat(Modulus32(SumRand(Rands(15))))
for v := range output {
fmt.Printf("output %v\n", v)
}
}
func Rands(count int) <-chan int {
out := make(chan int)
go func(out chan<- int) {
defer close(out)
for i := 0; i < count; i++ {
value := rand.Int()
fmt.Printf("input %v\n", value)
out <- value
}
}(out)
return out
}
func Modulus32(in <-chan int) <-chan int {
out := make(chan int)
go func(in <-chan int, out chan<- int) {
defer close(out)
for {
data, ok := <-in
if !ok {
return
}
mod := data % 32
fmt.Printf("modulus %v %% 32 == %v\n", data, mod)
out <- mod
}
}(in, out)
return out
}
func SumRand(in <-chan int) <-chan int {
out := make(chan int)
go func(in <-chan int, out chan<- int) {
defer close(out)
for {
data, ok := <-in
if !ok {
return
}
r := rand.Int()
sum := data + r
fmt.Printf("sum %v + %v == %v\n", data, r, sum)
out <- sum
}
}(in, out)
return out
}
func MultiplyFloat(in <-chan int) <-chan float64 {
out := make(chan float64)
go func(in <-chan int, out chan<- float64) {
defer close(out)
for {
data, ok := <-in
if !ok {
return
}
out <- float64(data) * rand.Float64()
}
}(in, out)
return out
}