-
Notifications
You must be signed in to change notification settings - Fork 2
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
pkg/maps: add generics sets implementations #70
Conversation
maps/sets.go
Outdated
// IndexedSet is a Set where the key of the map is extracted from | ||
// Fn. | ||
// Adding values with the same key will replace the previous value | ||
type IndexedSet[K comparable, T any] 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.
what kind of usecase would you use this in?
All the usecases i can think about are taken into account by https://github.com/upfluence/pkg/blob/master/slices/index_by.go#L4
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.
Where you will have used index_by several times in a row while scrolling.
Let's take entities A and B, where A has B as a foreign key. You want to list everything a subset of A based an a value of B and return the related B.
Without the possibility of a join, you will have to fetch both A and B. Filter A by B. Repeat until you have everything you need.
- Fetch A
- Fetch related B -> read in Set first
- Filter A with B filter
- Index B in Set to avoid refetch.
- Return A and B (Set.Values)
It would be lot simpler if we could fetch all of A in one shot but that's not always the case
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.
I am not sure to understand your example, lets chat about this tomorrow
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 we split this one in two:
1 - With your Set
and the helper Keys and Values. (keep it here)
2- A second one to discuss IndexedSet
maps/sets_test.go
Outdated
func TestIndexedSet_Has(t *testing.T) { | ||
var s Set[string] | ||
|
||
assert.False(t, s.Has("foo")) | ||
|
||
s.Add("foo") | ||
|
||
assert.True(t, s.Has("foo")) | ||
assert.False(t, s.Has("bar")) | ||
} | ||
|
||
func TestIndexedSet_Delete(t *testing.T) { | ||
var s Set[string] |
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.
Those are not even using an IndexedSet 🤔
What does this PR do?
Add generics set type:
Set
as replacement forstringutil.Set
andint64util.Set
IndexedSet
to create set where the key is not the object itselfWhat are the observable changes?
New types
Good PR checklist