- added FindFirst and FindLast

- removed Search
This commit is contained in:
milarin 2024-02-20 16:03:29 +01:00
parent 0592add8ca
commit 26ea846e93
2 changed files with 19 additions and 11 deletions

View File

@ -9,3 +9,22 @@ func Filter[T any](slice []T, f func(T) bool) []T {
}
return ret
}
func FindFirst[T any](slice []T, f func(T) bool) (T, bool) {
for _, v := range slice {
if f(v) {
return v, true
}
}
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]
if f(v) {
return v, true
}
}
return *new(T), false
}

View File

@ -1,11 +0,0 @@
package slices
func Search[T any](slice []T, f func(a, b T) T) T {
if len(slice) == 0 {
return *new(T)
}
value := slice[0]
Each(slice, func(v T) { value = f(value, v) })
return value
}