Initial commit of modash

This was taken from github.com/lmika/gopkgs/fp
This commit is contained in:
Leon Mika 2025-01-27 13:19:52 +11:00
commit a20530ddfd
20 changed files with 425 additions and 0 deletions

18
moslice/filter.go Normal file
View file

@ -0,0 +1,18 @@
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
}