A library written in TypeScript implementing the mathematical branch known as set theory - a subset of discrete mathematics.
While there is a current proposal for adding new methods to the Set class, it is not formally recommended yet. This library provides functions to coincide with the Set
class, as standardized by the ECMAScript Language Specification.
npm install @neoncitylights/sets
const a = new Set<number>([1, 2, 3, 4]);
const b = new Set<number>([4, 5, 6, 7]);
getIntersection(a, b); // new Set<number>([4])
getUnion(a, b); // new Set<number>([1, 2, 3, 4, 5, 6, 7])
const c = new Set<string>(["hello", "world"]);
const d = new Set<string>(["hello"]);
isSupersetOf(c, d); // true
isSubsetOf(d, c); // true
Auto-generated API documentation is available.
fn
: # predicates.areSetsDisjoint<TElement>(a, b): boolean • sourcefn
: # predicates.areSetsEquivalent<TElement>(a, b): boolean • sourcefn
: # predicates.areSetsEqual<TElement>(a, b): boolean • sourcefn
: # predicates.isSubsetOf<TElement>(a, b): boolean • sourcefn
: # predicates.isSupersetOf<TElement>(a, b): boolean • sourcefn
: # predicates.isProperSubsetOf<TElement>(a, b): boolean • sourcefn
: # predicates.isProperSupersetOf<TElement>(a, b): boolean • source
fn
: # operations.getUnion<TElement>(a, b): Set<TElement> • sourcefn
: # operations.getIntersection<TElement>(a, b): Set<TElement> • sourcefn
: # operations.getDifference<TElement>(a, b): Set<TElement> • sourcefn
: # operations.getSymmetricDifference<TElement>(a, b): Set<TElement> • source
fn
: # similarity.getJaccardSimilarityCoefficient<TElement>(a, b): number • sourcefn
: # similarity.getLogDice<TElement>(a, b): number • sourcefn
: # similarity.getOverlapCoefficient<TElement>(a, b): number • sourcefn
: # similarity.getSorensenDiceCoefficient<TElement>(a, b): number • source
T
: # types.SetFunction<TElement, TResult> • sourceT
: # types.SetOperation<TElement> • sourceT
: # types.SetPredicate<TElement> • sourceT
: # types.SetSimilarity<TElement> • source
A set is an unordered collection of unique elements. The type of elements can be anything, such as words, numbers, and even other sets. In math, the name of sets are usually denoted with a capital letter, like
Sets are written with curly brackets at the start and end, and each element is separated by a comma.
For expressing larger, finite sets, we can use an ellipsis in-between. They can also be at the beginning or end to indicate that the set is infinite.
Sets are written by defining a variable, and declaring a predicate (a statement that must be true or false) for that variable.
For example, to express
To express
Compared to roster notation, set-builder notation is a way of more rigorously[^rigour-wiki] defining a set. However, it is important to note that depending on the audience, it can be less accessible to readers.
Cardinality is the formal term that represents the number of elements within a set. It is also known as the "size" of the element. Given a set named
Disjoint sets are sets that have no elements in common. Given two sets named
Equivalent sets are two sets that have the same cardinality.
Equal sets are sets that have the same cardinality and the same elements.
A subset is a set where all elements of set
A superset is a set where all elements of
A proper subset is when set
A proper superset is when set
The intersection is the set of elements that are the members of two sets.
The union is the set of all elements in a collection of sets.
The difference (also known as the complement) is the set of all elements that are in set A that are not in set B.
The symmetric difference is the set that belongs to one but not both of two sets.
This library is licensed under the MIT license (LICENSE-MIT
or http://opensource.org/licenses/MIT).
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MIT license, shall be licensed as above, without any additional terms or conditions.