Skip to content

Commit

Permalink
feat: adding FilterReject (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
samber authored Jun 27, 2024
1 parent 20468b7 commit 91b5d94
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Supported helpers for slices:
- [DropWhile](#dropwhile)
- [DropRightWhile](#droprightwhile)
- [Reject](#reject)
- [FilterReject](#filterreject)
- [Count](#count)
- [CountBy](#countby)
- [CountValues](#countvalues)
Expand Down Expand Up @@ -749,6 +750,18 @@ odd := lo.Reject([]int{1, 2, 3, 4}, func(x int, _ int) bool {

[[play](https://go.dev/play/p/YkLMODy1WEL)]

### FilterReject

FilterReject mixes Filter and Reject, this method returns two slices, one for the elements of collection that predicate returns truthy for and one for the elements that predicate does not return truthy for.

```go
kept, rejected := lo.FilterReject([]int{1, 2, 3, 4}, func(x int, _ int) bool {
return x%2 == 0
})
// []int{2, 4}
// []int{1, 3}
```

### Count

Counts the number of elements in the collection that compare equal to value.
Expand Down
17 changes: 17 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,23 @@ func Reject[V any](collection []V, predicate func(item V, index int) bool) []V {
return result
}

// FilterReject mixes Filter and Reject, this method returns two slices, one for the elements of collection that
// predicate returns truthy for and one for the elements that predicate does not return truthy for.
func FilterReject[V any](collection []V, predicate func(V, int) bool) (kept []V, rejected []V) {
kept = make([]V, 0, len(collection))
rejected = make([]V, 0, len(collection))

for i, item := range collection {
if predicate(item, i) {
kept = append(kept, item)
} else {
rejected = append(rejected, item)
}
}

return kept, rejected
}

// Count counts the number of elements in the collection that compare equal to value.
// Play: https://go.dev/play/p/Y3FlK54yveC
func Count[T comparable](collection []T, value T) (count int) {
Expand Down
23 changes: 21 additions & 2 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func TestAssociate(t *testing.T) {

func TestSliceToMap(t *testing.T) {
t.Parallel()

type foo struct {
baz string
bar int
Expand Down Expand Up @@ -500,6 +500,25 @@ func TestReject(t *testing.T) {
is.Equal(r2, []string{"foo", "bar"})
}

func TestFilterReject(t *testing.T) {
t.Parallel()
is := assert.New(t)

left1, right1 := FilterReject([]int{1, 2, 3, 4}, func(x int, _ int) bool {
return x%2 == 0
})

is.Equal(left1, []int{2, 4})
is.Equal(right1, []int{1, 3})

left2, right2 := FilterReject([]string{"Smith", "foo", "Domin", "bar", "Olivia"}, func(x string, _ int) bool {
return len(x) > 3
})

is.Equal(left2, []string{"Smith", "Domin", "Olivia"})
is.Equal(right2, []string{"foo", "bar"})
}

func TestCount(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand Down Expand Up @@ -626,7 +645,7 @@ func TestSlice(t *testing.T) {
out16 := Slice(in, -10, 1)
out17 := Slice(in, -1, 3)
out18 := Slice(in, -10, 7)

is.Equal([]int{}, out1)
is.Equal([]int{0}, out2)
is.Equal([]int{0, 1, 2, 3, 4}, out3)
Expand Down

0 comments on commit 91b5d94

Please sign in to comment.