-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
91 lines (83 loc) · 1.62 KB
/
example_test.go
File metadata and controls
91 lines (83 loc) · 1.62 KB
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
package itertools_test
import (
"fmt"
"slices"
"github.com/keep94/itertools"
)
func ExampleEnumerate() {
notesIter := slices.Values([]string{"do", "re", "mi", "fa", "so"})
for i, n := range itertools.Enumerate(notesIter) {
fmt.Println(i, n)
}
// Output:
// 0 do
// 1 re
// 2 mi
// 3 fa
// 4 so
}
func ExampleZip() {
notesIter := slices.Values([]string{"do", "re", "mi", "fa", "so"})
ordinalsIter := slices.Values([]int{1, 2, 3})
for n, o := range itertools.Zip(notesIter, ordinalsIter) {
fmt.Println(n, o)
}
// Output:
// do 1
// re 2
// mi 3
}
func ExampleChain() {
notes := []string{"do", "re", "mi", "fa", "so"}
ordinals := []int{1, 2, 3}
notesIter := slices.Values(notes)
ordinalsIter := itertools.Chain(
slices.Values(ordinals), itertools.CycleValues(0))
for n, o := range itertools.Zip(notesIter, ordinalsIter) {
fmt.Println(n, o)
}
// Output:
// do 1
// re 2
// mi 3
// fa 0
// so 0
}
func ExampleDropWhile() {
seq := slices.Values([]int{1, 2, 3, 4, 5, 1, 2, 3, 4, 5})
f := func(x int) bool { return x < 4 }
for x := range itertools.DropWhile(f, seq) {
fmt.Println(x)
}
// Output:
// 4
// 5
// 1
// 2
// 3
// 4
// 5
}
func ExampleAt() {
seq := slices.Values([]int{10, 13, 16})
fmt.Println(itertools.At(0, seq))
fmt.Println(itertools.At(1, seq))
fmt.Println(itertools.At(2, seq))
fmt.Println(itertools.At(3, seq))
// Output:
// 10 true
// 13 true
// 16 true
// 0 false
}
func ExampleTakeWhile() {
seq := itertools.CycleValues(1, 2, 3, 4, 5)
f := func(x int) bool { return x < 4 }
for x := range itertools.TakeWhile(f, seq) {
fmt.Println(x)
}
// Output:
// 1
// 2
// 3
}