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 } // FilterMap returns a mapped 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 FilterMap[T, U any](ts []T, predicate func(t T) (U, bool)) []U { if ts == nil { return nil } filteredUs := make([]U, 0) for _, t := range ts { u, ok := predicate(t) if !ok { continue } filteredUs = append(filteredUs, u) } return filteredUs }