added FindFirstIndex and FindLastIndex

This commit is contained in:
milarin 2024-02-20 16:06:41 +01:00
parent 26ea846e93
commit 5735640ab6

View File

@ -19,12 +19,20 @@ func FindFirst[T any](slice []T, f func(T) bool) (T, bool) {
return *new(T), false
}
func FindLast[T any](slice []T, f func(T) bool) (T, bool) {
for i := len(slice); i >= 0; i-- {
v := slice[i]
func FindFirstIndex[T any](slice []T, f func(T) bool) (int, bool) {
for i, v := range slice {
if f(v) {
return v, true
return i, true
}
}
return *new(T), false
return -1, false
}
func FindLastIndex[T any](slice []T, f func(T) bool) (int, bool) {
for i := len(slice); i >= 0; i-- {
if f(slice[i]) {
return i, true
}
}
return -1, false
}