Strawman for new Set methods
Clone or download
gsathya Merge pull request #54 from bakkot/use-intrinsic-add
Further simplify isSubsetOf fallback behavior
Latest commit 3f8ce3f Jan 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs rebuild Jan 16, 2019
spec also use intrinsic add Jan 16, 2019
LICENSE License Jul 15, 2017
README.md Rename to isDisjointFrom Jan 14, 2019
name-bikeshedding.md Changed structure of README Dec 13, 2018
other-languages.md Changed structure of README Dec 13, 2018
package-lock.json Remove tests Dec 20, 2018
package.json Remove tests Dec 20, 2018
single-vs-multiple.md Changed structure of README Dec 13, 2018

README.md

New Set methods

See formal spec WIP.

See proposal extending Set and Map with Array-like methods.

Proposal

This proposal does not change syntax of language.

New methods based on set theory are added to Set.prototype.

  • Set.prototype.intersection(iterable) - method creates new Set instance by set intersection operation.
  • Set.prototype.union(iterable) - method creates new Set instance by set union operation.
  • Set.prototype.difference(iterable) - method creates new Set without elements present in iterable.
  • Set.prototype.symmetricDifference(iterable) - returns Set of elements found only in either this or in iterable.
  • Set.prototype.isSubsetOf(iterable)
  • Set.prototype.isDisjointFrom(iterable)
  • Set.prototype.isSupersetOf(iterable)

(Semi)relevant previous discussions

Motivations

  • reduces need to depend on Immutable.js Set<T>
  • reduces boilerplate code when dealing with common use cases of Set
  • no new syntax

Adoption

  • No npm package duplicating this proposal was found
  • Very similar API was found in popular Collections.js (205k downloads per month)
  • This proposal is inspired by Set API from Immutable.js (3M downloads per month)

Comparision with Immutable.js

  • No static intersection, union methods in this proposal
  • union, intersection, difference takes single argument

Comparison with other languages

See other languages document to get overview of Set methods in other languages.

Not included in this proposal but worth considering

  • Static Set.union(...iterables), Set.intersection(...iterables)

Polyfill

Naming

See naming bikeshedding document for details.

We decided to choose:

  • Symmetric difference - symmetricDifference
  • Intersection - intersection
  • Union - union
  • Difference - difference