-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoc_test.go
350 lines (314 loc) · 7.75 KB
/
doc_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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package iterset
import (
"fmt"
"maps"
"slices"
"strings"
"testing"
)
type item struct{ id string }
func (it item) Id() string { return it.id }
// Intersect a map with a slice of keys, retaining original order.
func Example_intersect() {
data := map[string]int{"a": 0, "b": 1, "c": 2}
keys := []string{"d", "c", "b"}
// With no sets.
for _, key := range keys {
value, ok := data[key]
if ok {
fmt.Println(key, value)
}
}
// A typical `mapset` would copy `data`, and have no impact on readability.
s := Set(slices.Collect(maps.Keys(data))...)
for _, key := range keys {
if s.Contains(key) {
fmt.Println(key, data[key])
}
}
// Using an intersect method would also copy `keys`, and lose ordering.
// Whereas `iterset` methods have in-lined logic with zero copying and lazy iteration.
for key, value := range Cast(data).Intersect(slices.Values(keys)) {
fmt.Println(key, value)
}
// Output:
// c 2
// b 1
// c 2
// b 1
// c 2
// b 1
}
// Is one slice a superset of another?
func Example_superset() {
left, right := []string{"a", "b", "c"}, []string{"b", "c", "d"}
// With no sets.
m := map[string]bool{}
for _, c := range left {
m[c] = true
}
isSuperset := true
for _, c := range right {
if !m[c] {
isSuperset = false
break
}
}
fmt.Println(isSuperset)
// Or in functional style.
fmt.Println(!slices.ContainsFunc(right, func(c string) bool { return !m[c] }))
// A typical `mapset` would copy both slices, which makes early exits irrelevant.
// Or it only solves half the problem.
s := Set(left...)
fmt.Println(!slices.ContainsFunc(right, func(c string) bool { return !s.Contains(c) }))
// Whereas `iterset` methods have in-lined logic with minimal copying and early exits.
fmt.Println(Set(left...).IsSuperset(slices.Values(right)))
// Output:
// false
// false
// false
// false
}
// Remove duplicates, retaining original order.
func Example_unique() {
values := []string{"a", "b", "a"}
// With no sets.
m := map[string]bool{}
keys := []string{}
for _, c := range values {
if !m[c] {
keys = append(keys, c)
}
m[c] = true
}
fmt.Println(keys)
// A typical `mapset` would either have no impact on readability, or lose ordering.
// Whereas `iterset` has this built-in with lazy iteration.
for c := range Unique(slices.Values(values)) {
fmt.Println(c)
}
// What if a `set` is still needed, in addition to ordering.
idx := Index(slices.Values(values))
fmt.Println(idx)
fmt.Println(Sorted(idx)) // keys sorted by value
// Output:
// [a b]
// a
// b
// map[a:0 b:1]
// [a b]
}
func ExampleMapSet_Contains() {
s := Set("b", "a", "b")
fmt.Println(s.Contains("a"), s.Contains("b", "c"))
// Output: true false
}
func ExampleMapSet_Equal() {
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("a", "b").Equal(k), Set("a").Equal(k))
// Output: true false
}
func ExampleMapSet_IsSubset() {
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("a").IsSubset(k), Set("a", "c").IsSubset(k))
// Output: true false
}
func ExampleIsSubset() {
s1 := slices.Values([]string{"a"})
s2 := slices.Values([]string{"a", "b"})
fmt.Println(IsSubset(s1, s2), IsSubset(s2, s1))
// Output: true false
}
func ExampleMapSet_IsSuperset() {
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("a", "b").IsSuperset(k), Set("a").IsSuperset(k))
// Output: true false
}
func ExampleMapSet_IsDisjoint() {
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Set("c").IsDisjoint(k), Set("a").IsDisjoint(k))
// Output: true false
}
func ExampleMapSet_Add() {
s := Set("a", "b")
s.Add("b", "c")
fmt.Println(len(s))
// Output: 3
}
func ExampleMapSet_Delete() {
s := Set("a", "b")
s.Delete("b", "c")
fmt.Println(len(s))
// Output: 1
}
func ExampleMapSet_Remove() {
s := Set("a", "b")
s.Remove(slices.Values([]string{"b", "c"}))
fmt.Println(s)
// Output: map[a:{}]
}
func ExampleMapSet_Toggle() {
s := Set("a", "b")
s.Toggle(maps.All(Set("b", "c")))
fmt.Println(s)
// Output: map[a:{} c:{}]
}
func ExampleMapSet_Union() {
m := map[string]int{"a": 0, "b": 1}
n := map[string]int{"b": 2, "c": 3}
fmt.Println(Cast(m).Union(maps.All(n)))
// Output: map[a:0 b:2 c:3]
}
func ExampleIntersect() {
s1 := slices.Values([]string{"a", "b"})
s2 := slices.Values([]string{"b", "c"})
fmt.Println(slices.Collect(Intersect(s1, s2)))
// Output: [b]
}
func ExampleMapSet_Difference() {
k := slices.Values([]string{"b", "c"})
fmt.Println(maps.Collect(Set("a", "b").Difference(k)))
// Output: map[a:{}]
}
func ExampleDifference() {
s1 := slices.Values([]string{"a", "b"})
s2 := slices.Values([]string{"b", "c"})
fmt.Println(slices.Collect(Difference(s1, s2)))
// Output: [a]
}
func ExampleMapSet_SymmetricDifference() {
k := slices.Values([]string{"b", "c"})
fmt.Println(slices.Collect(Set("a", "b").SymmetricDifference(k)))
// Output: [c a]
}
func ExampleUnique() {
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(slices.Collect(Unique(k)))
// Output: [b a]
}
func ExampleUniqueBy() {
items := []item{{id: "b"}, {id: "a"}, {id: "b"}}
for key, value := range UniqueBy(slices.Values(items), item.Id) {
fmt.Println(key, value)
}
// Output:
// b {b}
// a {a}
}
func ExampleCompact() {
k := slices.Values([]string{"b", "b", "a", "a", "b"})
for key, count := range Compact(k) {
fmt.Println(key, count)
}
// Output:
// b 2
// a 2
// b 1
}
func ExampleCompactBy() {
items := []item{{id: "b"}, {id: "b"}, {id: "a"}, {id: "a"}, {id: "b"}}
for key, values := range CompactBy(slices.Values(items), item.Id) {
fmt.Println(key, values)
}
// Output:
// b [{b} {b}]
// a [{a} {a}]
// b [{b}]
}
func ExampleCollect() {
k := slices.Values([]string{"b", "a", "b"})
fmt.Println(Collect(k, true))
// Output: map[a:true b:true]
}
func ExampleIndex() {
fmt.Println(Index(slices.Values([]string{"b", "a", "b"})))
// Output: map[a:1 b:0]
}
func ExampleCount() {
fmt.Println(Count(slices.Values([]string{"b", "a", "b"})))
// Output: map[a:1 b:2]
}
func ExampleIndexBy() {
items := []item{{id: "b"}, {id: "a"}, {id: "b"}}
fmt.Println(IndexBy(slices.Values(items), item.Id))
// Output: map[a:{a} b:{b}]
}
func ExampleGroupBy() {
items := []item{{id: "b"}, {id: "a"}, {id: "b"}}
fmt.Println(GroupBy(slices.Values(items), item.Id))
// Output: map[a:[{a}] b:[{b} {b}]]
}
func ExampleMemoize() {
fmt.Println(Memoize(slices.Values([]string{"b", "a", "b"}), strings.ToUpper))
// Output: map[a:A b:B]
}
func ExampleSorted() {
fmt.Println(Sorted(Index(slices.Values([]string{"b", "a", "b"}))))
// Output: [b a]
}
func ExampleMin() {
s := Min(map[string]int{"a": 2, "b": 1, "c": 1})
slices.Sort(s)
fmt.Println(s)
// Output: [b c]
}
func ExampleMax() {
s := Max(map[string]int{"a": 2, "b": 2, "c": 1})
slices.Sort(s)
fmt.Println(s)
// Output: [a b]
}
func TestIter(t *testing.T) {
k := slices.Values([]string{"a", "A"})
for range UniqueBy(k, strings.TrimSpace) {
break
}
for range Compact(k) {
break
}
for range CompactBy(k, strings.TrimSpace) {
break
}
for range Set("a").Intersect(k) {
break
}
for c := range Index(k).Difference(k) {
t.Errorf("should be empty: %s", c)
}
for range Set("b").Difference(k) {
break
}
for range Set("b").ReverseDifference(k) {
break
}
for c := range Index(k).SymmetricDifference(k) {
t.Errorf("should be empty: %s", c)
}
for range Set("b").SymmetricDifference(k) {
break
}
for c := range Set("b").SymmetricDifference(k) {
if c == "b" {
break
}
}
}
func TestEmpty(t *testing.T) {
var m MapSet[string, struct{}]
if m.Union() == nil {
t.Error("should not be nil")
}
for range m.Intersect(nil) {
t.Error("should be empty")
}
for range Intersect(nil, maps.Keys(m)) {
t.Error("should be empty")
}
m.Toggle(maps.All(m))
for range m.ReverseDifference(maps.Keys(m)) {
t.Error("should be empty")
}
for range m.SymmetricDifference(maps.Keys(m)) {
t.Error("should be empty")
}
}