[go] slices, maps: add examples; doc comment fixes

45 views
Skip to first unread message

Ben Hoyt (Gerrit)

unread,
Jun 22, 2023, 8:39:33 AM6/22/23
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Ben Hoyt has uploaded this change for review.

View Change

slices, maps: add examples; doc comment fixes

There are currently no examples in the new slices and maps package, so
add some. This adds examples for most functions in the slices package
except the very obvious ones, and adds examples for the DeleteFunc and
EqualFunc functions in the maps package.

Also clarify/correct a few doc comments:

* EqualFunc takes an "equality" function, not a "comparison" function
* It's confusing for Delete and DeleteFunc to say they "do not create a
new slice", as they do return a new slice. They already say they
"return the modified slice" which is enough.
* Similar for Compact, and mention that it returns the modified slice
(and say why)
* Note that CompactFunc keeps the first element in equal runs
* Say what cmp is in SortStableFunc and IsSortedFunc
* Say that MinFunc and MaxFunc return the first value

Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
---
A src/maps/example_test.go
A src/slices/example_test.go
M src/slices/slices.go
M src/slices/sort.go
4 files changed, 378 insertions(+), 8 deletions(-)

diff --git a/src/maps/example_test.go b/src/maps/example_test.go
new file mode 100644
index 0000000..779c66d
--- /dev/null
+++ b/src/maps/example_test.go
@@ -0,0 +1,45 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package maps_test
+
+import (
+ "fmt"
+ "maps"
+ "strings"
+)
+
+func ExampleDeleteFunc() {
+ m := map[string]int{
+ "one": 1,
+ "two": 2,
+ "three": 3,
+ "four": 4,
+ }
+ maps.DeleteFunc(m, func(k string, v int) bool {
+ return v%2 != 0 // delete odd values
+ })
+ fmt.Println(m)
+ // Output:
+ // map[four:4 two:2]
+}
+
+func ExampleEqualFunc() {
+ m1 := map[int]string{
+ 1: "one",
+ 10: "Ten",
+ 1000: "THOUSAND",
+ }
+ m2 := map[int][]byte{
+ 1: []byte("One"),
+ 10: []byte("Ten"),
+ 1000: []byte("Thousand"),
+ }
+ eq := maps.EqualFunc(m1, m2, func(v1 string, v2 []byte) bool {
+ return strings.ToLower(v1) == strings.ToLower(string(v2))
+ })
+ fmt.Println(eq)
+ // Output:
+ // true
+}
diff --git a/src/slices/example_test.go b/src/slices/example_test.go
new file mode 100644
index 0000000..16d85d5
--- /dev/null
+++ b/src/slices/example_test.go
@@ -0,0 +1,324 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+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)
+ // Output:
+ // Vera: 2 true
+ // Bill: 1 false
+}
+
+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 cmp.Compare(a.Name, b.Name)
+ })
+ fmt.Println("Bob:", n, found)
+ // Output:
+ // Bob: 1 true
+}
+
+func ExampleCompact() {
+ seq := []int{0, 1, 1, 2, 3, 5, 8}
+ seq = slices.Compact(seq)
+ fmt.Println(seq)
+ // Output:
+ // [0 1 2 3 5 8]
+}
+
+func ExampleCompactFunc() {
+ names := []string{"bob", "Bob", "alice", "Vera", "VERA"}
+ names = slices.CompactFunc(names, func(a, b string) bool {
+ return strings.ToLower(a) == strings.ToLower(b)
+ })
+ fmt.Println(names)
+ // Output:
+ // [bob alice Vera]
+}
+
+func ExampleCompare() {
+ // Using Compare with SortFunc to sort a slice of slices
+ names := [][]byte{[]byte("Bob"), []byte("Alice"), []byte("Vera")}
+ slices.SortFunc(names, func(a, b []byte) int {
+ return slices.Compare(a, b)
+ })
+ fmt.Printf("%s\n", names)
+ // Output:
+ // [Alice Bob Vera]
+}
+
+func ExampleCompareFunc() {
+ numbers := []int{0, 43, 8}
+ strings := []string{"0", "42", "8"}
+ result := slices.CompareFunc(numbers, strings, func(n int, s string) int {
+ sn, err := strconv.ParseInt(s, 0, 64)
+ if err != nil {
+ return 1
+ }
+ return cmp.Compare(n, int(sn))
+ })
+ fmt.Println(result)
+ // Output:
+ // 1
+}
+
+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)
+ // Output:
+ // Has a negative: true
+ // Has an odd number: false
+}
+
+func ExampleDelete() {
+ letters := []string{"a", "b", "c", "d", "e"}
+ letters = slices.Delete(letters, 1, 4)
+ fmt.Println(letters)
+ // Output:
+ // [a e]
+}
+
+func ExampleDeleteFunc() {
+ seq := []int{0, 1, 1, 2, 3, 5, 8}
+ seq = slices.DeleteFunc(seq, func(n int) bool {
+ return n%2 != 0 // delete the odd numbers
+ })
+ fmt.Println(seq)
+ // Output:
+ // [0 2 8]
+}
+
+func ExampleEqual() {
+ numbers := []int{0, 42, 8}
+ fmt.Println(slices.Equal(numbers, []int{0, 42, 8}))
+ fmt.Println(slices.Equal(numbers, []int{10}))
+ // Output:
+ // true
+ // false
+}
+
+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)
+ // Output:
+ // true
+}
+
+func ExampleIndex() {
+ numbers := []int{0, 42, 8}
+ fmt.Println(slices.Index(numbers, 8))
+ fmt.Println(slices.Index(numbers, 7))
+ // Output:
+ // 2
+ // -1
+}
+
+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)
+ // Output:
+ // First negative at index 2
+}
+
+func ExampleInsert() {
+ names := []string{"Alice", "Bob", "Vera"}
+ names = slices.Insert(names, 1, "Bill", "Billie")
+ names = slices.Insert(names, len(names), "Zac")
+ fmt.Println(names)
+ // Output:
+ // [Alice Bill Billie Bob Vera Zac]
+}
+
+func ExampleIsSorted() {
+ fmt.Println(slices.IsSorted([]string{"Alice", "Bob", "Vera"}))
+ fmt.Println(slices.IsSorted([]int{0, 2, 1}))
+ // Output:
+ // true
+ // false
+}
+
+func ExampleIsSortedFunc() {
+ names := []string{"alice", "Bob", "VERA"}
+ isSortedInsensitive := slices.IsSortedFunc(names, func(a, b string) int {
+ return cmp.Compare(strings.ToLower(a), strings.ToLower(b))
+ })
+ fmt.Println(isSortedInsensitive)
+ fmt.Println(slices.IsSorted(names))
+ // Output:
+ // true
+ // false
+}
+
+func ExampleMax() {
+ numbers := []int{0, 42, -10, 8}
+ fmt.Println(slices.Max(numbers))
+ // Output:
+ // 42
+}
+
+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)
+ // Output:
+ // Alice
+}
+
+func ExampleMin() {
+ numbers := []int{0, 42, -10, 8}
+ fmt.Println(slices.Min(numbers))
+ // Output:
+ // -10
+}
+
+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)
+ // Output:
+ // Bob
+}
+
+func ExampleReplace() {
+ names := []string{"Alice", "Bob", "Vera", "Zac"}
+ names = slices.Replace(names, 1, 3, "Bill", "Billie", "Cat")
+ fmt.Println(names)
+ // Output:
+ // [Alice Bill Billie Cat Zac]
+}
+
+func ExampleReverse() {
+ names := []string{"alice", "Bob", "VERA"}
+ slices.Reverse(names)
+ fmt.Println(names)
+ // Output:
+ // [VERA Bob alice]
+}
+
+func ExampleSort() {
+ smallInts := []int8{0, 42, -10, 8}
+ slices.Sort(smallInts)
+ fmt.Println(smallInts)
+ // Output:
+ // [-10 0 8 42]
+}
+
+func ExampleSortFunc_caseInsensitive() {
+ names := []string{"Bob", "alice", "VERA"}
+ slices.SortFunc(names, func(a, b string) int {
+ return cmp.Compare(strings.ToLower(a), strings.ToLower(b))
+ })
+ fmt.Println(names)
+ // Output:
+ // [alice Bob VERA]
+}
+
+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 := cmp.Compare(a.Name, b.Name); n != 0 {
+ return n
+ }
+ // If names are equal, order by age
+ return cmp.Compare(a.Age, b.Age)
+ })
+ fmt.Println(people)
+ // Output:
+ // [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
+}
+
+func ExampleSortStableFunc() {
+ type Person struct {
+ Name string
+ Age int
+ }
+ people := []Person{
+ {"Gopher", 13},
+ {"Alice", 55},
+ {"Bob", 24},
+ {"Alice", 20},
+ }
+ // First sort by age
+ slices.SortFunc(people, func(a, b Person) int {
+ return cmp.Compare(a.Age, b.Age)
+ })
+ // Then stable sort by name, keeping age ordering intact
+ slices.SortStableFunc(people, func(a, b Person) int {
+ return cmp.Compare(a.Name, b.Name)
+ })
+ fmt.Println(people)
+ // Output:
+ // [{Alice 20} {Alice 55} {Bob 24} {Gopher 13}]
+}
diff --git a/src/slices/slices.go b/src/slices/slices.go
index c8eacae..98c13f2 100644
--- a/src/slices/slices.go
+++ b/src/slices/slices.go
@@ -27,7 +27,7 @@
return true
}

-// EqualFunc reports whether two slices are equal using a comparison
+// EqualFunc reports whether two slices are equal using an equality
// function on each pair of elements. If the lengths are different,
// EqualFunc returns false. Otherwise, the elements are compared in
// increasing index order, and the comparison stops at the first index
@@ -210,7 +210,6 @@

// Delete removes the elements s[i:j] from s, returning the modified slice.
// Delete panics if s[i:j] is not a valid slice of s.
-// Delete modifies the contents of the slice s; it does not create a new slice.
// Delete is O(len(s)-j), so if many items must be deleted, it is better to
// make a single call deleting them all together than to delete one at a time.
// Delete might not modify the elements s[len(s)-(j-i):len(s)]. If those
@@ -224,8 +223,6 @@

// DeleteFunc removes any elements from s for which del returns true,
// returning the modified slice.
-// DeleteFunc modifies the contents of the slice s;
-// it does not create a new slice.
// When DeleteFunc removes m elements, it might not modify the elements
// s[len(s)-m:len(s)]. If those elements contain pointers you might consider
// zeroing those elements so that objects they reference can be garbage
@@ -348,7 +345,8 @@

// Compact replaces consecutive runs of equal elements with a single copy.
// This is like the uniq command found on Unix.
-// Compact modifies the contents of the slice s; it does not create a new slice.
+// Compact modifies the contents of the slice s and returns the modified slice,
+// which may have a smaller length.
// When Compact discards m elements in total, it might not modify the elements
// s[len(s)-m:len(s)]. If those elements contain pointers you might consider
// zeroing those elements so that objects they reference can be garbage collected.
@@ -368,7 +366,8 @@
return s[:i]
}

-// CompactFunc is like Compact but uses a comparison function.
+// CompactFunc is like Compact but uses an equality function to compare elements.
+// For runs of elements that compare equal, CompactFunc keeps the first one.
func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
if len(s) < 2 {
return s
diff --git a/src/slices/sort.go b/src/slices/sort.go
index 24fc6e2..828cc63 100644
--- a/src/slices/sort.go
+++ b/src/slices/sort.go
@@ -29,7 +29,7 @@
}

// SortStableFunc sorts the slice x while keeping the original order of equal
-// elements, using cmp to compare elements.
+// elements, using cmp to compare elements in the same way as [SortFunc].
func SortStableFunc[S ~[]E, E any](x S, cmp func(a, b E) int) {
stableCmpFunc(x, len(x), cmp)
}
@@ -45,7 +45,7 @@
}

// IsSortedFunc reports whether x is sorted in ascending order, with cmp as the
-// comparison function.
+// comparison function as defined by [SortFunc].
func IsSortedFunc[S ~[]E, E any](x S, cmp func(a, b E) int) bool {
for i := len(x) - 1; i > 0; i-- {
if cmp(x[i], x[i-1]) < 0 {
@@ -70,6 +70,7 @@
}

// MinFunc returns the minimal value in x, using cmp to compare elements.
+// If there are multiple minimal values, MinFunc returns the first one.
// It panics if x is empty.
func MinFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E {
if len(x) < 1 {
@@ -99,6 +100,7 @@
}

// MaxFunc returns the maximal value in x, using cmp to compare elements.
+// If there are multiple maximal values, MaxFunc returns the first one.
// It panics if x is empty.
func MaxFunc[S ~[]E, E any](x S, cmp func(a, b E) int) E {
if len(x) < 1 {

To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
Gerrit-Change-Number: 505095
Gerrit-PatchSet: 1
Gerrit-Owner: Ben Hoyt <ben...@gmail.com>

Aaron Beitch (Gerrit)

unread,
Jun 22, 2023, 2:04:41 PM6/22/23
to Ben Hoyt, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Ben Hoyt.

View Change

2 comments:

  • File src/slices/example_test.go:

    • Patch Set #1, Line 66: return slices.Compare(a, b)

      If I were writing this code I would use bytes.Compare here, so I don't think this is the best example.

      A simpler example may be better here. For example, compare two string slices that have all equal elements, compare two string slices on that have differing numbers of elements, etc.

    • Patch Set #1, Line 313:

    • 	// First sort by age

    • 	slices.SortFunc(people, func(a, b Person) int {

    • 		return cmp.Compare(a.Age, b.Age)
      })


    • // Then stable sort by name, keeping age ordering intact

    • 	slices.SortStableFunc(people, func(a, b Person) int {

    • 		return cmp.Compare(a.Name, b.Name)
      })

      You could simplify by removing the SortFunc call, and just focus on the behavior of SortStableFunc when some values may be equal.

To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
Gerrit-Change-Number: 505095
Gerrit-PatchSet: 1
Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
Gerrit-CC: Aaron Beitch <aar...@arista.com>
Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
Gerrit-Comment-Date: Thu, 22 Jun 2023 18:04:37 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

Ian Lance Taylor (Gerrit)

unread,
Jun 22, 2023, 8:09:08 PM6/22/23
to Ben Hoyt, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

Attention is currently required from: Ben Hoyt.

Patch set 1:Run-TryBot +1

View Change

    To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
    Gerrit-Change-Number: 505095
    Gerrit-PatchSet: 1
    Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Aaron Beitch <aar...@arista.com>
    Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
    Gerrit-Comment-Date: Fri, 23 Jun 2023 00:09:06 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes

    Ben Hoyt (Gerrit)

    unread,
    Jun 22, 2023, 9:17:44 PM6/22/23
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Ben Hoyt, Ian Lance Taylor.

    Ben Hoyt uploaded patch set #2 to this change.

    View Change

    The following approvals got outdated and were removed: Run-TryBot+1 by Ian Lance Taylor, TryBot-Result+1 by Gopher Robot

    slices, maps: add examples; doc comment fixes

    There are currently no examples in the new slices and maps package, so
    add some. This adds examples for most functions in the slices package
    except the very obvious ones, and adds examples for the DeleteFunc and
    EqualFunc functions in the maps package.

    Also clarify/correct a few doc comments:

    * EqualFunc takes an "equality" function, not a "comparison" function
    * It's confusing for Delete and DeleteFunc to say they "do not create a
    new slice", as they do return a new slice. They already say they
    "return the modified slice" which is enough.
    * Similar for Compact, and mention that it returns the modified slice
    (and say why)
    * Note that CompactFunc keeps the first element in equal runs
    * Say what cmp is in SortStableFunc and IsSortedFunc
    * Say that MinFunc and MaxFunc return the first value

    Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
    ---
    A src/maps/example_test.go
    A src/slices/example_test.go
    M src/slices/slices.go
    M src/slices/sort.go
    4 files changed, 376 insertions(+), 8 deletions(-)

    To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
    Gerrit-Change-Number: 505095
    Gerrit-PatchSet: 2
    Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Aaron Beitch <aar...@arista.com>
    Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>

    Ben Hoyt (Gerrit)

    unread,
    Jun 22, 2023, 9:22:18 PM6/22/23
    to goph...@pubsubhelper.golang.org, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

    Attention is currently required from: Aaron Beitch, Ian Lance Taylor.

    View Change

    2 comments:

    • File src/slices/example_test.go:

      • If I were writing this code I would use bytes. […]

        Yeah, good call. And I realised it's unnecessary to wrap a function call in a function literal anyway; you can just specify bytes.Compare or slices.Compare directly. I've changed to a more direct example.

      • Patch Set #1, Line 313:

        	// First sort by age
        slices.SortFunc(people, func(a, b Person) int {
        return cmp.Compare(a.Age, b.Age)
        })
        // Then stable sort by name, keeping age ordering intact
        slices.SortStableFunc(people, func(a, b Person) int {
        return cmp.Compare(a.Name, b.Name)
        })

      • You could simplify by removing the SortFunc call, and just focus on the behavior of SortStableFunc w […]

        Done

    To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
    Gerrit-Change-Number: 505095
    Gerrit-PatchSet: 2
    Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Aaron Beitch <aar...@arista.com>
    Gerrit-Attention: Aaron Beitch <aar...@arista.com>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Comment-Date: Fri, 23 Jun 2023 01:22:12 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Aaron Beitch <aar...@arista.com>

    Ian Lance Taylor (Gerrit)

    unread,
    Jun 22, 2023, 11:33:20 PM6/22/23
    to Ben Hoyt, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Gopher Robot, Aaron Beitch, golang-co...@googlegroups.com

    Attention is currently required from: Aaron Beitch, Ben Hoyt.

    Patch set 2:Run-TryBot +1

    View Change

      To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      Gerrit-Change-Number: 505095
      Gerrit-PatchSet: 2
      Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
      Gerrit-Comment-Date: Fri, 23 Jun 2023 03:33:17 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes

      Ian Lance Taylor (Gerrit)

      unread,
      Jun 23, 2023, 5:27:10 PM6/23/23
      to Ben Hoyt, goph...@pubsubhelper.golang.org, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

      Attention is currently required from: Aaron Beitch, Ben Hoyt.

      View Change

      2 comments:

      • File src/slices/example_test.go:

        • Patch Set #2, Line 77: strings := []string{"0", "42", "8"}

          Maybe this should be "0", "0", "8" to avoid any confusion from the fact that 43 and 42 look pretty similar.

        • Patch Set #2, Line 79: sn, err := strconv.ParseInt(s, 0, 64)

          Simpler to use strconv.Atoi here. Then you don't need to conversion to int in the call to cmp.Compare.

      To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      Gerrit-Change-Number: 505095
      Gerrit-PatchSet: 2
      Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
      Gerrit-Comment-Date: Fri, 23 Jun 2023 21:27:07 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No

      Ben Hoyt (Gerrit)

      unread,
      Jun 23, 2023, 6:41:45 PM6/23/23
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Attention is currently required from: Aaron Beitch, Ben Hoyt, Ian Lance Taylor.

      Ben Hoyt uploaded patch set #3 to this change.

      View Change

      The following approvals got outdated and were removed: Run-TryBot+1 by Ian Lance Taylor, TryBot-Result+1 by Gopher Robot

      slices, maps: add examples; doc comment fixes

      There are currently no examples in the new slices and maps package, so
      add some. This adds examples for most functions in the slices package
      except the very obvious ones, and adds examples for the DeleteFunc and
      EqualFunc functions in the maps package.

      Also clarify/correct a few doc comments:

      * EqualFunc takes an "equality" function, not a "comparison" function
      * It's confusing for Delete and DeleteFunc to say they "do not create a
      new slice", as they do return a new slice. They already say they
      "return the modified slice" which is enough.
      * Similar for Compact, and mention that it returns the modified slice
      (and say why)
      * Note that CompactFunc keeps the first element in equal runs
      * Say what cmp is in SortStableFunc and IsSortedFunc
      * Say that MinFunc and MaxFunc return the first value

      Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      ---
      A src/maps/example_test.go
      A src/slices/example_test.go
      M src/slices/slices.go
      M src/slices/sort.go
      4 files changed, 376 insertions(+), 8 deletions(-)

      To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      Gerrit-Change-Number: 505095
      Gerrit-PatchSet: 3
      Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Ben Hoyt <ben...@gmail.com>

      Ben Hoyt (Gerrit)

      unread,
      Jun 23, 2023, 6:42:41 PM6/23/23
      to goph...@pubsubhelper.golang.org, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

      Attention is currently required from: Aaron Beitch, Ian Lance Taylor.

      View Change

      2 comments:

      • File src/slices/example_test.go:

        • Maybe this should be "0", "0", "8" to avoid any confusion from the fact that 43 and 42 look pretty s […]

          Done

        • Simpler to use strconv.Atoi here. Then you don't need to conversion to int in the call to cmp. […]

          Done

      To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      Gerrit-Change-Number: 505095
      Gerrit-PatchSet: 3
      Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Comment-Date: Fri, 23 Jun 2023 22:42:36 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Ian Lance Taylor <ia...@golang.org>

      Ian Lance Taylor (Gerrit)

      unread,
      Jun 23, 2023, 8:51:18 PM6/23/23
      to Ben Hoyt, goph...@pubsubhelper.golang.org, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

      Attention is currently required from: Aaron Beitch, Ben Hoyt.

      View Change

      1 comment:

      • Patchset:

        • Patch Set #3:

          I wanted to look down the MaxFunc/MinFunc behavior with a test, so now this has a merge conflict. Please rebase to HEAD. Sorry about that.

      To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      Gerrit-Change-Number: 505095
      Gerrit-PatchSet: 3
      Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
      Gerrit-Comment-Date: Sat, 24 Jun 2023 00:51:15 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No

      Ben Hoyt (Gerrit)

      unread,
      Jun 24, 2023, 4:03:57 AM6/24/23
      to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Attention is currently required from: Aaron Beitch, Ben Hoyt.

      Ben Hoyt uploaded patch set #4 to this change.

      View Change

      slices, maps: add examples; doc comment fixes


      There are currently no examples in the new slices and maps package, so
      add some. This adds examples for most functions in the slices package
      except the very obvious ones, and adds examples for the DeleteFunc and
      EqualFunc functions in the maps package.

      Also clarify/correct a few doc comments:

      * EqualFunc takes an "equality" function, not a "comparison" function
      * It's confusing for Delete and DeleteFunc to say they "do not create a
      new slice", as they do return a new slice. They already say they
      "return the modified slice" which is enough.
      * Similar for Compact, and mention that it returns the modified slice
      (and say why)
      * Note that CompactFunc keeps the first element in equal runs
      * Say what cmp is in SortStableFunc and IsSortedFunc
      * Say that MinFunc and MaxFunc return the first value

      Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      ---
      A src/maps/example_test.go
      A src/slices/example_test.go
      M src/slices/slices.go
      M src/slices/sort.go
      4 files changed, 374 insertions(+), 8 deletions(-)

      To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      Gerrit-Change-Number: 505095
      Gerrit-PatchSet: 4

      Ben Hoyt (Gerrit)

      unread,
      Jun 24, 2023, 4:07:19 AM6/24/23
      to goph...@pubsubhelper.golang.org, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

      Attention is currently required from: Aaron Beitch, Ian Lance Taylor.

      View Change

      1 comment:

      • Patchset:

        • Patch Set #3:

          I wanted to look down the MaxFunc/MinFunc behavior with a test, so now this has a merge conflict. […]

          No problemo -- done.

      To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
      Gerrit-Change-Number: 505095
      Gerrit-PatchSet: 4
      Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Aaron Beitch <aar...@arista.com>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Comment-Date: Sat, 24 Jun 2023 08:07:14 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No

      Ian Lance Taylor (Gerrit)

      unread,
      Jun 24, 2023, 11:43:38 AM6/24/23
      to Ben Hoyt, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Gopher Robot, Aaron Beitch, golang-co...@googlegroups.com

      Attention is currently required from: Aaron Beitch, Ben Hoyt.

      Patch set 4:Run-TryBot +1

      View Change

        To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
        Gerrit-Change-Number: 505095
        Gerrit-PatchSet: 4
        Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-CC: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
        Gerrit-Comment-Date: Sat, 24 Jun 2023 15:43:34 +0000
        Gerrit-HasComments: No
        Gerrit-Has-Labels: Yes

        Ben Hoyt (Gerrit)

        unread,
        Jun 26, 2023, 4:09:36 PM6/26/23
        to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

        Attention is currently required from: Aaron Beitch, Ben Hoyt, Ian Lance Taylor.

        Ben Hoyt uploaded patch set #5 to this change.

        View Change

        The following approvals got outdated and were removed: Run-TryBot+1 by Ian Lance Taylor, TryBot-Result+1 by Gopher Robot

        slices, maps: add examples; doc comment fixes


        There are currently no examples in the new slices and maps package, so
        add some. This adds examples for most functions in the slices package
        except the very obvious ones, and adds examples for the DeleteFunc and
        EqualFunc functions in the maps package.

        Also clarify/correct a few doc comments:

        * EqualFunc takes an "equality" function, not a "comparison" function
        * It's confusing for Delete and DeleteFunc to say they "do not create a
        new slice", as they do return a new slice. They already say they
        "return the modified slice" which is enough.
        * Similar for Compact, and mention that it returns the modified slice
        (and say why)
        * Note that CompactFunc keeps the first element in equal runs
        * Say what cmp is in SortStableFunc and IsSortedFunc
        * Say that MinFunc and MaxFunc return the first value

        Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
        ---
        A src/maps/example_test.go
        A src/slices/example_test.go
        M src/slices/slices.go
        M src/slices/sort.go
        4 files changed, 374 insertions(+), 8 deletions(-)

        To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: newpatchset
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
        Gerrit-Change-Number: 505095
        Gerrit-PatchSet: 5
        Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-CC: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Ben Hoyt <ben...@gmail.com>

        Ian Lance Taylor (Gerrit)

        unread,
        Jun 26, 2023, 8:41:37 PM6/26/23
        to Ben Hoyt, goph...@pubsubhelper.golang.org, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

        Attention is currently required from: Aaron Beitch, Ben Hoyt, Ian Lance Taylor.

        Patch set 5:Run-TryBot +1Auto-Submit +1Code-Review +2

        View Change

        1 comment:

        To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
        Gerrit-Change-Number: 505095
        Gerrit-PatchSet: 5
        Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
        Gerrit-CC: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
        Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Comment-Date: Tue, 27 Jun 2023 00:41:33 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: Yes

        Emmanuel Odeke (Gerrit)

        unread,
        Jun 28, 2023, 6:26:43 PM6/28/23
        to Ben Hoyt, goph...@pubsubhelper.golang.org, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

        Attention is currently required from: Aaron Beitch, Ben Hoyt, Ian Lance Taylor.

        Patch set 5:Code-Review +2

        View Change

        1 comment:

        To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
        Gerrit-Change-Number: 505095
        Gerrit-PatchSet: 5
        Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
        Gerrit-Reviewer: Emmanuel Odeke <emma...@orijtech.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
        Gerrit-CC: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Aaron Beitch <aar...@arista.com>
        Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
        Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Comment-Date: Wed, 28 Jun 2023 22:26:39 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: Yes

        Carlos Amedee (Gerrit)

        unread,
        Jun 29, 2023, 11:34:18 AM6/29/23
        to Ben Hoyt, goph...@pubsubhelper.golang.org, Emmanuel Odeke, Gopher Robot, Ian Lance Taylor, Aaron Beitch, golang-co...@googlegroups.com

        Attention is currently required from: Aaron Beitch, Ben Hoyt, Ian Lance Taylor.

        Patch set 5:Code-Review +1

        View Change

          To view, visit change 505095. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-MessageType: comment
          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: I59c7bb1c7cabc4986d81018a5aaf5b712d3310f2
          Gerrit-Change-Number: 505095
          Gerrit-PatchSet: 5
          Gerrit-Owner: Ben Hoyt <ben...@gmail.com>
          Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
          Gerrit-Reviewer: Emmanuel Odeke <emma...@orijtech.com>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
          Gerrit-CC: Aaron Beitch <aar...@arista.com>
          Gerrit-Attention: Aaron Beitch <aar...@arista.com>
          Gerrit-Attention: Ben Hoyt <ben...@gmail.com>
          Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
          Gerrit-Comment-Date: Thu, 29 Jun 2023 15:34:12 +0000
          Gerrit-HasComments: No
          Gerrit-Has-Labels: Yes
          Reply all
          Reply to author
          Forward
          0 new messages