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: documentation improvements #70986

Open
3052 opened this issue Dec 24, 2024 · 5 comments
Open

iter: documentation improvements #70986

3052 opened this issue Dec 24, 2024 · 5 comments

Comments

@3052
Copy link

3052 commented Dec 24, 2024

Proposal Details

these

https://go.dev/pkg/iter/?m=old#Seq
https://go.dev/pkg/iter/?m=old#Seq2

both say this

"See the iter package documentation for more details" and link back to the same page:

https://go.dev/pkg/iter

also, the entire page does not give an example of implementing either type. technically yes, it does have the below example, but not an example of creating iter.Seq, and even the given example has a iter.Seq as input, so it would assume that you have already implemented iter.Seq without giving an example on how to do so

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()
		for {
			v1, ok1 := next()
			if !ok1 {
				return
			}
			v2, ok2 := next()
			// If ok2 is false, v2 should be the
			// zero value; yield one last pair.
			if !yield(v1, v2) {
				return
			}
			if !ok2 {
				return
			}
		}
	}
}
@3052 3052 added the Proposal label Dec 24, 2024
@gopherbot gopherbot added this to the Proposal milestone Dec 24, 2024
@seankhliao seankhliao changed the title proposal: iter: documentation improvements iter: documentation improvements Dec 24, 2024
@seankhliao seankhliao removed this from the Proposal milestone Dec 24, 2024
@3052
Copy link
Author

3052 commented Dec 24, 2024

here is an example from maps

func Keys[Map ~map[K]V, K comparable, V any](m Map) iter.Seq[K] {
	return func(yield func(K) bool) {
		for k := range m {
			if !yield(k) {
				return
			}
		}
	}
}

@Jorropo
Copy link
Member

Jorropo commented Dec 25, 2024

Reading the links you sent the example you show is meant to demonstrate iter.Pull and I find it for it's use of iter.Pull but it's the goal so ¯\_(ツ)_/¯
If you have ideas on how to improve the docs a patch would be welcome https://go.dev/doc/contribute but I'm not sure how it should look.

@3052
Copy link
Author

3052 commented Dec 25, 2024

I think Ive made it pretty clear already what the issues are, and what a fix could look like - so I don't think I need to add anything further

@thediveo
Copy link

The referenced docs have this paragraph

Iterator functions are most often called by a range loop, as in:

Admittedly upon encountering this for the first time I thought "but how do I write one, so I can use it?"

Would it be fine to do a PR that inserts a new paragraph and a short code before the above paragraph? Like a simple iterator over a slice just for simple illustration?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants