-
Notifications
You must be signed in to change notification settings - Fork 5
/
out.go
71 lines (60 loc) · 1.07 KB
/
out.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
package main
import "fmt"
func Elems_string(a ...string) <-chan string {
ch := make(chan string)
go func() {
for _, x := range a {
ch <- x
}
close(ch)
}()
return ch
}
func Elems_int(a ...int) <-chan int {
ch := make(chan int)
go func() {
for _, x := range a {
ch <- x
}
close(ch)
}()
return ch
}
func Map_int_string(in <-chan int, f func(int) string) <-chan string {
out := make(chan string)
go func() {
for x := range in {
out <- f(x)
}
close(out)
}()
return out
}
func Merge_string(chans ...<-chan string) <-chan string {
merged := make(chan string)
done := make(chan bool)
for _, ch := range chans {
ch := ch
go func() {
for x := range ch {
merged <- x
}
done <- true
}()
}
go func() {
for range chans {
<-done
}
close(merged)
}()
return merged
}
func main() {
letters := Elems_string("A", "B", "C", "D", "E")
numbers := Elems_int(1, 2, 3, 4, 5)
everything := Merge_string(letters, Map_int_string(numbers, func(x int) string { return fmt.Sprint(x) }))
for s := range everything {
fmt.Println(s)
}
}