-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxtract_example_test.go
176 lines (155 loc) · 4.27 KB
/
xtract_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
package xtract_test
import (
"fmt"
"github.com/miyamo2/xtract"
"strings"
)
func Example() {
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 100, 101}
xt := xtract.FromSlice(s)
even := xt.ByValue(func(i int) bool { return i%2 == 0 })
odd := xt.ByValue(func(i int) bool { return i%2 != 0 })
fmt.Println("---even---")
for v := range even.Values() {
fmt.Println(v)
}
fmt.Println("---odd---")
for v := range odd.Values() {
fmt.Println(v)
}
evenAndTwoDigits := even.ByValue(func(i int) bool { return i > 9 && i < 100 })
fmt.Println("---even and two digits---")
for v := range evenAndTwoDigits.Values() {
fmt.Println(v)
}
oddAndTwoDigits := odd.ByValue(func(i int) bool { return i > 9 && i < 100 })
fmt.Println("---odd and two digits---")
for v := range oddAndTwoDigits.Values() {
fmt.Println(v)
}
// Output: ---even---
//0
//2
//4
//6
//8
//10
//100
//---odd---
//1
//3
//5
//7
//9
//11
//101
//---even and two digits---
//10
//---odd and two digits---
//11
}
func ExampleSliceExtractor_ByValue() {
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByValue(func(v string) bool { return len(v) < 9 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
//iterator
}
func ExampleSliceExtractor_ByKey() {
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByKey(func(i int) bool { return i > 0 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: iterator
//range over func
}
func ExampleSliceExtractor_ByKeyAndValue() {
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).ByKeyAndValue(func(i int, v string) bool { return i > 1 && len(v) > 6 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: range over func
}
func ExampleSliceExtractor_Limit() {
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).Limit(1)
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
}
func ExampleSliceExtractor_Offset() {
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s).Offset(1)
for v := range xt.Values() {
fmt.Println(v)
}
// Output: iterator
//range over func
}
func ExampleSliceExtractor_KeyAndValues() {
s := []string{"go", "iterator", "range over func"}
xt := xtract.FromSlice(s)
for k, v := range xt.KeyAndValues() {
fmt.Printf("%d: %s\n", k, v)
}
// Output: 0: go
//1: iterator
//2: range over func
}
func ExampleMapExtractor_ByValue() {
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByValue(func(v string) bool { return len(v) < 8 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
}
func ExampleMapExtractor_ByKey() {
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByKey(func(k string) bool { return strings.Contains(k, " ") })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: iterator
}
func ExampleMapExtractor_ByKeyAndValue() {
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByKeyAndValue(func(k, v string) bool { return strings.Contains(k, "e") && len(v) < 8 })
for v := range xt.Values() {
fmt.Println(v)
}
// Output: go
}
func ExampleMapExtractor_Limit() {
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).Limit(1)
values := make([]string, 0, 1)
for v := range xt.Values() {
values = append(values, v)
}
fmt.Println(len(values))
// Output: 1
}
func ExampleMapExtractor_Offset() {
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).Offset(1)
values := make([]string, 0, 1)
for v := range xt.Values() {
values = append(values, v)
}
fmt.Println(len(values))
// Output: 2
}
func ExampleMapExtractor_KeyAndValues() {
m := map[string]string{"language": "go", "design pattern": "iterator", "implementation": "range over func"}
xt := xtract.FromMap(m).ByKey(func(k string) bool { return strings.Contains(k, " ") })
for k, v := range xt.KeyAndValues() {
fmt.Printf("%s: %s\n", k, v)
}
// Output: design pattern: iterator
}