Commit fa8cd48
Changed files (2)
pkg
pkg/x/iterate.go
@@ -25,3 +25,11 @@ func Contains[T comparable](items []T, predicate Predicate[T]) bool {
item := Find[T](items, predicate)
return item != Default[T]()
}
+
+func Map[TInput any, TOutput any](items []TInput, mapFrom func(TInput) TOutput) []TOutput {
+ results := []TOutput{}
+ for _, item := range items {
+ results = append(results, mapFrom(item))
+ }
+ return results
+}
pkg/x/iterate_test.go
@@ -1,6 +1,7 @@
package x
import (
+ "strconv"
"testing"
"github.com/stretchr/testify/assert"
@@ -71,3 +72,13 @@ func TestContains(t *testing.T) {
assert.False(t, result)
})
}
+
+func TestMap(t *testing.T) {
+ t.Run("maps each item", func(t *testing.T) {
+ items := []int{1, 2, 3}
+ result := Map(items, func(item int) string {
+ return strconv.Itoa(item)
+ })
+ assert.Equal(t, []string{"1", "2", "3"}, result)
+ })
+}