-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter2_example_test.go
228 lines (205 loc) · 4.06 KB
/
iter2_example_test.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package iter2_test
import (
"bufio"
"fmt"
"maps"
"os"
"slices"
"strconv"
"strings"
"time"
"github.com/mkch/iter2"
)
func ExampleZip() {
ks := []int{1, 2, 3}
vs := []string{"one", "two", "three"}
type pair struct {
N int
Str string
}
s := slices.Collect(iter2.Zip(slices.Values(ks), slices.Values(vs), func(i int, s string) pair { return pair{i, s} }))
fmt.Println(s)
// Output: [{1 one} {2 two} {3 three}]
}
func ExampleZip2() {
ks := []int{1, 2, 3}
vs := []string{"one", "two", "three"}
zipped := iter2.Zip2(slices.Values(ks), slices.Values(vs))
m := maps.Collect(zipped)
for k, v := range m {
fmt.Println(k, v)
}
// Unordered output:
// 1 one
// 2 two
// 3 three
}
func ExampleConcat() {
seq1 := slices.Values([]int{1, 2, 3})
seq2 := slices.Values([]int{4, 5})
seq := iter2.Concat(seq1, seq2)
fmt.Println(slices.Collect(seq))
// Output: [1 2 3 4 5]
}
func ExampleMerge() {
seq1 := slices.Values([]int{1, 2, 3})
seq2 := slices.Values([]int{4, 5})
seq := iter2.Merge(seq1, seq2)
for v := range seq {
fmt.Println(v)
}
// Unordered output:
// 1
// 2
// 3
// 4
// 5
}
func ExampleMap() {
iter := slices.Values([]int{1, 2, 3})
seq := iter2.Map(iter, func(v int) string { return strconv.Itoa(v + 1) })
fmt.Printf("%q", slices.Collect(seq))
// Output: ["2" "3" "4"]
}
func ExampleMap2() {
iter := slices.All([]int{1, 2, 3})
seq := iter2.Map2(iter, func(i int, v int) (int, string) { return i + 1, strings.Repeat("a", v) })
for k, v := range seq {
fmt.Printf("%v %q\n", k, v)
}
// Output:
// 1 "a"
// 2 "aa"
// 3 "aaa"
}
func ExampleMap1To2() {
seq1 := slices.Values([]int{1, 2, 3})
seq2 := iter2.Map1To2(seq1, func(v int) (byte, string) { return byte(v), strconv.Itoa(v) })
for k, v := range seq2 {
fmt.Printf("%v %q\n", k, v)
}
// Output:
// 1 "1"
// 2 "2"
// 3 "3"
}
func ExampleKeys() {
seq2 := func(yield func(int, string) bool) {
if !yield(0, "zero") {
return
}
if !yield(1, "one") {
return
}
}
keys := iter2.Keys(seq2)
fmt.Println(slices.Collect(keys))
// Output: [0 1]
}
func ExampleValues() {
seq2 := func(yield func(int, string) bool) {
if !yield(0, "zero") {
return
}
if !yield(1, "one") {
return
}
}
values := iter2.Values(seq2)
fmt.Println(slices.Collect(values))
// Output: [zero one]
}
func ExampleTake() {
seq := slices.Values([]int{1, 2, 3, 4, 5})
seq = iter2.Take(seq, 2)
fmt.Println(slices.Collect(seq))
// Output: [1 2]
}
func ExampleWalkDir() {
dirs := iter2.WalkDir(os.DirFS("testdata"), ".")
for d, err := range dirs {
if err != nil {
continue // continue to ignore the error
}
if d.Path == "should_skip" {
d.SkipDir()
continue
}
fmt.Printf("Walk: %v\n", d.Path)
}
}
func ExamplePush() {
seq, yield, stop := iter2.Push[int]()
defer stop()
time.AfterFunc(time.Millisecond*1, func() {
yield(1)
time.AfterFunc(time.Millisecond*1, func() {
yield(2)
stop()
})
})
for t := range seq {
fmt.Println(t)
}
// Output:
// 1
// 2
}
func ExampleFilter() {
s := []int{1, 2, 3, 4}
even := iter2.Filter(slices.Values(s),
func(n int) bool { return n%2 == 0 })
fmt.Println(slices.Collect(even))
// Output: [2 4]
}
func ExampleFilter2() {
s := []int{1, 2, 3, 4}
even := iter2.Filter2(slices.All(s),
func(i, n int) bool { return n%2 == 0 })
for i, n := range even {
fmt.Println(i, n)
}
// Unordered output:
// 1 2
// 3 4
}
func ExampleJust() {
seq := iter2.Just(1, 2, 3)
fmt.Println(slices.Collect(seq))
// Output: [1 2 3]
}
func ExampleJust2() {
type KV = struct {
K int
V string
}
seq := iter2.Just2(KV{1, "one"}, KV{2, "two"})
for k, v := range seq {
fmt.Println(k, v)
}
// Output:
// 1 one
// 2 two
}
func ExampleTokens() {
r := bufio.NewScanner(strings.NewReader("abc\ndef"))
lines := iter2.Tokens(r)
for line := range lines {
fmt.Printf("%s\n", line)
}
// Output:
// abc
// def
}
func ExampleTokenStrings() {
r := bufio.NewScanner(strings.NewReader("Some words here"))
r.Split(bufio.ScanWords)
lines := iter2.TokenStrings(r)
for line := range lines {
fmt.Printf("%s\n", line)
}
// Output:
// Some
// words
// here
}