Skip to content

Commit d90fdb7

Browse files
authored
Merge pull request #1353 from ydb-platform/xslices
Added xslices.Split + refactored xslices.Filter
2 parents 49d103f + 84da30d commit d90fdb7

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

internal/xslices/filter.go

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

internal/xslices/filter_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package xslices
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestFilter(t *testing.T) {
10+
filtered := 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}, filtered)
14+
}

internal/xslices/split.go

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

3-
func Filter[T any](x []T, filter func(t T) bool) (good, bad []T) {
3+
func Split[T any](x []T, isOk func(t T) bool) (good, bad []T) {
44
good = make([]T, 0, len(x))
55
bad = make([]T, 0, len(x))
66

77
for i := 0; i < len(x); i++ {
8-
if filter(x[i]) {
8+
if isOk(x[i]) {
99
good = append(good, x[i])
1010
} else {
1111
bad = append(bad, x[i])

internal/xslices/split_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"github.com/stretchr/testify/require"
77
)
88

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 {
9+
func TestSplit(t *testing.T) {
10+
good, bad := Split([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, func(t int) bool {
1111
return t%2 == 0
1212
})
1313
require.Equal(t, []int{0, 2, 4, 6, 8}, good)

0 commit comments

Comments
 (0)