Add intersections and unions to Set
On Mon, Mar 4, 2013 at 10:08 AM, <aleth at instantbird.org> wrote:
It would be useful to be able to form the intersection and the union of two Sets. These are natural operations that are currently not part of the API (harmony:simple_maps_and_sets).
Similar methods would make sense for Map, but one would have to think about what to do in the case where the key but not the value matches.
An intersection is equivalent to a particular filter, so an alternative might be to add a method like Array.filter to Sets instead.
(I filed bug 847355 for this and was told this mailing list was the right place for this suggestion.)
Yes please, and also minus (remove from set A all elements it shares with set B). All three of these are fairly vital for a lot of code using sets.
The Set constructor accepts an iterable (including an Array and a Set) as an argument to populate the newly-constructed Set with several values. There should also be the possibility to add or remove multiple elements of an already-constructed Set. That covers unions and differences, but it is more generic. I would propose:
- Set.prototype.addMultiple(iterable): adds several elements
- Set.prototype.deleteMultiple(iterable): removes several elements
- Set.prototype.restrict(iterable): removes the elements that are not enumerated by the iterable.
(Naturally, the two first functions are also valuable for Maps.) For example, in order to obtain the difference of mySet1 and mySet2 as a new Set, you could write:
(new Set(mySet1)).deleteMultiple(mySet2) // copies the elements of mySet1 in a new Set, then removes the elements of mySet2.
Another potentially useful generalisation is to accept multiple iterables instead of one. For example:
- new Set(iterable, iterable, ...)
(ditto for the 'addMultiple', 'deleteMultiple' and 'restrict' methods), so that we could write
new Set(mySet1, mySet2, mySet3)
in order to create a new Set which is the union of mySet1, mySet2, and mySet3.
—Claude
Le 4 mars 2013 à 19:56, "Tab Atkins Jr." <jackalmage at gmail.com> a écrit :
Le 4 mars 2013 à 23:37, Claude Pache <claude.pache at gmail.com> a écrit :
The Set constructor accepts an iterable (including an Array and a Set) as an argument to populate the newly-constructed Set with several values. There should also be the possibility to add or remove multiple elements of an already-constructed Set. That covers unions and differences, but it is more generic. I would propose:
- Set.prototype.addMultiple(iterable): adds several elements
- Set.prototype.deleteMultiple(iterable): removes several elements
- Set.prototype.restrict(iterable): removes the elements that are not enumerated by the iterable.
Better than 'restrict': a 'filter' method (as proposed by Tab Atkins Jr. below) similar to the one we have on Arrays. For the intersection of two Sets, we can write:
mySet1.filter(x => mySet2.has(x))
This pattern has the advantage to be readily applicable to Maps and other collections with a clear semantic.
I agree on the need but forsee problems with parametrized equivalence operator [1][2] like "which comparator should be used for the union of 2 sets with different comparators?"
The need for set intersection/union/minus/etc. feels more important than the need to parametrized the comparator.
David
[1] rwldrn/tc39-notes/blob/master/es6/2013-01/jan-29.md#43-parameterize-the-equivalence-operator-for-mapset [2] rwldrn/tc39-notes/blob/master/es6/2013-01/jan-31.md#mapset-comparator
Le 04/03/2013 19:08, aleth at instantbird.org a écrit :
On Tue, Mar 5, 2013 at 8:15 AM, David Bruant <bruant.d at gmail.com> wrote:
I agree on the need but forsee problems with parametrized equivalence operator [1][2] like "which comparator should be used for the union of 2 sets with different comparators?"
I vote TypeError. If I really do intend to mix two incompatible Sets, I can go ahead and write it longhand, which is what, 3 lines? 2 with set.addAll(iterable).
The need for set intersection/union/minus/etc. feels more important than
the need to parametrized the comparator.
Not sure why this distinction matters, but I feel the opposite. You can very easily and naturally write intersection/union/minus/etc. based on the primitives already in the spec. Not so with custom equivalence! And keys that are pairs or small records are a very common need.
Intersection et al are easier but much less important.
On Tue, Mar 5, 2013 at 8:53 AM, Jason Orendorff <jason.orendorff at gmail.com> wrote:
On Tue, Mar 5, 2013 at 8:15 AM, David Bruant <bruant.d at gmail.com> wrote:
I agree on the need but forsee problems with parametrized equivalence operator [1][2] like "which comparator should be used for the union of 2 sets with different comparators?"
I vote TypeError. If I really do intend to mix two incompatible Sets, I can go ahead and write it longhand, which is what, 3 lines? 2 with set.addAll(iterable).
Alternately, just use the comparator of the first set (the one you're calling the method on). If we add generic (non-method) forms, then actually take the first one.
I'm loathe to throw a TypeError when it's difficult (impossible?) to check two sets for equivalent comparators, so you'd have to wrap every Set mixing operation in a try/catch if you can't be 100% certain that they use the same comparators.
On Mon, Mar 4, 2013 at 10:56 AM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:
On Mon, Mar 4, 2013 at 10:08 AM, <aleth at instantbird.org> wrote:
It would be useful to be able to form the intersection and the union of two Sets. These are natural operations that are currently not part of the API (harmony:simple_maps_and_sets).
Similar methods would make sense for Map, but one would have to think about what to do in the case where the key but not the value matches.
An intersection is equivalent to a particular filter, so an alternative might be to add a method like Array.filter to Sets instead.
(I filed bug 847355 for this and was told this mailing list was the right place for this suggestion.)
Yes please, and also minus (remove from set A all elements it shares with set B). All three of these are fairly vital for a lot of code using sets.
I agree that these methods would be useful. They are common set operations and they do seem missing from the Set draft spec.
Peter
+1
Additionally I propose to give more JS friendly method names:
Intersection as and
:
set1and2 = set1.and(set2);
Union as or
:
set1or2 = set1.or(set2);
Complement as not
:
set1butNot2 = set1.not(set2);
Peter Michaux wrote:
On Mon, Mar 4, 2013 at 10:56 AM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:
On Mon, Mar 4, 2013 at 10:08 AM, <aleth at instantbird.org> wrote:
It would be useful to be able to form the intersection and the union of two Sets. These are natural operations that are currently not part of the API (harmony:simple_maps_and_sets).
Similar methods would make sense for Map, but one would have to think about what to do in the case where the key but not the value matches.
An intersection is equivalent to a particular filter, so an alternative might be to add a method like Array.filter to Sets instead.
(I filed bug 847355 for this and was told this mailing list was the right place for this suggestion.)
Yes please, and also minus (remove from set A all elements it shares with set B). All three of these are fairly vital for a lot of code using sets.
I agree that these methods would be useful. They are common set operations and they do seem missing from the Set draft spec.
Peter
es-discuss mailing list es-discuss at mozilla.org, mail.mozilla.org/listinfo/es-discuss
Mariusz Nowak
It would be useful to be able to form the intersection and the union of two Sets. These are natural operations that are currently not part of the API (harmony:simple_maps_and_sets).
Similar methods would make sense for Map, but one would have to think about what to do in the case where the key but not the value matches.
An intersection is equivalent to a particular filter, so an alternative might be to add a method like Array.filter to Sets instead.
(I filed bug 847355 for this and was told this mailing list was the right place for this suggestion.)