Source file src/slices/example_test.go

     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.
     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  	// Output:
    22  	// Vera: 2 true
    23  	// Bill: 1 false
    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  	// Output:
    41  	// Bob: 1 true
    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  	// Output:
    49  	// [0 1 2 3 5 8]
    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  	// Output:
    57  	// [bob alice Vera]
    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  	// Output:
    67  	// Equal: 0
    68  	// V < X: -1
    69  	// V > C: 1
    70  	// 3 > 2: 1
    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  	// Output:
    85  	// 1
    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  	// Output:
    99  	// Has a negative: true
   100  	// Has an odd number: false
   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  	// Output:
   108  	// [a e]
   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 // delete the odd numbers
   115  	})
   116  	fmt.Println(seq)
   117  	// Output:
   118  	// [0 2 8]
   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  	// Output:
   126  	// true
   127  	// false
   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  	// Output:
   142  	// true
   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  	// Output:
   150  	// 2
   151  	// -1
   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  	// Output:
   161  	// First negative at index 2
   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  	// Output:
   170  	// [Alice Bill Billie Bob Vera Zac]
   171  }
   172  
   173  func ExampleIsSorted() {
   174  	fmt.Println(slices.IsSorted([]string{"Alice", "Bob", "Vera"}))
   175  	fmt.Println(slices.IsSorted([]int{0, 2, 1}))
   176  	// Output:
   177  	// true
   178  	// false
   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  	// Output:
   189  	// true
   190  	// false
   191  }
   192  
   193  func ExampleMax() {
   194  	numbers := []int{0, 42, -10, 8}
   195  	fmt.Println(slices.Max(numbers))
   196  	// Output:
   197  	// 42
   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  	// Output:
   216  	// Alice
   217  }
   218  
   219  func ExampleMin() {
   220  	numbers := []int{0, 42, -10, 8}
   221  	fmt.Println(slices.Min(numbers))
   222  	// Output:
   223  	// -10
   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  	// Output:
   242  	// Bob
   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  	// Output:
   250  	// [Alice Bill Billie Cat Zac]
   251  }
   252  
   253  func ExampleReverse() {
   254  	names := []string{"alice", "Bob", "VERA"}
   255  	slices.Reverse(names)
   256  	fmt.Println(names)
   257  	// Output:
   258  	// [VERA Bob alice]
   259  }
   260  
   261  func ExampleSort() {
   262  	smallInts := []int8{0, 42, -10, 8}
   263  	slices.Sort(smallInts)
   264  	fmt.Println(smallInts)
   265  	// Output:
   266  	// [-10 0 8 42]
   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  	// Output:
   276  	// [alice Bob VERA]
   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  		// If names are equal, order by age
   295  		return cmp.Compare(a.Age, b.Age)
   296  	})
   297  	fmt.Println(people)
   298  	// Output:
   299  	// [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
   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  	// Stable sort by name, keeping age ordering of Alice intact
   314  	slices.SortStableFunc(people, func(a, b Person) int {
   315  		return strings.Compare(a.Name, b.Name)
   316  	})
   317  	fmt.Println(people)
   318  	// Output:
   319  	// [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
   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  	// Output:
   330  	// [0 42 -10 8]
   331  	// [0 42 -10 8]
   332  	// [0 42 10 8]
   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  	// Output:
   343  	// 4
   344  	// [0 42 -10 8]
   345  	// 4
   346  	// 8
   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  	// Output:
   358  	// 10
   359  	// [0 1 2 3]
   360  	// 4
   361  	// 4
   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  	// Output:
   370  	// [0 1 2 3 4 5 6]
   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  	// Output:
   378  	// true
   379  	// false
   380  }
   381  
   382  func ExampleRepeat() {
   383  	numbers := []int{0, 1, 2, 3}
   384  	repeat := slices.Repeat(numbers, 2)
   385  	fmt.Println(repeat)
   386  	// Output:
   387  	// [0 1 2 3 0 1 2 3]
   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  	// Output:
   396  	// 0 : Alice
   397  	// 1 : Bob
   398  	// 2 : Vera
   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  	// Output:
   407  	// 2 : Vera
   408  	// 1 : Bob
   409  	// 0 : Alice
   410  }
   411  
   412  func ExampleValues() {
   413  	names := []string{"Alice", "Bob", "Vera"}
   414  	for v := range slices.Values(names) {
   415  		fmt.Println(v)
   416  	}
   417  	// Output:
   418  	// Alice
   419  	// Bob
   420  	// Vera
   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  	// Output:
   435  	// [1 2 0 2 4 6 8]
   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  	// Output:
   450  	// [0 2 4 6 8]
   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  	// Output:
   468  	// [-6 -2 0 4 8]
   469  	// true
   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) // the comparison is being done in reverse
   485  	}
   486  
   487  	s := slices.SortedFunc(seq, sortFunc)
   488  	fmt.Println(s)
   489  	// Output:
   490  	// [8 4 0 -2 -6]
   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  	// Output:
   514  	// [{Bob 5} {Gopher 13} {Alice 20} {Zac 20} {Vera 24}]
   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  	// Chunk people into []Person 2 elements at a time.
   534  	for c := range slices.Chunk(people, 2) {
   535  		fmt.Println(c)
   536  	}
   537  
   538  	// Output:
   539  	// [{Gopher 13} {Alice 20}]
   540  	// [{Bob 5} {Vera 24}]
   541  	// [{Zac 15}]
   542  }
   543  

View as plain text