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: x/exp/xiter: add Find/FindMap/Contains/ContainsFunc #67458

Open
leaxoy opened this issue May 17, 2024 · 1 comment
Open

proposal: x/exp/xiter: add Find/FindMap/Contains/ContainsFunc #67458

leaxoy opened this issue May 17, 2024 · 1 comment
Labels
Milestone

Comments

@leaxoy
Copy link

leaxoy commented May 17, 2024

Proposal Details

Proposed add below functions:

func Find[E any](s Seq[E], f func(E) bool) (E, bool)
func FindMap[E, U any](s Seq[E], f func(E) (U, bool)) (U, bool)

func Contains[E comprable](s Seq[E], v E) bool {
    return ContainsFunc(s, func (x E) bool { return x==v})
}
func ContainsFunc[E any](s Seq[E], f func(E) bool) bool {
    _, ok := Find(s, f)
    return ok
}
@Merovius
Copy link
Contributor

I'm not sure either of these pulls their weight. From containers, I would tend to expect a more efficient way to figure out whether they contain an element. And I'm not sure how often it is useful whether a sequence contains an element, while also throwing out the sequence (note that not all iterators are re-startable and even for those that are, I'd expect a linear search to be expensive enough that it's useful not to just throw away the state if you found what you where looking for). They might be worthwhile, but ISTM that should come with some evidence (in particular, we probably want to wait a bit to gather experience with iterators, before building up a humongous library of iteration-manipulators - in my experience Go's iterators are just generally less useful than in other languages).

Independent from that, I don't think we should have both Find and FindMap. Either form can be pretty easily expressed using the other (potentially using Map/Map2).

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

3 participants