Skip to content

Commit

Permalink
feat(slices): add Remove function as alias for Without (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Jan 1, 2025
1 parent 03789d9 commit 25d4f3b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions slices/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ func Without[S ~[]E, E comparable](s S, items ...E) S {
return result
}

// Remove returns a new slice without the given items.
// Alias for Without.
//
// Remove([]int{1, 2, 3}, 2, 3) // []int{1}
// Remove([]string{"a", "b", "c"}, "b", "c") // []string{"a"}
func Remove[S ~[]E, E comparable](s S, items ...E) S {
return Without(s, items...)
}

// Partition returns two new slices, the first containing
// the items of a given slice that satisfy the given predicate function,
// and the second containing the items that do not satisfy the predicate function.
Expand Down
5 changes: 4 additions & 1 deletion slices/slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,15 +283,18 @@ func TestOnly(t *testing.T) {
assert.Equal(t, []T{{"2"}, {"4"}}, Only(s3, T{"2"}, T{"4"}))
}

func TestWithout(t *testing.T) {
func TestWithoutAndRemove(t *testing.T) {
s1 := []int{1, 2, 3, 4}
assert.Equal(t, []int{1, 3}, Without(s1, 2, 4))
assert.Equal(t, []int{1, 3}, Remove(s1, 2, 4))

s2 := []string{"1", "2", "3", "4"}
assert.Equal(t, []string{"1", "3"}, Without(s2, "2", "4"))
assert.Equal(t, []string{"1", "3"}, Remove(s2, "2", "4"))

s3 := []T{{"1"}, {"2"}, {"3"}, {"4"}}
assert.Equal(t, []T{{"1"}, {"3"}, {"4"}}, Without(s3, T{"2"}))
assert.Equal(t, []T{{"1"}, {"3"}, {"4"}}, Remove(s3, T{"2"}))
}

func TestPartition(t *testing.T) {
Expand Down

0 comments on commit 25d4f3b

Please sign in to comment.