forked from creachadair/mds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
125 lines (108 loc) · 2.62 KB
/
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
package slice_test
import (
"fmt"
"strings"
"github.com/creachadair/mds/slice"
)
func isOdd(v int) bool { return v%2 == 1 }
func isEven(v int) bool { return v%2 == 0 }
func ExamplePartition() {
vs := []int{3, 1, 8, 4, 2, 6, 9, 10, 5, 7}
odd := slice.Partition(vs, isOdd)
fmt.Println(odd)
// Output:
// [3 1 9 5 7]
}
func ExampleSplit() {
vs := []int{1, 2, 3, 4, 5, 6, 7, 8}
fmt.Println("input:", vs)
first3, tail := slice.Split(vs, 3)
head, last3 := slice.Split(vs, -3)
fmt.Println("first3:", first3, "tail:", tail)
fmt.Println("last3:", last3, "head:", head)
// Output:
// input: [1 2 3 4 5 6 7 8]
// first3: [1 2 3] tail: [4 5 6 7 8]
// last3: [6 7 8] head: [1 2 3 4 5]
}
func ExampleMatchingKeys() {
vs := map[string]int{"red": 3, "yellow": 6, "blue": 4, "green": 5}
keys := slice.MatchingKeys(vs, isEven)
for _, key := range keys {
fmt.Println(key, vs[key])
}
// Unordered output:
// blue 4
// yellow 6
}
func ExampleRotate() {
vs := []int{8, 6, 7, 5, 3, 0, 9}
slice.Rotate(vs, -3)
fmt.Println(vs)
// Output:
// [5 3 0 9 8 6 7]
}
func ExampleChunks() {
vs := strings.Fields("my heart is a fish hiding in the water grass")
for _, c := range slice.Chunks(vs, 3) {
fmt.Println(c)
}
// Output:
// [my heart is]
// [a fish hiding]
// [in the water]
// [grass]
}
func ExampleBatches() {
vs := strings.Fields("the freckles in our eyes are mirror images that when we kiss are perfectly aligned")
for _, b := range slice.Batches(vs, 4) {
fmt.Println(b)
}
// Output:
// [the freckles in our]
// [eyes are mirror images]
// [that when we kiss]
// [are perfectly aligned]
}
func ExampleLCS() {
fmt.Println(slice.LCS(
[]int{1, 0, 3, 4, 2, 7, 9, 9},
[]int{1, 3, 5, 7, 9, 11},
))
// Output:
// [1 3 7 9]
}
func ExampleEditScript() {
lhs := strings.Fields("if you mix red with green you get blue")
rhs := strings.Fields("red mixed with green does not give blue at all")
fmt.Println("start", lhs)
var out []string
for _, e := range slice.EditScript(lhs, rhs) {
switch e.Op {
case slice.OpDrop:
fmt.Println("drop", e.X)
case slice.OpEmit:
fmt.Println("emit", e.X)
out = append(out, e.X...)
case slice.OpCopy:
fmt.Println("copy", e.Y)
out = append(out, e.Y...)
case slice.OpReplace:
fmt.Println("replace", e.X, "with", e.Y)
out = append(out, e.Y...)
default:
panic("invalid")
}
}
fmt.Println("end", out)
// Output:
// start [if you mix red with green you get blue]
// drop [if you mix]
// emit [red]
// copy [mixed]
// emit [with green]
// replace [you get] with [does not give]
// emit [blue]
// copy [at all]
// end [red mixed with green does not give blue at all]
}