1 // Copyright 2023 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
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)
26 func ExampleBinarySearchFunc() {
36 n, found := slices.BinarySearchFunc(people, Person{"Bob", 0}, func(a, b Person) int {
37 return cmp.Compare(a.Name, b.Name)
39 fmt.Println("Bob:", n, found)
44 func ExampleCompact() {
45 seq := []int{0, 1, 1, 2, 3, 5, 8}
46 seq = slices.Compact(seq)
52 func ExampleCompactFunc() {
53 names := []string{"bob", "Bob", "alice", "Vera", "VERA"}
54 names = slices.CompactFunc(names, strings.EqualFold)
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"}))
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)
81 return cmp.Compare(n, sn)
88 func ExampleContainsFunc() {
89 numbers := []int{0, 42, -10, 8}
90 hasNegative := slices.ContainsFunc(numbers, func(n int) bool {
93 fmt.Println("Has a negative:", hasNegative)
94 hasOdd := slices.ContainsFunc(numbers, func(n int) bool {
97 fmt.Println("Has an odd number:", hasOdd)
99 // Has a negative: true
100 // Has an odd number: false
103 func ExampleDelete() {
104 letters := []string{"a", "b", "c", "d", "e"}
105 letters = slices.Delete(letters, 1, 4)
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 // delete the odd numbers
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}))
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)
145 func ExampleIndex() {
146 numbers := []int{0, 42, 8}
147 fmt.Println(slices.Index(numbers, 8))
148 fmt.Println(slices.Index(numbers, 7))
154 func ExampleIndexFunc() {
155 numbers := []int{0, 42, -10, 8}
156 i := slices.IndexFunc(numbers, func(n int) bool {
159 fmt.Println("First negative at index", i)
161 // First negative at index 2
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")
170 // [Alice Bill Billie Bob Vera Zac]
173 func ExampleIsSorted() {
174 fmt.Println(slices.IsSorted([]string{"Alice", "Bob", "Vera"}))
175 fmt.Println(slices.IsSorted([]int{0, 2, 1}))
181 func ExampleIsSortedFunc() {
182 names := []string{"alice", "Bob", "VERA"}
183 isSortedInsensitive := slices.IsSortedFunc(names, func(a, b string) int {
184 return cmp.Compare(strings.ToLower(a), strings.ToLower(b))
186 fmt.Println(isSortedInsensitive)
187 fmt.Println(slices.IsSorted(names))
194 numbers := []int{0, 42, -10, 8}
195 fmt.Println(slices.Max(numbers))
200 func ExampleMaxFunc() {
211 firstOldest := slices.MaxFunc(people, func(a, b Person) int {
212 return cmp.Compare(a.Age, b.Age)
214 fmt.Println(firstOldest.Name)
220 numbers := []int{0, 42, -10, 8}
221 fmt.Println(slices.Min(numbers))
226 func ExampleMinFunc() {
237 firstYoungest := slices.MinFunc(people, func(a, b Person) int {
238 return cmp.Compare(a.Age, b.Age)
240 fmt.Println(firstYoungest.Name)
245 func ExampleReplace() {
246 names := []string{"Alice", "Bob", "Vera", "Zac"}
247 names = slices.Replace(names, 1, 3, "Bill", "Billie", "Cat")
250 // [Alice Bill Billie Cat Zac]
253 func ExampleReverse() {
254 names := []string{"alice", "Bob", "VERA"}
255 slices.Reverse(names)
262 smallInts := []int8{0, 42, -10, 8}
263 slices.Sort(smallInts)
264 fmt.Println(smallInts)
269 func ExampleSortFunc_caseInsensitive() {
270 names := []string{"Bob", "alice", "VERA"}
271 slices.SortFunc(names, func(a, b string) int {
272 return cmp.Compare(strings.ToLower(a), strings.ToLower(b))
279 func ExampleSortFunc_multiField() {
290 slices.SortFunc(people, func(a, b Person) int {
291 if n := cmp.Compare(a.Name, b.Name); n != 0 {
294 // If names are equal, order by age
295 return cmp.Compare(a.Age, b.Age)
299 // [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
302 func ExampleSortStableFunc() {
313 // Stable sort by name, keeping age ordering of Alices intact
314 slices.SortStableFunc(people, func(a, b Person) int {
315 return cmp.Compare(a.Name, b.Name)
319 // [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]