-
Notifications
You must be signed in to change notification settings - Fork 28
feat(gslice): add ToBoolMap to collect slice elements into a set (map[T]bool) #20
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
Changes from 2 commits
82e9830
9d566aa
d44d7b9
0df2e15
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 |
|---|---|---|
|
|
@@ -54,7 +54,7 @@ | |
| // | ||
| // Convert to Map: | ||
| // | ||
| // - [ToMap], [ToMapValues] | ||
| // - [ToMap], [ToMapValues], [ToSet] | ||
| // - [GroupBy] | ||
| // | ||
| // Set operations: | ||
|
|
@@ -1116,6 +1116,24 @@ func ToMapValues[T any, K comparable](s []T, f func(T) K) map[K]T { | |
| return iter.ToMapValues(f, iter.StealSlice(s)) | ||
| } | ||
|
|
||
| // ToSet collects elements of a slice into a set-like structure represented by map[T]bool. | ||
| // Each unique element from the input slice becomes a key in the map with the value `true`. | ||
| // Duplicate elements are automatically de-duplicated due to map key uniqueness. | ||
| // | ||
| // 🚀 EXAMPLE: | ||
| // | ||
| // s := []int{1, 2, 2, 3} | ||
| // ToSet(s) ⏩ map[int]bool{1: true, 2: true, 3: true} | ||
| // | ||
| // 🧠 NOTE: | ||
| // | ||
| // The element type T must be comparable, as required by Go map keys. | ||
| // | ||
| // 💡 AKA: setOf(...) in Kotlin, .toSet() in Python | ||
| func ToSet[T comparable](s []T) map[T]bool { | ||
|
Collaborator
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. We already have a collection/set data type, the name
Contributor
Author
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. Makes sense — my initial thought was to mirror naming from languages like Kotlin/Python ( |
||
| return ToMap(s, func(t T) (T, bool) { return t, true }) | ||
| } | ||
|
|
||
| // PtrOf returns pointers that point to equivalent elements of slice s. | ||
| // ([]T → []*T). | ||
| // | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.