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

proposal: slices: add LastIndex and LastIndexFunc #63128

Open
dsnet opened this issue Sep 20, 2023 · 4 comments
Open

proposal: slices: add LastIndex and LastIndexFunc #63128

dsnet opened this issue Sep 20, 2023 · 4 comments
Labels
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Sep 20, 2023

I propose the addition of:

// LastIndex returns the index of the last occurrence of v in s, or -1 if not present.
func LastIndex[S ~[]E, E comparable](s S, v E) int

// LastIndexFunc returns the last index i satisfying f(s[i]), or -1 if none do.
func LastIndexFunc[S ~[]E, E any](s S, f func(E) bool) int

which parallels the existence of:

  • strings.IndexXXX
  • strings.LastIndexXXX
  • bytes.IndexXXX
  • bytes.LastIndexXXX

In my use-case, I'm processing Unicode runes as a []rune and wanted to find the last occurrence of a given rune.

@dsnet dsnet added the Proposal label Sep 20, 2023
@gopherbot gopherbot added this to the Proposal milestone Sep 20, 2023
@dsnet dsnet changed the title proposal: slices: add IndexLast and IndexLastFunc proposal: slices: add LastIndex and LastIndexFunc Sep 20, 2023
@earthboundkid
Copy link
Contributor

This makes a lot of sense, but devil's advocate, should we just tell people to do iterutils.Index(slices.Backwards(s), v) in a world where iterutils exists? I think the tricky of it would be that even with slices.Backwards, you run into issues with matching just the value vs. the index. Probably it's worth it anyway.

@jimmyfrasche
Copy link
Member

index doesn't make sense with iterators like it does with slices.

@ianlancetaylor
Copy link
Contributor

I can see a reasonable argument that the slices package should have a version of all relevant functions in the bytes and strings packages.

@ghost
Copy link

ghost commented Sep 21, 2023

This makes a lot of sense, but devil's advocate, should we just tell people to do iterutils.Index(slices.Backwards(s), v) in a world where iterutils exists? I think the tricky of it would be that even with slices.Backwards, you run into issues with matching just the value vs. the index. Probably it's worth it anyway.

slices.Backwards doesn't exist:

https://cs.opensource.google/search?q=backwards&ss=go/go:src/slices/

and according to the quoted comment, neither does iterutils. so I am not sure how much value would be in a suggestion thats not even actionable. also the implementation is dead simple:

https://github.com/golang/go/blob/go1.21.1/src/bytes/bytes.go#L146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

5 participants