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 AllMatch #68532

Open
idsulik opened this issue Jul 20, 2024 · 10 comments
Open

proposal: slices: add AllMatch #68532

idsulik opened this issue Jul 20, 2024 · 10 comments
Labels
Milestone

Comments

@idsulik
Copy link

idsulik commented Jul 20, 2024

This proposal suggests adding three new methods, AllMatch, to the slices package. The method will provide developers with powerful and concise ways to perform common operations on slices, enhancing code readability and reducing the likelihood of errors.

package slices

// AllMatch checks if all elements in the slice satisfy the predicate.
func AllMatch[T any](s []T, predicate func(T) bool) bool {
    for _, v := range s {
        if !predicate(v) {
            return false
        }
    }
    return true
}

I look forward to feedback from the maintainers and the community on this proposal and I'm ready to implement it if you don't mind.

@gopherbot gopherbot added this to the Proposal milestone Jul 20, 2024
@gophun
Copy link

gophun commented Jul 20, 2024

AnyMatch is just slices.ContainsFunc. And why a third time with SomeMatch?

@idsulik
Copy link
Author

idsulik commented Jul 20, 2024

AnyMatch is just slices.ContainsFunc. And why a third time with SomeMatch?

yes, you're right. we can omit it or add it as an alias for the ContainerFunc(to make it intuitive, because if you see SomeMatch and AllMatch, you'll want to see AnyMatch as well)

@seankhliao seankhliao changed the title proposal: Add AllMatch, AnyMatch, and SomeMatch Methods to slices Package proposal: slices: add AllMatch Jul 20, 2024
@meyermarcel
Copy link
Contributor

I don't see how a new or alias API improves readability. Adding new API defragments writing and worsens readability by having more inconsistent code. Surely writability is improved. But readability is more important in the long term.

@idsulik
Copy link
Author

idsulik commented Jul 20, 2024

@seankhliao , could you please explain why did you change the title?

@idsulik
Copy link
Author

idsulik commented Jul 20, 2024

Surely writability is improved. But readability is more important in the long term.

I agree with this, but I didn't get why the alias will reduce readability? As for me, it's the same as reading AnyMatch and ContainsFunc, the first one is even better, because it's intuitive and clear(the ContaintFunc sounds like "Does it contains func")

@idsulik idsulik changed the title proposal: slices: add AllMatch proposal: slices: add AllMatch and SomeMatch Jul 20, 2024
@seankhliao
Copy link
Member

We're not going to add aliases to existing functions, plus there is an expected format for titles.

@idsulik
Copy link
Author

idsulik commented Jul 20, 2024

We're not going to add aliases to existing functions, plus there is an expected format for titles.

thank you!
p.s. the SomeMatch is not an alias as I know, only AnyMatch

@seankhliao
Copy link
Member

Both are aliases of ContainsFunc.

@seankhliao seankhliao changed the title proposal: slices: add AllMatch and SomeMatch proposal: slices: add AllMatch Jul 20, 2024
@meyermarcel
Copy link
Contributor

Surely writability is improved. But readability is more important in the long term.

I agree with this, but I didn't get why the alias will reduce readability? As for me, it's the same as reading AnyMatch and ContainsFunc, the first one is even better, because it's intuitive and clear(the ContaintFunc sounds like "Does it contains func")

More possibilities for writers means more mental load for readers. An alias is not much but it adds up over the years. Look at the evolution of other languages and how many writing possibilities they have. Ultimately, writers have many opportunities on the shoulders of readers. As a reader it costs to have in mind "Ah, x is the same as y.".

@ianlancetaylor
Copy link
Contributor

Rather than having specific functions in slices, these seem more appropriate for the proposal x/exp/xiters package. See, for example, #67453.

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

6 participants