[go] slices: add examples for iterator-related functions

14 views
Skip to first unread message

jiahua wang (Gerrit)

unread,
Aug 7, 2024, 2:46:09 AM8/7/24
to goph...@pubsubhelper.golang.org, jiahua wang, golang-co...@googlegroups.com

jiahua wang has uploaded the change for review

Commit message

slices: add examples for iterator-related functions
Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c

Change diff

diff --git a/src/slices/example_test.go b/src/slices/example_test.go
index cb601ad..73d31e6 100644
--- a/src/slices/example_test.go
+++ b/src/slices/example_test.go
@@ -385,6 +385,125 @@
// [0 1 2 3 0 1 2 3]
}

+func ExampleAll() {
+ names := []string{"Alice", "Bob", "Vera"}
+ for i, v := range slices.All(names) {
+ fmt.Println(i, ":", v)
+ }
+ // Output:
+ // 0 : Alice
+ // 1 : Bob
+ // 2 : Vera
+}
+
+func ExampleBackward() {
+ names := []string{"Alice", "Bob", "Vera"}
+ for i, v := range slices.Backward(names) {
+ fmt.Println(i, ":", v)
+ }
+ // Output:
+ // 2 : Vera
+ // 1 : Bob
+ // 0 : Alice
+}
+
+func ExampleValues() {
+ names := []string{"Alice", "Bob", "Vera"}
+ for v := range slices.Values(names) {
+ fmt.Println(v)
+ }
+ // Output:
+ // Alice
+ // Bob
+ // Vera
+}
+
+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)
+ // Output:
+ // [1 2 0 2 4 6 8]
+}
+
+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)
+ // Output:
+ // [0 2 4 6 8]
+}
+
+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))
+ // Output:
+ // [-6 -2 0 4 8]
+ // true
+}
+
+func ExampleSortedFunc() {
+ seq := func(yield func(int) bool) {
+ flag := -1
+ for i := 0; i < 10; i += 2 {
+ flag = -flag
+ if !yield(i * flag) {
+ return
+ }
+ }
+ }
+
+ s := slices.SortedFunc(seq, func(a, b int) int { return b - a })
+ fmt.Println(s)
+ // Output:
+ // [8 4 0 -2 -6]
+}
+
+func ExampleSortedStableFunc() {
+ type Person struct {
+ Name string
+ Age int
+ }
+
+ people := []Person{
+ {"Gopher", 13},
+ {"Alice", 20},
+ {"Bob", 5},
+ {"Vera", 24},
+ {"Zac", 15},
+ }
+
+ s := slices.SortedStableFunc(slices.Values(people[:]), func(x, y Person) int { return x.Age - y.Age })
+ fmt.Println(s)
+ // Output:
+ // [{Bob 5} {Gopher 13} {Zac 15} {Alice 20} {Vera 24}]
+}
+
func ExampleChunk() {
type Person struct {
Name string

Change information

Files:
  • M src/slices/example_test.go
Change size: M
Delta: 1 file changed, 119 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
Gerrit-Change-Number: 603735
Gerrit-PatchSet: 1
Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Ian Lance Taylor (Gerrit)

unread,
Aug 7, 2024, 4:35:06 PM8/7/24
to jiahua wang, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Eli Bendersky‎, Gopher Robot, golang-co...@googlegroups.com
Attention needed from jiahua wang

Ian Lance Taylor voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • jiahua wang
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
Gerrit-Change-Number: 603735
Gerrit-PatchSet: 1
Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Eli Bendersky‎ <eli...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
Gerrit-Comment-Date: Wed, 07 Aug 2024 20:35:02 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Ian Lance Taylor (Gerrit)

unread,
Aug 7, 2024, 6:38:40 PM8/7/24
to jiahua wang, goph...@pubsubhelper.golang.org, Go LUCI, Ian Lance Taylor, Eli Bendersky‎, Gopher Robot, golang-co...@googlegroups.com
Attention needed from jiahua wang

Ian Lance Taylor added 2 comments

File src/slices/example_test.go
Line 481, Patchset 1 (Latest): s := slices.SortedFunc(seq, func(a, b int) int { return b - a })
Ian Lance Taylor . unresolved

"return b - a" is a bit hard to reason about and is not the best example. How about

// Use cmp(b, a), not cmp(a, b), to reverse the usual sort ordering.
sortFunc := func(a, b int) int { return cmp.Compare(b, a) }

s := slices.SortedFunc(seq, sortFunc)

Line 501, Patchset 1 (Latest): s := slices.SortedStableFunc(slices.Values(people[:]), func(x, y Person) int { return x.Age - y.Age })
Ian Lance Taylor . unresolved

Similar comment here. But more importantly, the difference between SortedFunc and SortedStableFunc is that ordering of equal elements stays the same. But there are no equal elements here. The example should have some.

Open in Gerrit

Related details

Attention is currently required from:
  • jiahua wang
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
    Gerrit-Change-Number: 603735
    Gerrit-PatchSet: 1
    Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
    Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
    Gerrit-CC: Eli Bendersky‎ <eli...@golang.org>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
    Gerrit-Comment-Date: Wed, 07 Aug 2024 22:38:34 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    satisfied_requirement
    open
    diffy

    jiahua wang (Gerrit)

    unread,
    Aug 8, 2024, 1:16:03 PM8/8/24
    to jiahua wang, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from jiahua wang

    jiahua wang uploaded new patchset

    jiahua wang uploaded patch set #2 to this change.
    Following approvals got outdated and were removed:
    • TryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
    Open in Gerrit

    Related details

    Attention is currently required from:
    • jiahua wang
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
      Gerrit-Change-Number: 603735
      Gerrit-PatchSet: 2
      unsatisfied_requirement
      open
      diffy

      Ian Lance Taylor (Gerrit)

      unread,
      Aug 8, 2024, 7:20:57 PM8/8/24
      to jiahua wang, goph...@pubsubhelper.golang.org, Go LUCI, Ian Lance Taylor, Eli Bendersky‎, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from jiahua wang

      Ian Lance Taylor added 4 comments

      File src/slices/example_test.go
      Line 481, Patchset 1: s := slices.SortedFunc(seq, func(a, b int) int { return b - a })
      Ian Lance Taylor . resolved

      "return b - a" is a bit hard to reason about and is not the best example. How about

      // Use cmp(b, a), not cmp(a, b), to reverse the usual sort ordering.
      sortFunc := func(a, b int) int { return cmp.Compare(b, a) }

      s := slices.SortedFunc(seq, sortFunc)

      Ian Lance Taylor

      Done

      Line 482, Patchset 2 (Latest): return cmp.Compare(b, a)
      Ian Lance Taylor . unresolved

      Please add a comment pointing out that the comparison is being done in reverse. Thanks.

      Line 501, Patchset 1: s := slices.SortedStableFunc(slices.Values(people[:]), func(x, y Person) int { return x.Age - y.Age })
      Ian Lance Taylor . resolved

      Similar comment here. But more importantly, the difference between SortedFunc and SortedStableFunc is that ordering of equal elements stays the same. But there are no equal elements here. The example should have some.

      Ian Lance Taylor

      Done

      Line 509, Patchset 2 (Latest): s := slices.SortedStableFunc(slices.Values(people[:]), sortFunc)
      Ian Lance Taylor . unresolved

      No need for [:] here.

      Open in Gerrit

      Related details

      Attention is currently required from:
      • jiahua wang
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
      Gerrit-Change-Number: 603735
      Gerrit-PatchSet: 2
      Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Eli Bendersky‎ <eli...@golang.org>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
      Gerrit-Comment-Date: Thu, 08 Aug 2024 23:20:51 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Ian Lance Taylor <ia...@golang.org>
      unsatisfied_requirement
      open
      diffy

      jiahua wang (Gerrit)

      unread,
      Aug 9, 2024, 6:11:45 AM8/9/24
      to jiahua wang, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from jiahua wang

      jiahua wang uploaded new patchset

      jiahua wang uploaded patch set #3 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • jiahua wang
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement is not satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
      Gerrit-Change-Number: 603735
      Gerrit-PatchSet: 3
      unsatisfied_requirement
      open
      diffy

      Ian Lance Taylor (Gerrit)

      unread,
      Aug 9, 2024, 3:58:14 PM8/9/24
      to jiahua wang, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Go LUCI, Eli Bendersky‎, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from jiahua wang

      Ian Lance Taylor voted and added 2 comments

      Votes added by Ian Lance Taylor

      Commit-Queue+1

      2 comments

      File src/slices/example_test.go
      Line 482, Patchset 2: return cmp.Compare(b, a)
      Ian Lance Taylor . resolved

      Please add a comment pointing out that the comparison is being done in reverse. Thanks.

      Ian Lance Taylor

      Done

      Line 509, Patchset 2: s := slices.SortedStableFunc(slices.Values(people[:]), sortFunc)
      Ian Lance Taylor . resolved

      No need for [:] here.

      Ian Lance Taylor

      Done

      Open in Gerrit

      Related details

      Attention is currently required from:
      • jiahua wang
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
        Gerrit-Change-Number: 603735
        Gerrit-PatchSet: 3
        Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-CC: Eli Bendersky‎ <eli...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
        Gerrit-Comment-Date: Fri, 09 Aug 2024 19:58:09 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: Yes
        unsatisfied_requirement
        satisfied_requirement
        open
        diffy

        Ian Lance Taylor (Gerrit)

        unread,
        Aug 9, 2024, 6:05:27 PM8/9/24
        to jiahua wang, goph...@pubsubhelper.golang.org, Go LUCI, Ian Lance Taylor, Eli Bendersky‎, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from jiahua wang

        Ian Lance Taylor voted and added 1 comment

        Votes added by Ian Lance Taylor

        Auto-Submit+1
        Code-Review+2
        Commit-Queue+1

        1 comment

        Patchset-level comments
        Ian Lance Taylor . resolved

        Related details

        Attention is currently required from:
        • jiahua wang
        Submit Requirements:
        • requirement satisfiedCode-Review
        • requirement satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
        Gerrit-Change-Number: 603735
        Gerrit-PatchSet: 3
        Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
        Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
        Gerrit-CC: Eli Bendersky‎ <eli...@golang.org>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
        Gerrit-Comment-Date: Fri, 09 Aug 2024 22:05:20 +0000
        Gerrit-HasComments: Yes
        Gerrit-Has-Labels: Yes
        satisfied_requirement
        unsatisfied_requirement
        open
        diffy

        Carlos Amedee (Gerrit)

        unread,
        Aug 14, 2024, 2:06:39 PM8/14/24
        to jiahua wang, goph...@pubsubhelper.golang.org, Go LUCI, Ian Lance Taylor, Eli Bendersky‎, Gopher Robot, golang-co...@googlegroups.com
        Attention needed from jiahua wang

        Carlos Amedee voted Code-Review+1

        Code-Review+1
        Open in Gerrit

        Related details

        Attention is currently required from:
        • jiahua wang
        Submit Requirements:
          • requirement satisfiedCode-Review
          • requirement satisfiedNo-Unresolved-Comments
          • requirement satisfiedReview-Enforcement
          • requirement satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: comment
          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
          Gerrit-Change-Number: 603735
          Gerrit-PatchSet: 3
          Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
          Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
          Gerrit-Reviewer: Ian Lance Taylor <ia...@google.com>
          Gerrit-CC: Eli Bendersky‎ <eli...@golang.org>
          Gerrit-CC: Gopher Robot <go...@golang.org>
          Gerrit-Attention: jiahua wang <wjh1...@gmail.com>
          Gerrit-Comment-Date: Wed, 14 Aug 2024 18:06:33 +0000
          Gerrit-HasComments: No
          Gerrit-Has-Labels: Yes
          satisfied_requirement
          open
          diffy

          Gopher Robot (Gerrit)

          unread,
          Aug 14, 2024, 2:07:29 PM8/14/24
          to jiahua wang, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Carlos Amedee, Go LUCI, Ian Lance Taylor, Eli Bendersky‎, golang-co...@googlegroups.com

          Gopher Robot submitted the change

          Change information

          Commit message:
          slices: add examples for iterator-related functions
          Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
          Reviewed-by: Ian Lance Taylor <ia...@google.com>
          Reviewed-by: Carlos Amedee <car...@golang.org>
          Auto-Submit: Ian Lance Taylor <ia...@google.com>
          Files:
          • M src/slices/example_test.go
          Change size: M
          Delta: 1 file changed, 127 insertions(+), 0 deletions(-)
          Branch: refs/heads/master
          Submit Requirements:
          • requirement satisfiedCode-Review: +1 by Carlos Amedee, +2 by Ian Lance Taylor
          • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
          Open in Gerrit
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: merged
          Gerrit-Project: go
          Gerrit-Branch: master
          Gerrit-Change-Id: I13e878579b51638c2c07ad3ea99be7276177875c
          Gerrit-Change-Number: 603735
          Gerrit-PatchSet: 4
          Gerrit-Owner: jiahua wang <wjh1...@gmail.com>
          Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
          Gerrit-Reviewer: Gopher Robot <go...@golang.org>
          open
          diffy
          satisfied_requirement
          Reply all
          Reply to author
          Forward
          0 new messages