-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (62 loc) · 1.24 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
package main
import "fmt"
// main demonstates a pipelining example with 4
// seperate stages.
//
// A generation stage
// A Doubling stage
// A multiplication stage
// A bitwise operating stage
//
// because they share channel types, we can easily compose them.
func main() {
final := stageThree(stageTwo(stageOne(generator())))
for v := range final {
fmt.Println(v)
}
}
// generator yields the values 1->1,000,000
// it returns a channel to consume its values
func generator() <-chan int {
out := make(chan int)
go func() {
defer close(out)
for i := range 1_000_000 {
out <- i
}
}()
return out
}
// stageOne doubles the numbers from the input stream.
func stageOne(upstream <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for in := range upstream {
out <- in * 2
}
}()
return out
}
// stageTwo multiples the number by 10
func stageTwo(upstream <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for in := range upstream {
out <- in * 10
}
}()
return out
}
// stageThree bit shifts the result
func stageThree(upstream <-chan int) <-chan int {
out := make(chan int)
go func() {
defer close(out)
for in := range upstream {
out <- in << 1
}
}()
return out
}