| | |
| | |
| | |
| |
|
| | package slices_test |
| |
|
| | import ( |
| | "cmp" |
| | "fmt" |
| | "slices" |
| | "strconv" |
| | "strings" |
| | ) |
| |
|
| | func ExampleBinarySearch() { |
| | names := []string{"Alice", "Bob", "Vera"} |
| | n, found := slices.BinarySearch(names, "Vera") |
| | fmt.Println("Vera:", n, found) |
| | n, found = slices.BinarySearch(names, "Bill") |
| | fmt.Println("Bill:", n, found) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleBinarySearchFunc() { |
| | type Person struct { |
| | Name string |
| | Age int |
| | } |
| | people := []Person{ |
| | {"Alice", 55}, |
| | {"Bob", 24}, |
| | {"Gopher", 13}, |
| | } |
| | n, found := slices.BinarySearchFunc(people, Person{"Bob", 0}, func(a, b Person) int { |
| | return strings.Compare(a.Name, b.Name) |
| | }) |
| | fmt.Println("Bob:", n, found) |
| | |
| | |
| | } |
| |
|
| | func ExampleCompact() { |
| | seq := []int{0, 1, 1, 2, 3, 5, 8} |
| | seq = slices.Compact(seq) |
| | fmt.Println(seq) |
| | |
| | |
| | } |
| |
|
| | func ExampleCompactFunc() { |
| | names := []string{"bob", "Bob", "alice", "Vera", "VERA"} |
| | names = slices.CompactFunc(names, strings.EqualFold) |
| | fmt.Println(names) |
| | |
| | |
| | } |
| |
|
| | func ExampleCompare() { |
| | names := []string{"Alice", "Bob", "Vera"} |
| | fmt.Println("Equal:", slices.Compare(names, []string{"Alice", "Bob", "Vera"})) |
| | fmt.Println("V < X:", slices.Compare(names, []string{"Alice", "Bob", "Xena"})) |
| | fmt.Println("V > C:", slices.Compare(names, []string{"Alice", "Bob", "Cat"})) |
| | fmt.Println("3 > 2:", slices.Compare(names, []string{"Alice", "Bob"})) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleCompareFunc() { |
| | numbers := []int{0, 43, 8} |
| | strings := []string{"0", "0", "8"} |
| | result := slices.CompareFunc(numbers, strings, func(n int, s string) int { |
| | sn, err := strconv.Atoi(s) |
| | if err != nil { |
| | return 1 |
| | } |
| | return cmp.Compare(n, sn) |
| | }) |
| | fmt.Println(result) |
| | |
| | |
| | } |
| |
|
| | func ExampleContainsFunc() { |
| | numbers := []int{0, 42, -10, 8} |
| | hasNegative := slices.ContainsFunc(numbers, func(n int) bool { |
| | return n < 0 |
| | }) |
| | fmt.Println("Has a negative:", hasNegative) |
| | hasOdd := slices.ContainsFunc(numbers, func(n int) bool { |
| | return n%2 != 0 |
| | }) |
| | fmt.Println("Has an odd number:", hasOdd) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleDelete() { |
| | letters := []string{"a", "b", "c", "d", "e"} |
| | letters = slices.Delete(letters, 1, 4) |
| | fmt.Println(letters) |
| | |
| | |
| | } |
| |
|
| | func ExampleDeleteFunc() { |
| | seq := []int{0, 1, 1, 2, 3, 5, 8} |
| | seq = slices.DeleteFunc(seq, func(n int) bool { |
| | return n%2 != 0 |
| | }) |
| | fmt.Println(seq) |
| | |
| | |
| | } |
| |
|
| | func ExampleEqual() { |
| | numbers := []int{0, 42, 8} |
| | fmt.Println(slices.Equal(numbers, []int{0, 42, 8})) |
| | fmt.Println(slices.Equal(numbers, []int{10})) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleEqualFunc() { |
| | numbers := []int{0, 42, 8} |
| | strings := []string{"000", "42", "0o10"} |
| | equal := slices.EqualFunc(numbers, strings, func(n int, s string) bool { |
| | sn, err := strconv.ParseInt(s, 0, 64) |
| | if err != nil { |
| | return false |
| | } |
| | return n == int(sn) |
| | }) |
| | fmt.Println(equal) |
| | |
| | |
| | } |
| |
|
| | func ExampleIndex() { |
| | numbers := []int{0, 42, 8} |
| | fmt.Println(slices.Index(numbers, 8)) |
| | fmt.Println(slices.Index(numbers, 7)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIndexFunc() { |
| | numbers := []int{0, 42, -10, 8} |
| | i := slices.IndexFunc(numbers, func(n int) bool { |
| | return n < 0 |
| | }) |
| | fmt.Println("First negative at index", i) |
| | |
| | |
| | } |
| |
|
| | func ExampleInsert() { |
| | names := []string{"Alice", "Bob", "Vera"} |
| | names = slices.Insert(names, 1, "Bill", "Billie") |
| | names = slices.Insert(names, len(names), "Zac") |
| | fmt.Println(names) |
| | |
| | |
| | } |
| |
|
| | func ExampleIsSorted() { |
| | fmt.Println(slices.IsSorted([]string{"Alice", "Bob", "Vera"})) |
| | fmt.Println(slices.IsSorted([]int{0, 2, 1})) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleIsSortedFunc() { |
| | names := []string{"alice", "Bob", "VERA"} |
| | isSortedInsensitive := slices.IsSortedFunc(names, func(a, b string) int { |
| | return strings.Compare(strings.ToLower(a), strings.ToLower(b)) |
| | }) |
| | fmt.Println(isSortedInsensitive) |
| | fmt.Println(slices.IsSorted(names)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleMax() { |
| | numbers := []int{0, 42, -10, 8} |
| | fmt.Println(slices.Max(numbers)) |
| | |
| | |
| | } |
| |
|
| | func ExampleMaxFunc() { |
| | type Person struct { |
| | Name string |
| | Age int |
| | } |
| | people := []Person{ |
| | {"Gopher", 13}, |
| | {"Alice", 55}, |
| | {"Vera", 24}, |
| | {"Bob", 55}, |
| | } |
| | firstOldest := slices.MaxFunc(people, func(a, b Person) int { |
| | return cmp.Compare(a.Age, b.Age) |
| | }) |
| | fmt.Println(firstOldest.Name) |
| | |
| | |
| | } |
| |
|
| | func ExampleMin() { |
| | numbers := []int{0, 42, -10, 8} |
| | fmt.Println(slices.Min(numbers)) |
| | |
| | |
| | } |
| |
|
| | func ExampleMinFunc() { |
| | type Person struct { |
| | Name string |
| | Age int |
| | } |
| | people := []Person{ |
| | {"Gopher", 13}, |
| | {"Bob", 5}, |
| | {"Vera", 24}, |
| | {"Bill", 5}, |
| | } |
| | firstYoungest := slices.MinFunc(people, func(a, b Person) int { |
| | return cmp.Compare(a.Age, b.Age) |
| | }) |
| | fmt.Println(firstYoungest.Name) |
| | |
| | |
| | } |
| |
|
| | func ExampleReplace() { |
| | names := []string{"Alice", "Bob", "Vera", "Zac"} |
| | names = slices.Replace(names, 1, 3, "Bill", "Billie", "Cat") |
| | fmt.Println(names) |
| | |
| | |
| | } |
| |
|
| | func ExampleReverse() { |
| | names := []string{"alice", "Bob", "VERA"} |
| | slices.Reverse(names) |
| | fmt.Println(names) |
| | |
| | |
| | } |
| |
|
| | func ExampleSort() { |
| | smallInts := []int8{0, 42, -10, 8} |
| | slices.Sort(smallInts) |
| | fmt.Println(smallInts) |
| | |
| | |
| | } |
| |
|
| | func ExampleSortFunc_caseInsensitive() { |
| | names := []string{"Bob", "alice", "VERA"} |
| | slices.SortFunc(names, func(a, b string) int { |
| | return strings.Compare(strings.ToLower(a), strings.ToLower(b)) |
| | }) |
| | fmt.Println(names) |
| | |
| | |
| | } |
| |
|
| | func ExampleSortFunc_multiField() { |
| | type Person struct { |
| | Name string |
| | Age int |
| | } |
| | people := []Person{ |
| | {"Gopher", 13}, |
| | {"Alice", 55}, |
| | {"Bob", 24}, |
| | {"Alice", 20}, |
| | } |
| | slices.SortFunc(people, func(a, b Person) int { |
| | if n := strings.Compare(a.Name, b.Name); n != 0 { |
| | return n |
| | } |
| | |
| | return cmp.Compare(a.Age, b.Age) |
| | }) |
| | fmt.Println(people) |
| | |
| | |
| | } |
| |
|
| | func ExampleSortStableFunc() { |
| | type Person struct { |
| | Name string |
| | Age int |
| | } |
| | people := []Person{ |
| | {"Gopher", 13}, |
| | {"Alice", 20}, |
| | {"Bob", 24}, |
| | {"Alice", 55}, |
| | } |
| | |
| | slices.SortStableFunc(people, func(a, b Person) int { |
| | return strings.Compare(a.Name, b.Name) |
| | }) |
| | fmt.Println(people) |
| | |
| | |
| | } |
| |
|
| | func ExampleClone() { |
| | numbers := []int{0, 42, -10, 8} |
| | clone := slices.Clone(numbers) |
| | fmt.Println(clone) |
| | clone[2] = 10 |
| | fmt.Println(numbers) |
| | fmt.Println(clone) |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleGrow() { |
| | numbers := []int{0, 42, -10, 8} |
| | grow := slices.Grow(numbers, 2) |
| | fmt.Println(cap(numbers)) |
| | fmt.Println(grow) |
| | fmt.Println(len(grow)) |
| | fmt.Println(cap(grow)) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleClip() { |
| | a := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| | s := a[:4:10] |
| | clip := slices.Clip(s) |
| | fmt.Println(cap(s)) |
| | fmt.Println(clip) |
| | fmt.Println(len(clip)) |
| | fmt.Println(cap(clip)) |
| | |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleConcat() { |
| | s1 := []int{0, 1, 2, 3} |
| | s2 := []int{4, 5, 6} |
| | concat := slices.Concat(s1, s2) |
| | fmt.Println(concat) |
| | |
| | |
| | } |
| |
|
| | func ExampleContains() { |
| | numbers := []int{0, 1, 2, 3} |
| | fmt.Println(slices.Contains(numbers, 2)) |
| | fmt.Println(slices.Contains(numbers, 4)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleRepeat() { |
| | numbers := []int{0, 1, 2, 3} |
| | repeat := slices.Repeat(numbers, 2) |
| | fmt.Println(repeat) |
| | |
| | |
| | } |
| |
|
| | func ExampleAll() { |
| | names := []string{"Alice", "Bob", "Vera"} |
| | for i, v := range slices.All(names) { |
| | fmt.Println(i, ":", v) |
| | } |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleBackward() { |
| | names := []string{"Alice", "Bob", "Vera"} |
| | for i, v := range slices.Backward(names) { |
| | fmt.Println(i, ":", v) |
| | } |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleValues() { |
| | names := []string{"Alice", "Bob", "Vera"} |
| | for v := range slices.Values(names) { |
| | fmt.Println(v) |
| | } |
| | |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleAppendSeq() { |
| | seq := func(yield func(int) bool) { |
| | for i := 0; i < 10; i += 2 { |
| | if !yield(i) { |
| | return |
| | } |
| | } |
| | } |
| |
|
| | s := slices.AppendSeq([]int{1, 2}, seq) |
| | fmt.Println(s) |
| | |
| | |
| | } |
| |
|
| | func ExampleCollect() { |
| | seq := func(yield func(int) bool) { |
| | for i := 0; i < 10; i += 2 { |
| | if !yield(i) { |
| | return |
| | } |
| | } |
| | } |
| |
|
| | s := slices.Collect(seq) |
| | fmt.Println(s) |
| | |
| | |
| | } |
| |
|
| | func ExampleSorted() { |
| | seq := func(yield func(int) bool) { |
| | flag := -1 |
| | for i := 0; i < 10; i += 2 { |
| | flag = -flag |
| | if !yield(i * flag) { |
| | return |
| | } |
| | } |
| | } |
| |
|
| | s := slices.Sorted(seq) |
| | fmt.Println(s) |
| | fmt.Println(slices.IsSorted(s)) |
| | |
| | |
| | |
| | } |
| |
|
| | func ExampleSortedFunc() { |
| | seq := func(yield func(int) bool) { |
| | flag := -1 |
| | for i := 0; i < 10; i += 2 { |
| | flag = -flag |
| | if !yield(i * flag) { |
| | return |
| | } |
| | } |
| | } |
| |
|
| | sortFunc := func(a, b int) int { |
| | return cmp.Compare(b, a) |
| | } |
| |
|
| | s := slices.SortedFunc(seq, sortFunc) |
| | fmt.Println(s) |
| | |
| | |
| | } |
| |
|
| | func ExampleSortedStableFunc() { |
| | type Person struct { |
| | Name string |
| | Age int |
| | } |
| |
|
| | people := []Person{ |
| | {"Gopher", 13}, |
| | {"Alice", 20}, |
| | {"Bob", 5}, |
| | {"Vera", 24}, |
| | {"Zac", 20}, |
| | } |
| |
|
| | sortFunc := func(x, y Person) int { |
| | return cmp.Compare(x.Age, y.Age) |
| | } |
| |
|
| | s := slices.SortedStableFunc(slices.Values(people), sortFunc) |
| | fmt.Println(s) |
| | |
| | |
| | } |
| |
|
| | func ExampleChunk() { |
| | type Person struct { |
| | Name string |
| | Age int |
| | } |
| |
|
| | type People []Person |
| |
|
| | people := People{ |
| | {"Gopher", 13}, |
| | {"Alice", 20}, |
| | {"Bob", 5}, |
| | {"Vera", 24}, |
| | {"Zac", 15}, |
| | } |
| |
|
| | |
| | for c := range slices.Chunk(people, 2) { |
| | fmt.Println(c) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | } |
| |
|