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
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions intersect.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ func Without[T comparable, Slice ~[]T](collection Slice, exclude ...T) Slice {
return result
}

// WithoutBy returns a slice excluding values whose extracted key is in the exclude list.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is can be a much better comment I think:

// 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.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I modifed this comments. Please review it again.

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.

result := make([]T, 0, len(collection))
for _, item := range collection {
if !Contains(exclude, extract(item)) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Without

Copy link
Contributor

@senago senago Aug 22, 2024

Choose a reason for hiding this comment

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

And maybe It would be good to simply call WithoutBy in Without, most likely compiler will optimize such a call (could check in godbolt)

func Without[T comparable](collection []T, exclude ...T) []T {
	return WithoutBy(collection, func(item T) T { return item }, exclude...)
}

Copy link
Author

Choose a reason for hiding this comment

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

I have refactored WithoutBy function. I used blacklist to replace whitelist for code readability. It's clear now. 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.

Yeah, with blacklist it's simpler
I suggested whitelist cause this way we can preallocate the exact capacity for the resulting slice

result = append(result, item)
}
}
return result
}

// WithoutEmpty returns slice excluding empty values.
//
// Deprecated: Use lo.Compact instead.
Expand Down
20 changes: 20 additions & 0 deletions intersect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,26 @@ func TestWithout(t *testing.T) {
is.IsType(nonempty, allStrings, "type preserved")
}

func TestWithoutBy(t *testing.T) {
t.Parallel()
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 add this in the test , This gives a more real life example:

func main() {
    // Example usage
    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
    for _, user := range filteredUsers {
        fmt.Println(user.Name)
    }
}

Copy link
Author

Choose a reason for hiding this comment

The 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)
Expand Down