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
329
330
331 }
332
333 func ExampleGrow() {
334 numbers := []int{0, 42, -10, 8}
335 grow := slices.Grow(numbers, 2)
336 fmt.Println(cap(numbers))
337 fmt.Println(grow)
338 fmt.Println(len(grow))
339 fmt.Println(cap(grow))
340
341
342
343
344
345 }
346
347 func ExampleClip() {
348 a := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
349 s := a[:4:10]
350 clip := slices.Clip(s)
351 fmt.Println(cap(s))
352 fmt.Println(clip)
353 fmt.Println(len(clip))
354 fmt.Println(cap(clip))
355
356
357
358
359
360 }
361
362 func ExampleConcat() {
363 s1 := []int{0, 1, 2, 3}
364 s2 := []int{4, 5, 6}
365 concat := slices.Concat(s1, s2)
366 fmt.Println(concat)
367
368
369 }
370
371 func ExampleContains() {
372 numbers := []int{0, 1, 2, 3}
373 fmt.Println(slices.Contains(numbers, 2))
374 fmt.Println(slices.Contains(numbers, 4))
375
376
377
378 }
379
380 func ExampleRepeat() {
381 numbers := []int{0, 1, 2, 3}
382 repeat := slices.Repeat(numbers, 2)
383 fmt.Println(repeat)
384
385
386 }
387
388 func ExampleChunk() {
389 type Person struct {
390 Name string
391 Age int
392 }
393
394 type People []Person
395
396 people := People{
397 {"Gopher", 13},
398 {"Alice", 20},
399 {"Bob", 5},
400 {"Vera", 24},
401 {"Zac", 15},
402 }
403
404
405 for c := range slices.Chunk(people, 2) {
406 fmt.Println(c)
407 }
408
409
410
411
412
413 }
414
View as plain text