Skip to content

Commit d9f7608

Browse files
committed
added xslices.Filter
1 parent c1bd073 commit d9f7608

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

internal/xslices/map_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package xslices
22

33
import (
4-
"github.com/stretchr/testify/require"
54
"strconv"
65
"testing"
6+
7+
"github.com/stretchr/testify/require"
78
)
89

910
func TestMap(t *testing.T) {

internal/xslices/split.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package xslices
2+
3+
func Filter[T any](x []T, filter func(t T) bool) (good, bad []T) {
4+
good = make([]T, 0, len(x))
5+
bad = make([]T, 0, len(x))
6+
7+
for i := 0; i < len(x); i++ {
8+
if filter(x[i]) {
9+
good = append(good, x[i])
10+
} else {
11+
bad = append(bad, x[i])
12+
}
13+
}
14+
15+
return good, bad
16+
}

internal/xslices/split_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package xslices
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestFilter(t *testing.T) {
10+
good, bad := Filter([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, func(t int) bool {
11+
return t%2 == 0
12+
})
13+
require.Equal(t, []int{0, 2, 4, 6, 8}, good)
14+
require.Equal(t, []int{1, 3, 5, 7, 9}, bad)
15+
}

0 commit comments

Comments
 (0)