Skip to content

iter: documentation improvements #70986

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

Closed
3052 opened this issue Dec 24, 2024 · 11 comments
Closed

iter: documentation improvements #70986

3052 opened this issue Dec 24, 2024 · 11 comments
Labels
Documentation Issues describing a change to documentation. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Milestone

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
@gopherbot gopherbot added the Documentation Issues describing a change to documentation. label 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.

@thediveo
Copy link
Contributor

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?

@cherrymui
Copy link
Member

Feel free to send a CL to improve the documentation, and/or add examples. Thanks!

Also, it probably makes sense to have a link to the language spec's range over function part.

@cherrymui cherrymui added this to the Backlog milestone Dec 26, 2024
@cherrymui cherrymui added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 26, 2024
@ianlancetaylor
Copy link
Contributor

The blog post https://go.dev/blog/range-functions also gives some examples of writing iterators.

I'm not sure how much should be added to the package documentation. It's hard to know when to stop. The right approach might be to add a few Example functions.

@thediveo
Copy link
Contributor

My current change is to have the maps.Keys iterator example as this is small enough, yet up to the point. The link to the for loop language spec also snugly fits in with the existing paragraph about "...often called by a range loop, as in...". Linking to more examples in the blog will IMHO also neatly fit in without overcrowding.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/638895 mentions this issue: iter: improve documentation with iterator example

@thediveo
Copy link
Contributor

Note that the self reference actually points to the package documentation that the CL improves. Feel free to submit your CL.

@ianlancetaylor
Copy link
Contributor

When I click on the link, it brings me to the top of the page. This doesn't seem unreasonable.

@ianlancetaylor
Copy link
Contributor

@3052 Understood, but it still seems OK to me as it currently is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Issues describing a change to documentation. help wanted NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

9 participants