-
Notifications
You must be signed in to change notification settings - Fork 2
/
query_examples_test.go
361 lines (310 loc) · 7.33 KB
/
query_examples_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
351
352
353
354
355
356
357
358
359
360
361
package collection_test
import (
"context"
"fmt"
"sync"
"github.com/marstr/collection/v2"
)
func ExampleEnumerableSlice_Enumerate() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// When a single value is provided, and it is an array or slice, each value in the array or slice is treated as an enumerable value.
originalInts := []int{1, 2, 3, 4, 5}
wrappedInts := collection.EnumerableSlice[int](originalInts)
for entry := range wrappedInts.Enumerate(ctx) {
fmt.Print(entry)
}
fmt.Println()
// It's easy to convert arrays to slices for these enumerations as well.
originalStrings := [7]string{"red", "orange", "yellow", "green", "blue", "indigo", "violet"}
wrappedStrings := collection.EnumerableSlice[string](originalStrings[:])
for entry := range wrappedStrings.Enumerate(ctx) {
fmt.Println(entry)
}
// Output:
// 12345
// red
// orange
// yellow
// green
// blue
// indigo
// violet
}
func ExampleEnumerator_Count() {
subject := collection.AsEnumerable("str1", "str1", "str2")
count1 := subject.Enumerate(context.Background()).Count(func(a string) bool {
return a == "str1"
})
fmt.Println(count1)
// Output: 2
}
func ExampleEnumerator_CountAll() {
subject := collection.AsEnumerable('a', 'b', 'c', 'd', 'e')
fmt.Println(subject.Enumerate(context.Background()).CountAll())
// Output: 5
}
func ExampleEnumerator_ElementAt() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// ElementAt leaves the Enumerator open, creating a memory leak unless remediated,
// context.Context should be cancelled to indicate that no further reads are coming.
fmt.Print(collection.Fibonacci.Enumerate(ctx).ElementAt(4))
// Output: 3
}
func ExampleFirst() {
empty := collection.NewQueue[int]()
notEmpty := collection.NewQueue(1, 2, 3, 4)
fmt.Println(collection.First[int](empty))
fmt.Println(collection.First[int](notEmpty))
// Output:
// 0 enumerator encountered no elements
// 1 <nil>
}
func ExampleLast() {
subject := collection.NewList(1, 2, 3, 4)
fmt.Println(collection.Last[int](subject))
// Output: 4
}
func ExampleEnumerator_Last() {
subject := collection.AsEnumerable(1, 2, 3)
fmt.Print(subject.Enumerate(context.Background()).Last())
//Output: 3
}
func ExampleMerge() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
a := collection.AsEnumerable(1, 2, 4)
b := collection.AsEnumerable(8, 16, 32)
c := collection.Merge(a, b)
sum := 0
for x := range c.Enumerate(ctx) {
sum += x
}
fmt.Println(sum)
product := 1
for y := range a.Enumerate(ctx) {
product *= y
}
fmt.Println(product)
// Output:
// 63
// 8
}
func ExampleEnumerator_Reverse() {
a := collection.AsEnumerable(1, 2, 3).Enumerate(context.Background())
a = a.Reverse()
fmt.Println(a.ToSlice())
// Output: [3 2 1]
}
func ExampleSelect() {
const offset = 'a' - 1
subject := collection.AsEnumerable[rune]('a', 'b', 'c')
subject = collection.Select(subject, func(a rune) rune {
return a - offset
})
fmt.Println(collection.ToSlice(subject))
// Output: [1 2 3]
}
func ExampleEnumerator_SelectMany() {
type BrewHouse struct {
Name string
Beers collection.Enumerable[string]
}
breweries := collection.AsEnumerable(
BrewHouse{
"Mac & Jacks",
collection.AsEnumerable[string](
"African Amber",
"Ibis IPA",
),
},
BrewHouse{
"Post Doc",
collection.AsEnumerable[string](
"Prereq Pale",
),
},
BrewHouse{
"Resonate",
collection.AsEnumerable[string](
"Comfortably Numb IPA",
"Lithium Altbier",
),
},
BrewHouse{
"Triplehorn",
collection.AsEnumerable[string](
"Samson",
"Pepper Belly",
),
},
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
beers := collection.SelectMany(breweries, func(brewer BrewHouse) collection.Enumerator[string] {
return brewer.Beers.Enumerate(ctx)
})
for beer := range beers.Enumerate(ctx) {
fmt.Println(beer)
}
// Output:
// African Amber
// Ibis IPA
// Prereq Pale
// Comfortably Numb IPA
// Lithium Altbier
// Samson
// Pepper Belly
}
func ExampleSkip() {
trimmed := collection.Take(collection.Skip(collection.Fibonacci, 1), 3)
for entry := range trimmed.Enumerate(context.Background()) {
fmt.Println(entry)
}
// Output:
// 1
// 1
// 2
}
func ExampleEnumerator_Skip() {
subject := collection.AsEnumerable(1, 2, 3, 4, 5, 6, 7)
skipped := subject.Enumerate(context.Background()).Skip(5)
for entry := range skipped {
fmt.Println(entry)
}
// Output:
// 6
// 7
}
func ExampleTake() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
taken := collection.Take(collection.Fibonacci, 4)
for entry := range taken.Enumerate(ctx) {
fmt.Println(entry)
}
// Output:
// 0
// 1
// 1
// 2
}
func ExampleEnumerator_Take() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
taken := collection.Fibonacci.Enumerate(ctx).Skip(4).Take(2)
for entry := range taken {
fmt.Println(entry)
}
// Output:
// 3
// 5
}
func ExampleTakeWhile() {
taken := collection.TakeWhile(collection.Fibonacci, func(x, n uint) bool {
return x < 10
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for entry := range taken.Enumerate(ctx) {
fmt.Println(entry)
}
// Output:
// 0
// 1
// 1
// 2
// 3
// 5
// 8
}
func ExampleEnumerator_TakeWhile() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
taken := collection.Fibonacci.Enumerate(ctx).TakeWhile(func(x, n uint) bool {
return x < 6
})
for entry := range taken {
fmt.Println(entry)
}
// Output:
// 0
// 1
// 1
// 2
// 3
// 5
}
func ExampleEnumerator_Tee() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
base := collection.AsEnumerable(1, 2, 4)
left, right := base.Enumerate(ctx).Tee()
var wg sync.WaitGroup
wg.Add(2)
product := 1
go func() {
for x := range left {
product *= x
}
wg.Done()
}()
sum := 0
go func() {
for x := range right {
sum += x
}
wg.Done()
}()
wg.Wait()
fmt.Printf("Sum: %d\n", sum)
fmt.Printf("Product: %d\n", product)
// Output:
// Sum: 7
// Product: 8
}
func ExampleUCount() {
subject := collection.NewStack[any](9, 'a', "str1")
result := collection.UCount[any](subject, func(a interface{}) bool {
_, ok := a.(string)
return ok
})
fmt.Println(result)
// Output: 1
}
func ExampleEnumerator_UCount() {
subject := collection.EnumerableSlice[string]([]string{"str1", "str1", "str2"})
count1 := subject.Enumerate(context.Background()).UCount(func(a string) bool {
return a == "str1"
})
fmt.Println(count1)
// Output: 2
}
func ExampleUCountAll() {
subject := collection.NewStack(8, 9, 10, 11)
fmt.Println(collection.UCountAll[int](subject))
// Output: 4
}
func ExampleEnumerator_UCountAll() {
subject := collection.EnumerableSlice[any]([]interface{}{'a', 2, "str1"})
fmt.Println(subject.Enumerate(context.Background()).UCountAll())
// Output: 3
}
func ExampleEnumerator_Where() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
results := collection.Fibonacci.Enumerate(ctx).Where(func(a uint) bool {
return a > 8
}).Take(3)
fmt.Println(results.ToSlice())
// Output: [13 21 34]
}
func ExampleWhere() {
nums := collection.EnumerableSlice[int]([]int{1, 2, 3, 4, 5})
results := collection.Where[int](nums, func(a int) bool {
return a < 3
})
fmt.Println(collection.ToSlice(results))
// Output: [1 2]
}