Add function Without which return slice with slice all elements not i… (#142)

* Add function Without which return slice with slice all elements not in another

* feat(Without): inverse args and use variadic argment for excluding items

Co-authored-by: Samuel Berthe <dev@samuel-berthe.fr>
This commit is contained in:
chensy
2022-07-04 16:17:03 +08:00
committed by GitHub
parent 5bdd1c13c9
commit 07fddc1640
3 changed files with 38 additions and 0 deletions

View File

@@ -175,3 +175,14 @@ func Union[T comparable](list1 []T, list2 []T) []T {
return result
}
// Without returns slice excluding all given values.
func Without[T comparable](collection []T, exclude ...T) []T {
result := make([]T, 0, len(collection))
for _, e := range collection {
if !Contains(exclude, e) {
result = append(result, e)
}
}
return result
}