-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add slices.Any, slices.None #2
Comments
Thank you for this suggestion. As presented here, the functions seem too special-purpose to be in keeping with the other features of this module. A more general approach would allow you to, say, find the index of the first value that passes the test. It could return -1 if no such value is found. This allows the trivial implementation of func Any[T any](s []T, test func(T) bool) bool {
return indexOf(s, test) >= 0
}
func None[T any](s []T, test func(T) bool) bool {
return !Any(s, test)
}
func All[T any](s []T, test func(T) bool) bool {
notTest := func(val T) bool { return !test(val) }
return !Any(s, notTest)
} As it happens, I am presently updating the go-generics module to align with the planned inclusion, in Go 1.21, of new Sound OK? |
(In fact, slices.ContainsFunc is exactly your |
Sounds good. Thanks for sharing the broader context. |
Any and None are useful constructs to determine if any (or none) of the elements match a predicate.
Sample implementations:
The text was updated successfully, but these errors were encountered: