Added some more find functions
All checks were successful
ci / deploy (push) Successful in 1m5s

This commit is contained in:
Leon Mika 2026-09-05 09:40:33 +10:00
parent e33476ea5c
commit 5586ab7464
2 changed files with 150 additions and 1 deletions

View file

@ -20,6 +20,16 @@ func FirstWhere[T any](ts []T, predicate func(t T) bool) T {
return zeroT
}
// FirstIndexWhere returns the index of the first element in the slice that satisfies the predicate. An empty slice will return -1
func FirstIndexWhere[T any](ts []T, predicate func(t T) bool) int {
for i, t := range ts {
if predicate(t) {
return i
}
}
return -1
}
func FindWhere[T any](ts []T, predicate func(t T) bool) (T, bool) {
var zeroT T
@ -41,3 +51,26 @@ func FindWithIndexWhere[T any](ts []T, predicate func(t T) bool) (T, int, bool)
}
return zeroT, 0, false
}
// AnyWhere returns true if any element in the slice satisfies the predicate. An empty slice will return false
func AnyWhere[T any](ts []T, predicate func(t T) bool) bool {
for _, t := range ts {
if predicate(t) {
return true
}
}
return false
}
// AllWhere returns true if all the element in the slice satisfies the predicate. An empty slice will return false
func AllWhere[T any](ts []T, predicate func(t T) bool) bool {
if len(ts) == 0 {
return false
}
for _, t := range ts {
if !predicate(t) {
return false
}
}
return true
}