Skip to content

Commit

Permalink
feat: adding RejectMap (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
samber authored Jun 27, 2024
1 parent 91b5d94 commit 80bb7ce
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
18 changes: 17 additions & 1 deletion 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)
- [RejectMap](#rejectmap)
- [FilterReject](#filterreject)
- [Count](#count)
- [CountBy](#countby)
Expand Down Expand Up @@ -750,9 +751,24 @@ odd := lo.Reject([]int{1, 2, 3, 4}, func(x int, _ int) bool {

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

### RejectMap

The opposite of FilterMap, this method returns a slice which obtained after both filtering and mapping using the given callback function.

The callback function should return two values:
- the result of the mapping operation and
- whether the result element should be included or not.

```go
items := lo.RejectMap([]int{1, 2, 3, 4}, func(x int, _ int) (int, bool) {
return x*10, x%2 == 0
})
// []int{10, 30}
```

### 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.
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 {
Expand Down
16 changes: 16 additions & 0 deletions slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,22 @@ func Reject[V any](collection []V, predicate func(item V, index int) bool) []V {
return result
}

// RejectMap is the opposite of FilterMap, this method returns a slice which obtained after both filtering and mapping using the given callback function.
// The callback function should return two values:
// - the result of the mapping operation and
// - whether the result element should be included or not.
func RejectMap[T any, R any](collection []T, callback func(item T, index int) (R, bool)) []R {
result := []R{}

for i, item := range collection {
if r, ok := callback(item, i); !ok {
result = append(result, r)
}
}

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) {
Expand Down
23 changes: 23 additions & 0 deletions slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,29 @@ func TestReject(t *testing.T) {
is.Equal(r2, []string{"foo", "bar"})
}

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

r1 := RejectMap([]int64{1, 2, 3, 4}, func(x int64, _ int) (string, bool) {
if x%2 == 0 {
return strconv.FormatInt(x, 10), false
}
return "", true
})
r2 := RejectMap([]string{"cpu", "gpu", "mouse", "keyboard"}, func(x string, _ int) (string, bool) {
if strings.HasSuffix(x, "pu") {
return "xpu", false
}
return "", true
})

is.Equal(len(r1), 2)
is.Equal(len(r2), 2)
is.Equal(r1, []string{"2", "4"})
is.Equal(r2, []string{"xpu", "xpu"})
}

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

0 comments on commit 80bb7ce

Please sign in to comment.