Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Without By #511

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ Supported intersection helpers:
- [Union](#union)
- [Without](#without)
- [WithoutEmpty](#withoutempty)
- [WithoutBy](#withoutby)

Supported search helpers:

Expand Down Expand Up @@ -2106,6 +2107,22 @@ subset := lo.WithoutEmpty([]int{0, 2, 10})
// []int{2, 10}
```

### WithoutBy

Returns slice excluding all values that satisfy the predicate.

```go
subset := lo.WithoutBy([]int{0, 2, 10}, func(x int) bool {
return x < 5
})
// []int{10}

subset := lo.WithoutBy([]string{"a", "b", "c"}, func(x string) bool {
return x == "a"
})
// []string{"b", "c"}
```

### IndexOf

Returns the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.
Expand Down
12 changes: 12 additions & 0 deletions intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice {
return result
}

// WithoutBy returns slice excluding all values that satisfy the predicate.
// The predicate function should return true if the item should be excluded.
func WithoutBy[T any, Slice ~[]T](collection Slice, predicate func(item T) bool) Slice {
result := make(Slice, 0, len(collection))
for i := range collection {
if !predicate(collection[i]) {
result = append(result, collection[i])
}
}
return result
}

// WithoutEmpty returns slice excluding empty values.
//
// Deprecated: Use lo.Compact instead.
Expand Down
39 changes: 39 additions & 0 deletions intersect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,45 @@ func TestWithout(t *testing.T) {
is.IsType(nonempty, allStrings, "type preserved")
}

func TestWithoutBy(t *testing.T) {
t.Parallel()
is := assert.New(t)
// Suite of tests to ensure that the WithoutBy function works as expected
// Tests will cover different types of slices and different types of elements
// to be removed from the slice

result1 := WithoutBy([]int{0, 1, 2, 3, 4, 5}, func(i int) bool { return i < 3 })
result2 := WithoutBy([]int{0, 1, 2, 3, 4, 5}, func(i int) bool { return i > 3 })
result3 := WithoutBy([]int{0, 1, 2, 3, 4, 5}, func(i int) bool { return i == 3 })

result4 := WithoutBy([]string{"foo", "bar", "baz"}, func(s string) bool { return s == "foo" })
result5 := WithoutBy([]string{"foo", "bar", "baz"}, func(s string) bool { return s == "bar" })
result6 := WithoutBy([]string{"foo", "bar", "baz"}, func(s string) bool { return s == "baz" })

result7 := WithoutBy([]bool{true, false, true}, func(b bool) bool { return b })
result8 := WithoutBy([]bool{true, false, true}, func(b bool) bool { return !b })

result9 := WithoutBy([]float64{1.1, 2.2, 3.3}, func(f float64) bool { return f == 1.1 })
result10 := WithoutBy([]float64{1.1, 2.2, 3.3}, func(f float64) bool { return f == 2.2 })
result11 := WithoutBy([]float64{1.1, 2.2, 3.3}, func(f float64) bool { return f == 3.3 })

is.Equal(result1, []int{3, 4, 5})
is.Equal(result2, []int{0, 1, 2, 3})
is.Equal(result3, []int{0, 1, 2, 4, 5})

is.Equal(result4, []string{"bar", "baz"})
is.Equal(result5, []string{"foo", "baz"})
is.Equal(result6, []string{"foo", "bar"})

is.Equal(result7, []bool{false})
is.Equal(result8, []bool{true, true})

is.Equal(result9, []float64{2.2, 3.3})
is.Equal(result10, []float64{1.1, 3.3})
is.Equal(result11, []float64{1.1, 2.2})

}

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