Add intersections and unions to Set

Claude Pache claude.pache at
Mon Mar 4 14:37:46 PST 2013

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.


Le 4 mars 2013 à 19:56, "Tab Atkins Jr." <jackalmage at> a écrit :

> On Mon, Mar 4, 2013 at 10:08 AM,  <aleth at> 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
>> (
>> 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.
> ~TJ
> _______________________________________________
> es-discuss mailing list
> es-discuss at

More information about the es-discuss mailing list