modash/moslice/filter.go
Leon Mika a20530ddfd Initial commit of modash
This was taken from github.com/lmika/gopkgs/fp
2025-01-27 13:19:52 +11:00

19 lines
486 B
Go

package moslice
// Filter returns a slice containing all the elements of ts for which the passed in
// predicate returns true. If no items match the predicate, the function will return
// an empty slice. If ts is nil, the function will also return nil.
func Filter[T any](ts []T, predicate func(t T) bool) []T {
if ts == nil {
return nil
}
filteredTs := make([]T, 0)
for _, t := range ts {
if predicate(t) {
filteredTs = append(filteredTs, t)
}
}
return filteredTs
}