Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iter: Pull example in package doc loops forever #68073

Closed
thepudds opened this issue Jun 19, 2024 · 6 comments
Closed

iter: Pull example in package doc loops forever #68073

thepudds opened this issue Jun 19, 2024 · 6 comments
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@thepudds
Copy link
Contributor

The Pairs example (playground link) in the iter package doc seems incorrect, with it looping forever while emitting the same values.

Another minor issue is it has an extra bool in the returned func signature. (Probably an older signature).

If we correct the extra bool, we can see it timeout on the playground:

// Pairs returns an iterator over successive pairs of values from seq.
func Pairs[V any](seq iter.Seq[V]) iter.Seq2[V, V] {
	return func(yield func(V, V) bool) {
		next, stop := iter.Pull(seq)
		defer stop()
		v1, ok1 := next()
		v2, ok2 := next()
		for ok1 || ok2 {
			if !yield(v1, v2) {
				return
			}
		}
	}
}

One possible correction might be this.

Unless someone beats me to it (which would be fine 😅), I will probably send a CL, but opening this issue in case any discussion is needed, including maybe I misunderstood the intent.

Finally, at first was unsure if it was meant to show overlapping vs. non-overlapping pairs. (I think the intent was probably non-overlapping pairs. I think itertools.pairwise in Python and std::ranges::views::pairwise_transform in C++ on the other hand both give overlapping pairs. That's probably fine, including this is just intended to be a simple example of using iter.Pull).

@gabyhelp
Copy link

Similar Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@seankhliao
Copy link
Member

cc @ianlancetaylor for CL 593555

@thepudds
Copy link
Contributor Author

thepudds commented Jun 19, 2024

Eh, sorry. I mostly wrote this up yesterday after checking if anyone had reported, but I was slow on posting and negligent on checking again 🙃 . Thanks @gabyhelp!

The looping forever is not reported in #68056, but a fix for that could be added to Ian's in-flight CL 593555 or a separate CL.

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Jun 19, 2024

Thanks for catching that, added a fix to CL 593555.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/593555 mentions this issue: iter: minor doc comment updates

@seankhliao seankhliao added Documentation NeedsFix The path to resolution is known, but the work has not been done. labels Jun 19, 2024
@seankhliao seankhliao added this to the Go1.23 milestone Jun 19, 2024
@AndrewHarrisSPU
Copy link

AndrewHarrisSPU commented Jun 19, 2024

Possibly irrelevant: this seems close to runnable example code that demonstrates something about the nature of the yield function. I'm not sure that's appropriate for this level of documentation. However, grokking the execution of yield is a bit of a light bulb that I believe documentation should try to flip on.

for {
	v1, ok1 := next()
	v2, ok2 := next()

	// short-circuit evaluation omits the yield of an (even-index, zero-value) pair, relative to !ok1 || !yield(v1, v2) || !ok2
	if !ok1 || !ok2 || !yield(v1, v2) { 
		return
	}
}

Mchnan pushed a commit to Mchnan/go-sylixos that referenced this issue Jul 9, 2024
Remove old return value. Use single variable range for iter.Seq[V].
Rewrite Pairs implementation to not loop forever.

Fixes golang#68056
Fixes golang#68073

Change-Id: I7ede0fe8ed058bbd57869d87e17b7f2c3641f7dd
Reviewed-on: https://go-review.googlesource.com/c/go/+/593555
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Reviewed-by: Damien Neil <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Commit-Queue: Ian Lance Taylor <[email protected]>
Reviewed-by: Mauri de Souza Meneguzzo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants