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

feat: add WithoutBy #515

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
feat: impove WithoutBy performance
  • Loading branch information
nicklaus-dev committed Aug 23, 2024
commit 6e5a845f9f0b8ba6abbaffa34998bbb6ce1de414
7 changes: 6 additions & 1 deletion intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,14 @@ func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice {
// WithoutBy filters a slice by excluding elements whose extracted keys match any in the exclude list.
// It returns a new slice containing only the elements whose keys are not in the exclude list.
func WithoutBy[T any, K comparable](collection []T, extract func(item T) K, exclude ...K) []T {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also the function def in README.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I fixed it. Please review again.

blacklist := make(map[K]struct{}, len(exclude))
for _, e := range exclude {
blacklist[e] = struct{}{}
}

result := make([]T, 0, len(collection))
for _, item := range collection {
if !Contains(exclude, extract(item)) {
if _, ok := blacklist[extract(item)]; !ok {
result = append(result, item)
}
}
Expand Down