-
Notifications
You must be signed in to change notification settings - Fork 14
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
sets: add sets intersection #155
Conversation
sets/sets.go
Outdated
if s.Size() < x.Size() { | ||
for item := range s.items { | ||
if x.Contains(item) { | ||
result.items[item] = struct{}{} | ||
} | ||
} | ||
} else { | ||
for item := range x.items { | ||
if s.Contains(item) { | ||
result.items[item] = 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.
Wouldn't it be better to just swap the variables and code the loop only once?
if s.Size() < x.Size() { | |
for item := range s.items { | |
if x.Contains(item) { | |
result.items[item] = struct{}{} | |
} | |
} | |
} else { | |
for item := range x.items { | |
if s.Contains(item) { | |
result.items[item] = struct{}{} | |
} | |
} | |
} | |
if s.Size() > x.Size() { | |
// Swap them to loop over the smallest set | |
s, x = x, s | |
} | |
for item := range s.items { | |
if x.Contains(item) { | |
result.items[item] = 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.
This is swapping pointers (s
is a pointer):
func (s *Set[T]) Intersection(...)
but on the other hand, in Go, "assignment to the method receiver propagates only to callees but not to callers", so at the exit of the function, the contents of the set s
do remain the same. Said in another way, the code proposed is safe, although confusing.
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.
Just wanted to notice that GitHub itself decided that "marco-m-pix4d marked this conversation resolved".
I did nothing 🙄.
I added a second commit, that I thinks makes the code less confusing.
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 remeber doing these exercises from Exercism :)
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.
Just for my understanding, what is the relation between this and PCI-3790 ?
I will use set intersection in fly_helper to determine which of the required branches are present and absent. |
Part of PCI-3790