grain
grain copied to clipboard
Redundant Operations in stdlib Set.intersect
The method Set.intersect is implemented as:
provide let intersect = (set1, set2) => {
let set = make()
forEach(key => {
if (contains(key, set2)) {
add(key, set)
}
}, set1)
forEach(key => {
if (contains(key, set1)) {
add(key, set)
}
}, set2)
set
}
Isn't the second forEach-loop redundant? If there is a key in set2 such that contains(key, set1)==true, then this key would already have been added to the intersection set in the first iteration, because then contains(key, set2)==true as well? Or is the equality-relation on keys not necessarily symmetric?
In other words, are there sets set1, set2 such that the second loop adds elements to set that the first loop didn't?
You're absolutely right—would you mind opening a PR for this?