-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_strings.go
101 lines (77 loc) · 1.59 KB
/
example_strings.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
96
97
98
99
100
101
package main
import (
"fmt"
. "iter_string"
"strings"
)
func words() Chan {
words_array := []string{"The", "quick", "brown", "fox", "jumps", "over", "the", "fence"}
words_chan := make(Chan)
go func() {
defer close(words_chan)
for _, w := range words_array {
words_chan <- w
}
}()
return words_chan
}
func main_string() {
words_chan := words()
// Take
for item := range Take(words_chan, 2) {
fmt.Printf("take : %v\n", item)
}
println(" - - -")
words_chan = words()
// Enumerate
for item := range Enumerate(words_chan) {
fmt.Printf("enumerate : %d, %v\n", item.Index, item.Value)
}
println(" - - -")
// Range
for n := range Range(10) {
fmt.Printf("%v \n", n)
}
println(" - - -")
words_chan = words()
// Filter
for item := range Filter(words_chan, filter_cb) {
fmt.Printf("filtered: %v \n", item)
}
println(" - - - ")
words_chan = words()
// Map (Map and Filter use Map())
for item := range Map(words_chan, map_cb) {
fmt.Printf("Mapped: %v \n", item)
}
println(" - - - ")
words_chan = words()
// Map (Map and Filter use Map())
reduced := Reduce(words_chan, reduce_cb)
fmt.Printf("Reduced: %v \n", reduced)
println(" - - - ")
}
// Callback for Reduce():
func reduce_cb(in Chan) string {
word := ""
for item := range in {
word = word + "#" + item
}
return word
}
// Callback for Filter():
func filter_cb(in Chan, out Chan) Chan {
for word := range in {
if len(word) > 3 {
out <- word
}
}
return out
}
// Callback for Map():
func map_cb(in Chan, out Chan) Chan {
for word := range in {
out <- strings.ToUpper(word)
}
return out
}