grain icon indicating copy to clipboard operation
grain copied to clipboard

Redundant Operations in stdlib Set.intersect

Open lumi-a opened this issue 1 year ago • 1 comments

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?

lumi-a avatar May 22 '24 17:05 lumi-a

You're absolutely right—would you mind opening a PR for this?

ospencer avatar May 22 '24 17:05 ospencer