-
-
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 SliceToSet #514
base: master
Are you sure you want to change the base?
feat: add SliceToSet #514
Conversation
slice_example_test.go
Outdated
@@ -479,3 +481,17 @@ func ExampleIsSortedByKey() { | |||
|
|||
// Output: true | |||
} | |||
|
|||
func ExampleSliceToSet() { | |||
list := []string{"a", "b", "d"} |
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.
Can you add more test , in which we have duplicate entries in the slice ?
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.
Sure, I fixed it! Please review it again.
@@ -378,6 +378,17 @@ func SliceToMap[T any, K comparable, V any](collection []T, transform func(item | |||
return Associate(collection, transform) | |||
} | |||
|
|||
// SliceToSet returns a map with each unique element of the slice as a key. | |||
func SliceToSet[T comparable, Slice ~[]T](collection Slice) map[T]struct{} { |
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.
Can you add this function in the Readme.
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.
Sure, I fixed it! Please review it again.
Thanks for your first contribution. I'm not sure this is the best name for such a helper. 1- it is not exactly a "set" data structure, but a map (see https://github.com/emirpasic/gods) Any takes? Other ideas:
Before merging this PR, I would like more feedback from the community 🙏 |
Hi samber, Thank you for your advice. I understand the concern regarding the To address this, package main
import "fmt"
// SliceToMapKey returns a map with each unique element of the slice as a key and a default value.
func SliceToMapKey[T comparable, Slice ~[]T, V any](collection Slice, defaultValue V) map[T]V {
result := make(map[T]V, len(collection))
for _, item := range collection {
result[item] = defaultValue
}
return result
}
func main() {
// Example usage:
// For checking duplicates with map[T]struct{}
result1 := SliceToMapKey([]string{"a", "b", "a"}, struct{}{})
fmt.Printf("%v\n", result1) // map[string]struct{}{"a": {}, "b": {}}
// For checking duplicates with map[T]bool
result2 := SliceToMapKey([]string{"a", "b", "a"}, true)
fmt.Printf("%v\n", result2) // map[string]bool{"a": true, "b": true}
} |
I find naming func Keyify[T comparable](collection []T) map[T]struct{} I'd stay with having Maybe it's even worth adding func KeyifyBy[T any, K comparable](collection []T, by func(T)K) map[K]T I could imagine calling it like |
|
see #505