Giter Site home page Giter Site logo

dalavancloud / proposal-set-methods Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tc39/proposal-set-methods

1.0 3.0 0.0 90 KB

Strawman for new Set methods

Home Page: https://tc39.github.io/proposal-set-methods/

License: Other

JavaScript 63.44% HTML 36.56%

proposal-set-methods's Introduction

New Set methods

See formal spec WIP.

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

(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

Java

Java Set interface is rather limited and doesn't include APIs found in this proposal.

Though, Java 8 introduces stream API that allow to deal with arbitrary collections.

Stream API has methods from this proposal like union (as concat).

Stream API nor Set does not include intersection, xor.

Other languages

.union, .intersection etc. desired signature

Signature of these functions isn't obvious. They can accept:

  • single Set
    • best possible performance
    • would slowdown adoption
  • multiple Sets
    • looks like rare use case
  • Single iterable
  • Multiple iterables
    • Used by Immutable.js
    • Certain algorithms can be ineffective without converting arguments to Set instances (intersection without at least log(n) random access seems to be too slow for real world)

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.

Not included in this proposal but worth considering

  • Set.prototype.isSubsetOf(otherSet)
  • Set.prototype.isSupersetOf(iterable)
  • Static Set.union(...iterables), Set.intersection(...iterables)

Naming

Symmetric difference

Proposed names: xor, symmetricDifference, disjointUnion

Intersection

Proposed names: intersect, intersection.

Union

Proposed names: union, concat.

Difference

Proposed names: difference, minus, except, without, withoutAll, relativeComplement.

proposal-set-methods's People

Contributors

ginden avatar gsathya avatar michaelficarra avatar mathiasbynens avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.