-
-
Notifications
You must be signed in to change notification settings - Fork 837
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
base: master
Are you sure you want to change the base?
feat: add WithoutBy #515
Changes from 6 commits
107d579
3cf2fc4
21f10da
b968056
fd53232
4f1f56a
6b9a75d
6e5a845
c102c03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,18 @@ func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice { | |
return result | ||
} | ||
|
||
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can also the function def in README. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I fixed it. Please review again. |
||
result := make([]T, 0, len(collection)) | ||
for _, item := range collection { | ||
if !Contains(exclude, extract(item)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd say it's worth using sets here for better performance func WithoutBy[T any, K comparable](collection []T, extract func(item T) K, exclude ...K) []T {
whitelist := make(map[K]struct{}, len(collection))
for _, item := range collection {
whitelist[extract(item)] = struct{}{}
}
for _, k := range exclude {
delete(whitelist, k)
}
result := make([]T, 0, len(whitelist))
for _, item := range collection {
if _, ok := whitelist[extract(item)]; ok {
result = append(result, item)
}
}
return result
} Might as well refactor existing function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And maybe It would be good to simply call func Without[T comparable](collection []T, exclude ...T) []T {
return WithoutBy(collection, func(item T) T { return item }, exclude...)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have refactored WithoutBy function. I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, with |
||
result = append(result, item) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// WithoutEmpty returns slice excluding empty values. | ||
// | ||
// Deprecated: Use lo.Compact instead. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package lo | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func ExampleWithoutBy() { | ||
type User struct { | ||
ID int | ||
Name string | ||
} | ||
// original users | ||
users := []User{ | ||
{ID: 1, Name: "Alice"}, | ||
{ID: 2, Name: "Bob"}, | ||
{ID: 3, Name: "Charlie"}, | ||
} | ||
|
||
// exclude users with IDs 2 and 3 | ||
excludedIDs := []int{2, 3} | ||
|
||
// extract function to get the user ID | ||
extractID := func(user User) int { | ||
return user.ID | ||
} | ||
|
||
// filtering users | ||
filteredUsers := WithoutBy(users, extractID, excludedIDs...) | ||
|
||
// output the filtered users | ||
fmt.Printf("%v\n", filteredUsers) | ||
// Output: | ||
// [{1 Alice}] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -270,6 +270,26 @@ func TestWithout(t *testing.T) { | |
is.IsType(nonempty, allStrings, "type preserved") | ||
} | ||
|
||
func TestWithoutBy(t *testing.T) { | ||
t.Parallel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can also add this in the test , This gives a more real life example:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a intersect_example_test.go file to add this example. Please review it again. |
||
is := assert.New(t) | ||
|
||
type User struct { | ||
Name string | ||
Age int | ||
} | ||
|
||
result1 := WithoutBy([]User{{Name: "nick"}, {Name: "peter"}}, | ||
func(item User) string { | ||
return item.Name | ||
}, "nick", "lily") | ||
result2 := WithoutBy([]User{}, func(item User) int { return item.Age }, 1, 2, 3) | ||
result3 := WithoutBy([]User{}, func(item User) string { return item.Name }) | ||
is.Equal(result1, []User{{Name: "peter"}}) | ||
is.Equal(result2, []User{}) | ||
is.Equal(result3, []User{}) | ||
} | ||
|
||
func TestWithoutEmpty(t *testing.T) { | ||
t.Parallel() | ||
is := assert.New(t) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: missing
struct
hereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I fixed it, please review it again.