2022-09-17 00:04:53 +02:00
|
|
|
package slices
|
|
|
|
|
|
|
|
func Filter[T any](slice []T, f func(T) bool) []T {
|
|
|
|
ret := make([]T, 0, len(slice))
|
|
|
|
for _, v := range slice {
|
|
|
|
if f(v) {
|
|
|
|
ret = append(ret, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
2024-02-20 16:03:29 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|