mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
iter: minor doc comment updates
Remove old return value. Use single variable range for iter.Seq[V]. Rewrite Pairs implementation to not loop forever. Fixes #68056 Fixes #68073 Change-Id: I7ede0fe8ed058bbd57869d87e17b7f2c3641f7dd Reviewed-on: https://go-review.googlesource.com/c/go/+/593555 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Damien Neil <dneil@google.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Commit-Queue: Ian Lance Taylor <iant@google.com> Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
This commit is contained in:
parent
d73a8a206a
commit
fed2c11d67
1 changed files with 16 additions and 10 deletions
|
|
@ -31,7 +31,7 @@ element in the sequence, false if it should stop.
|
||||||
Iterator functions are most often called by a range loop, as in:
|
Iterator functions are most often called by a range loop, as in:
|
||||||
|
|
||||||
func PrintAll[V any](seq iter.Seq[V]) {
|
func PrintAll[V any](seq iter.Seq[V]) {
|
||||||
for _, v := range seq {
|
for v := range seq {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -92,9 +92,8 @@ sequence only once. These “single-use iterators” typically report values
|
||||||
from a data stream that cannot be rewound to start over.
|
from a data stream that cannot be rewound to start over.
|
||||||
Calling the iterator again after stopping early may continue the
|
Calling the iterator again after stopping early may continue the
|
||||||
stream, but calling it again after the sequence is finished will yield
|
stream, but calling it again after the sequence is finished will yield
|
||||||
no values at all, immediately returning true. Doc comments for
|
no values at all. Doc comments for functions or methods that return
|
||||||
functions or methods that return single-use iterators should document
|
single-use iterators should document this fact:
|
||||||
this fact:
|
|
||||||
|
|
||||||
// Lines returns an iterator over lines read from r.
|
// Lines returns an iterator over lines read from r.
|
||||||
// It returns a single-use iterator.
|
// It returns a single-use iterator.
|
||||||
|
|
@ -119,17 +118,24 @@ For example:
|
||||||
|
|
||||||
// Pairs returns an iterator over successive pairs of values from seq.
|
// Pairs returns an iterator over successive pairs of values from seq.
|
||||||
func Pairs[V any](seq iter.Seq[V]) iter.Seq2[V, V] {
|
func Pairs[V any](seq iter.Seq[V]) iter.Seq2[V, V] {
|
||||||
return func(yield func(V, V) bool) bool {
|
return func(yield func(V, V) bool) {
|
||||||
next, stop := iter.Pull(seq)
|
next, stop := iter.Pull(seq)
|
||||||
defer stop()
|
defer stop()
|
||||||
v1, ok1 := next()
|
for {
|
||||||
v2, ok2 := next()
|
v1, ok1 := next()
|
||||||
for ok1 || ok2 {
|
if !ok1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
v2, ok2 := next()
|
||||||
|
// If ok2 is false, v2 should be the
|
||||||
|
// zero value; yield one last pair.
|
||||||
if !yield(v1, v2) {
|
if !yield(v1, v2) {
|
||||||
return false
|
return
|
||||||
|
}
|
||||||
|
if !ok2 {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue