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

Proposal: Add SliceToSet, WithoutBy #505

Open
senago opened this issue Jul 25, 2024 · 1 comment
Open

Proposal: Add SliceToSet, WithoutBy #505

senago opened this issue Jul 25, 2024 · 1 comment

Comments

@senago
Copy link
Contributor

senago commented Jul 25, 2024

It's pretty often that one needs to exclude some entries from slice by some keys

There's lo.Without which could be useful
But it's not always convenient cause it could be so that you have an entity and only its' keys
In that case it could be useful to have

SliceToSet[K comparable](values []K) map[K]struct{}

For example:

type Entity struct {
	Key string
	Value string
}

func filter(entities []Entity) []Entity {
	blackList := SliceToSet(getBlackList()) // map[string]struct{}

	return lo.Filter(entities, func(e Entity, _ int) bool {
		_, ok := blackList[e.Key]
		return !ok
	})
}

Yeah, one could simply return the map from getBlackList() or use lo.Contains
But to return that map SliceToSet could still be useful and lo.Contains is not always optimal
And I guess there's lots of other applications of such a function

Also seems like it could be useful adding

WithoutBy[T any, K comparable](collection []T, extract func(T) K, exclude ...K) []T
@samber
Copy link
Owner

samber commented Jul 26, 2024

Both proposals seem good to me.

I first hesitated to name SliceToSet() just Set(), but your suggestion might be more accurate.

Please open 2 PRs for these helpers.

@usman1100 usman1100 mentioned this issue Aug 11, 2024
This was referenced Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants