Source file
src/slices/example_test.go
1
2
3
4
5 package slices_test
6
7 import (
8 "cmp"
9 "fmt"
10 "slices"
11 "strconv"
12 "strings"
13 )
14
15 func ExampleBinarySearch() {
16 names := []string{"Alice", "Bob", "Vera"}
17 n, found := slices.BinarySearch(names, "Vera")
18 fmt.Println("Vera:", n, found)
19 n, found = slices.BinarySearch(names, "Bill")
20 fmt.Println("Bill:", n, found)
21
22
23
24 }
25
26 func ExampleBinarySearchFunc() {
27 type Person struct {
28 Name string
29 Age int
30 }
31 people := []Person{
32 {"Alice", 55},
33 {"Bob", 24},
34 {"Gopher", 13},
35 }
36 n, found := slices.BinarySearchFunc(people, Person{"Bob", 0}, func(a, b Person) int {
37 return strings.Compare(a.Name, b.Name)
38 })
39 fmt.Println("Bob:", n, found)
40
41
42 }
43
44 func ExampleCompact() {
45 seq := []int{0, 1, 1, 2, 3, 5, 8}
46 seq = slices.Compact(seq)
47 fmt.Println(seq)
48
49
50 }
51
52 func ExampleCompactFunc() {
53 names := []string{"bob", "Bob", "alice", "Vera", "VERA"}
54 names = slices.CompactFunc(names, strings.EqualFold)
55 fmt.Println(names)
56
57
58 }
59
60 func ExampleCompare() {
61 names := []string{"Alice", "Bob", "Vera"}
62 fmt.Println("Equal:", slices.Compare(names, []string{"Alice", "Bob", "Vera"}))
63 fmt.Println("V < X:", slices.Compare(names, []string{"Alice", "Bob", "Xena"}))
64 fmt.Println("V > C:", slices.Compare(names, []string{"Alice", "Bob", "Cat"}))
65 fmt.Println("3 > 2:", slices.Compare(names, []string{"Alice", "Bob"}))
66
67
68
69
70
71 }
72
73 func ExampleCompareFunc() {
74 numbers := []int{0, 43, 8}
75 strings := []string{"0", "0", "8"}
76 result := slices.CompareFunc(numbers, strings, func(n int, s string) int {
77 sn, err := strconv.Atoi(s)
78 if err != nil {
79 return 1
80 }
81 return cmp.Compare(n, sn)
82 })
83 fmt.Println(result)
84
85
86 }
87
88 func ExampleContainsFunc() {
89 numbers := []int{0, 42, -10, 8}
90 hasNegative := slices.ContainsFunc(numbers, func(n int) bool {
91 return n < 0
92 })
93 fmt.Println("Has a negative:", hasNegative)
94 hasOdd := slices.ContainsFunc(numbers, func(n int) bool {
95 return n%2 != 0
96 })
97 fmt.Println("Has an odd number:", hasOdd)
98
99
100
101 }
102
103 func ExampleDelete() {
104 letters := []string{"a", "b", "c", "d", "e"}
105 letters = slices.Delete(letters, 1, 4)
106 fmt.Println(letters)
107
108
109 }
110
111 func ExampleDeleteFunc() {
112 seq := []int{0, 1, 1, 2, 3, 5, 8}
113 seq = slices.DeleteFunc(seq, func(n int) bool {
114 return n%2 != 0
115 })
116 fmt.Println(seq)
117
118
119 }
120
121 func ExampleEqual() {
122 numbers := []int{0, 42, 8}
123 fmt.Println(slices.Equal(numbers, []int{0, 42, 8}))
124 fmt.Println(slices.Equal(numbers, []int{10}))
125
126
127
128 }
129
130 func ExampleEqualFunc() {
131 numbers := []int{0, 42, 8}
132 strings := []string{"000", "42", "0o10"}
133 equal := slices.EqualFunc(numbers, strings, func(n int, s string) bool {
134 sn, err := strconv.ParseInt(s, 0, 64)
135 if err != nil {
136 return false
137 }
138 return n == int(sn)
139 })
140 fmt.Println(equal)
141
142
143 }
144
145 func ExampleIndex() {
146 numbers := []int{0, 42, 8}
147 fmt.Println(slices.Index(numbers, 8))
148 fmt.Println(slices.Index(numbers, 7))
149
150
151
152 }
153
154 func ExampleIndexFunc() {
155 numbers := []int{0, 42, -10, 8}
156 i := slices.IndexFunc(numbers, func(n int) bool {
157 return n < 0
158 })
159 fmt.Println("First negative at index", i)
160
161
162 }
163
164 func ExampleInsert() {
165 names := []string{"Alice", "Bob", "Vera"}
166 names = slices.Insert(names, 1, "Bill", "Billie")
167 names = slices.Insert(names, len(names), "Zac")
168 fmt.Println(names)
169
170
171 }
172
173 func ExampleIsSorted() {
174 fmt.Println(slices.IsSorted([]string{"Alice", "Bob", "Vera"}))
175 fmt.Println(slices.IsSorted([]int{0, 2, 1}))
176
177
178
179 }
180
181 func ExampleIsSortedFunc() {
182 names := []string{"alice", "Bob", "VERA"}
183 isSortedInsensitive := slices.IsSortedFunc(names, func(a, b string) int {
184 return strings.Compare(strings.ToLower(a), strings.ToLower(b))
185 })
186 fmt.Println(isSortedInsensitive)
187 fmt.Println(slices.IsSorted(names))
188
189
190
191 }
192
193 func ExampleMax() {
194 numbers := []int{0, 42, -10, 8}
195 fmt.Println(slices.Max(numbers))
196
197
198 }
199
200 func ExampleMaxFunc() {
201 type Person struct {
202 Name string
203 Age int
204 }
205 people := []Person{
206 {"Gopher", 13},
207 {"Alice", 55},
208 {"Vera", 24},
209 {"Bob", 55},
210 }
211 firstOldest := slices.MaxFunc(people, func(a, b Person) int {
212 return cmp.Compare(a.Age, b.Age)
213 })
214 fmt.Println(firstOldest.Name)
215
216
217 }
218
219 func ExampleMin() {
220 numbers := []int{0, 42, -10, 8}
221 fmt.Println(slices.Min(numbers))
222
223
224 }
225
226 func ExampleMinFunc() {
227 type Person struct {
228 Name string
229 Age int
230 }
231 people := []Person{
232 {"Gopher", 13},
233 {"Bob", 5},
234 {"Vera", 24},
235 {"Bill", 5},
236 }
237 firstYoungest := slices.MinFunc(people, func(a, b Person) int {
238 return cmp.Compare(a.Age, b.Age)
239 })
240 fmt.Println(firstYoungest.Name)
241
242
243 }
244
245 func ExampleReplace() {
246 names := []string{"Alice", "Bob", "Vera", "Zac"}
247 names = slices.Replace(names, 1, 3, "Bill", "Billie", "Cat")
248 fmt.Println(names)
249
250
251 }
252
253 func ExampleReverse() {
254 names := []string{"alice", "Bob", "VERA"}
255 slices.Reverse(names)
256 fmt.Println(names)
257
258
259 }
260
261 func ExampleSort() {
262 smallInts := []int8{0, 42, -10, 8}
263 slices.Sort(smallInts)
264 fmt.Println(smallInts)
265
266
267 }
268
269 func ExampleSortFunc_caseInsensitive() {
270 names := []string{"Bob", "alice", "VERA"}
271 slices.SortFunc(names, func(a, b string) int {
272 return strings.Compare(strings.ToLower(a), strings.ToLower(b))
273 })
274 fmt.Println(names)
275
276
277 }
278
279 func ExampleSortFunc_multiField() {
280 type Person struct {
281 Name string
282 Age int
283 }
284 people := []Person{
285 {"Gopher", 13},
286 {"Alice", 55},
287 {"Bob", 24},
288 {"Alice", 20},
289 }
290 slices.SortFunc(people, func(a, b Person) int {
291 if n := strings.Compare(a.Name, b.Name); n != 0 {
292 return n
293 }
294
295 return cmp.Compare(a.Age, b.Age)
296 })
297 fmt.Println(people)
298
299
300 }
301
302 func ExampleSortStableFunc() {
303 type Person struct {
304 Name string
305 Age int
306 }
307 people := []Person{
308 {"Gopher", 13},
309 {"Alice", 20},
310 {"Bob", 24},
311 {"Alice", 55},
312 }
313
314 slices.SortStableFunc(people, func(a, b Person) int {
315 return strings.Compare(a.Name, b.Name)
316 })
317 fmt.Println(people)
318
319
320 }
321
322 func ExampleClone() {
323 numbers := []int{0, 42, -10, 8}
324 clone := slices.Clone(numbers)
325 fmt.Println(clone)
326 clone[2] = 10
327 fmt.Println(numbers)
328 fmt.Println(clone)
329
330
331
332
333 }
334
335 func ExampleGrow() {
336 numbers := []int{0, 42, -10, 8}
337 grow := slices.Grow(numbers, 2)
338 fmt.Println(cap(numbers))
339 fmt.Println(grow)
340 fmt.Println(len(grow))
341 fmt.Println(cap(grow))
342
343
344
345
346
347 }
348
349 func ExampleClip() {
350 a := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
351 s := a[:4:10]
352 clip := slices.Clip(s)
353 fmt.Println(cap(s))
354 fmt.Println(clip)
355 fmt.Println(len(clip))
356 fmt.Println(cap(clip))
357
358
359
360
361
362 }
363
364 func ExampleConcat() {
365 s1 := []int{0, 1, 2, 3}
366 s2 := []int{4, 5, 6}
367 concat := slices.Concat(s1, s2)
368 fmt.Println(concat)
369
370
371 }
372
373 func ExampleContains() {
374 numbers := []int{0, 1, 2, 3}
375 fmt.Println(slices.Contains(numbers, 2))
376 fmt.Println(slices.Contains(numbers, 4))
377
378
379
380 }
381
382 func ExampleRepeat() {
383 numbers := []int{0, 1, 2, 3}
384 repeat := slices.Repeat(numbers, 2)
385 fmt.Println(repeat)
386
387
388 }
389
390 func ExampleAll() {
391 names := []string{"Alice", "Bob", "Vera"}
392 for i, v := range slices.All(names) {
393 fmt.Println(i, ":", v)
394 }
395
396
397
398
399 }
400
401 func ExampleBackward() {
402 names := []string{"Alice", "Bob", "Vera"}
403 for i, v := range slices.Backward(names) {
404 fmt.Println(i, ":", v)
405 }
406
407
408
409
410 }
411
412 func ExampleValues() {
413 names := []string{"Alice", "Bob", "Vera"}
414 for v := range slices.Values(names) {
415 fmt.Println(v)
416 }
417
418
419
420
421 }
422
423 func ExampleAppendSeq() {
424 seq := func(yield func(int) bool) {
425 for i := 0; i < 10; i += 2 {
426 if !yield(i) {
427 return
428 }
429 }
430 }
431
432 s := slices.AppendSeq([]int{1, 2}, seq)
433 fmt.Println(s)
434
435
436 }
437
438 func ExampleCollect() {
439 seq := func(yield func(int) bool) {
440 for i := 0; i < 10; i += 2 {
441 if !yield(i) {
442 return
443 }
444 }
445 }
446
447 s := slices.Collect(seq)
448 fmt.Println(s)
449
450
451 }
452
453 func ExampleSorted() {
454 seq := func(yield func(int) bool) {
455 flag := -1
456 for i := 0; i < 10; i += 2 {
457 flag = -flag
458 if !yield(i * flag) {
459 return
460 }
461 }
462 }
463
464 s := slices.Sorted(seq)
465 fmt.Println(s)
466 fmt.Println(slices.IsSorted(s))
467
468
469
470 }
471
472 func ExampleSortedFunc() {
473 seq := func(yield func(int) bool) {
474 flag := -1
475 for i := 0; i < 10; i += 2 {
476 flag = -flag
477 if !yield(i * flag) {
478 return
479 }
480 }
481 }
482
483 sortFunc := func(a, b int) int {
484 return cmp.Compare(b, a)
485 }
486
487 s := slices.SortedFunc(seq, sortFunc)
488 fmt.Println(s)
489
490
491 }
492
493 func ExampleSortedStableFunc() {
494 type Person struct {
495 Name string
496 Age int
497 }
498
499 people := []Person{
500 {"Gopher", 13},
501 {"Alice", 20},
502 {"Bob", 5},
503 {"Vera", 24},
504 {"Zac", 20},
505 }
506
507 sortFunc := func(x, y Person) int {
508 return cmp.Compare(x.Age, y.Age)
509 }
510
511 s := slices.SortedStableFunc(slices.Values(people), sortFunc)
512 fmt.Println(s)
513
514
515 }
516
517 func ExampleChunk() {
518 type Person struct {
519 Name string
520 Age int
521 }
522
523 type People []Person
524
525 people := People{
526 {"Gopher", 13},
527 {"Alice", 20},
528 {"Bob", 5},
529 {"Vera", 24},
530 {"Zac", 15},
531 }
532
533
534 for c := range slices.Chunk(people, 2) {
535 fmt.Println(c)
536 }
537
538
539
540
541
542 }
543
View as plain text