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: add README
  • Loading branch information
nicklaus-dev committed Aug 17, 2024
commit b9680569a16ceb6ee65169392e0aafa83564b4cc
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ Supported intersection helpers:
- [Difference](#difference)
- [Union](#union)
- [Without](#without)
- [WithoutBy](#withoutby)
- [WithoutEmpty](#withoutempty)

Supported search helpers:
Expand Down Expand Up @@ -2097,6 +2098,38 @@ subset := lo.Without([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5)
// []int{10}
```

### WithoutBy

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.


```go
type user {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit suspicious about the fact the struct is unexported in the example. But also that the fields are unexported.

Based on the extract func, it might work, but I'm unsure, the README should not promote using unexported struct with unexported field.

The _test is in the same package, so it's not a proof it works

Copy link
Author

Choose a reason for hiding this comment

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

OK, it's a little bit misleading. I fixed it in README.md and test files. Please review it again.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for doing the changes. It's clearer now.

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...)
// []user[{id: 1, name: "Alice"}]
```

### WithoutEmpty

Returns slice excluding empty values.
Expand Down