Added FilterMap

This commit is contained in:
Leon Mika 2026-09-05 09:23:54 +10:00
parent f7abe7c01f
commit e33476ea5c
2 changed files with 42 additions and 1 deletions

View file

@ -16,3 +16,22 @@ func Filter[T any](ts []T, predicate func(t T) bool) []T {
} }
return filteredTs 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
}

View file

@ -1,10 +1,12 @@
package moslice_test package moslice_test
import ( import (
"lmika.dev/pkg/modash/moslice" "fmt"
"strings" "strings"
"testing" "testing"
"lmika.dev/pkg/modash/moslice"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -27,3 +29,23 @@ func TestFilter(t *testing.T) {
assert.Equal(t, []int{}, moslice.Filter([]int{}, func(x int) bool { return x%2 == 0 })) assert.Equal(t, []int{}, moslice.Filter([]int{}, func(x int) bool { return x%2 == 0 }))
}) })
} }
func TestFilterMap(t *testing.T) {
var (
ints = []int{1, 2, 3, 4, 5}
strs = []string{"foo", "bar", "baz"}
)
t.Run("should filter items matching the predicate", func(t *testing.T) {
assert.Equal(t, []string{"2", "4"}, moslice.FilterMap(ints, func(x int) (string, bool) { return fmt.Sprint(x), x%2 == 0 }))
assert.Equal(t, []int{3, 3}, moslice.FilterMap(strs, func(x string) (int, bool) { return len(x), strings.Contains(x, "b") }))
})
t.Run("should moslice nil if the passed in slice is nil", func(t *testing.T) {
assert.Nil(t, moslice.FilterMap(nil, func(x int) (string, bool) { return fmt.Sprint(x), x%2 == 0 }))
})
t.Run("should return empty slice if the passed in slice is empty slice", func(t *testing.T) {
assert.Equal(t, []string{}, moslice.FilterMap([]int{}, func(x int) (string, bool) { return fmt.Sprint(x), x%2 == 0 }))
})
}