-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.go
60 lines (52 loc) · 1.32 KB
/
pipeline.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
package main
import (
"fmt"
"strings"
)
func sourceGopher(downstream chan string) {
for _, v := range []string{"hello world", "hello world", "a bad apple", "goodbye all"} {
downstream <- v
}
close(downstream)
}
func filterGopher(upstream, downstream chan string) {
for item := range upstream {
if !strings.Contains(item, "bad") {
downstream <- item
}
}
close(downstream)
}
func removeDuplicate(upstream, downstream chan string) {
pushed := make(map[string]bool)
for item := range upstream {
if !pushed[item] {
pushed[item] = true
downstream <- item
}
}
close(downstream)
}
func splitWords(words string, downstream chan string) {
for _, word := range strings.Fields(words) {
downstream <- word
}
close(downstream)
}
func printGopher(upstream chan string) {
for v := range upstream {
fmt.Println(v)
}
}
func main() {
c0 := make(chan string)
c1 := make(chan string)
c2 := make(chan string)
go sourceGopher(c0)
go filterGopher(c0, c1)
go removeDuplicate(c1, c2)
printGopher(c2)
c3 := make(chan string)
go splitWords("Sometimes it’s easier to operate on words than on sentences. Write a pipeline element that takes strings, splits them up into words (you can use the Fields function from the strings package), and sends all the words, one by one, to the next pipeline stage.", c3)
printGopher(c3)
}