CSet
CSet is a JavaScript lazy Set library with support for cartesian product and predicate filtering, that is loosely based on tuple relation calculus and relation algebra.
Currently CSet support most normal set operation, including cartesian product.
Changes
3.0.0
- Version schema is now based on Semantic Version System (https://semver.org/)
- From now to the future CSet will only support positive integers as elements.
- Select now supports partial filtering.
Install
npm install cset
Use (API)
CSetArray
Create a set, from Array of values. After set creation all operations on set are chainable, and they are not destructive and a new set is returned.
const CSetArray = ; const A = 1 2 3;
Intersection
Creates a set with the intersection of two sets.
const A = 1 2 3;
Intersection of cartesian/cross product
Both cartesian/cross product must have same headers, headers don't need to be in the same order.
const a = 1 2; const b = 3 4; const c = 1 2; const d = 3 5; const ab = a; const dc = d; console; // ["B", "A"]; console; // ["A", "B"]; const ab_INTERSECT_dc = ab; consol; // [[1,3],[2,3]]
Union
Creates a set with the union of two sets.
const A = 1 2 3;
Union of cartesian/cross product
Both cartesian/cross product must have same headers, headers don't need to be in the same order.
const a = 1 2; const b = 3 4; const c = 5 6; const d = 7 8; const ab = a; const cd = c; const ab_UNION_cd = ab; console; // [[1,3],[1,4],[2,3],[2,4],[5,7],[6,7],[5,8],[6,8]]
Difference
Creates a set with the difference of two sets.
const A = 1 2 3;
Difference of cartesian/cross product
Both cartesian/cross product must have same headers, headers don't need to be in the same order.
const a = 1 2; const b = 3 4; const c = 1 2; const d = 3 5; const ab = a; const dc = d; const ab_DIFFERENCE_dc = ab; console; // [[1,4],[2,4]]
SymmetricDifference
Creates a set with the symmetric difference of two sets.
const A = 1 2 3;
SymmetricDifference of cartesian/cross product
Both cartesian/cross product must have same headers, headers don't need to be in the same order.
const a = 1 2; const b = 3 4; const c = 1 2; const d = 3 5; const ab = a; const dc = d; const ab_SYMMETRIC_DIFFERENCE_dc = ab; console; // [[1,4],[2,4],[1,5],[2,5]]
Cartesian/Cross Product
Creates a set with the cartesian/cross product of two sets.
const A = 1 2 3;
Has
It checks if an element is in the provided set.
const A = 1 2 3; A; // True A; // False
Values
Iterates all values of a set.
const A = 1 2 3; for let e of A console; // will print all elements on A.
isEmpty
Checks if set is empty.
const empty = ; const intersectEmpty = 1 2; const notEmpty = 1 2; console; // True console; // True console; // False
isSubset
Check if set is a subset of other set.
const a = 0 1 2; const b = 0 1 2 3 4 5; console; // True console; // True console; // False
isProperSubset
Check if set is a proper subset of other set.
const a = 0 1 2; const b = 0 1 2 3 4 5; console; // False, all elements of a are in a, so its not proper subset. console; // True, all lements of a are in b,
isSuperset
Check if set is a superset of other set.
const a = 0 1 2; const b = 0 1 2 3 4 5; console; // False console; // True console; // True
isProperSuperset
Check if set is a proper superset of other set.
const a = 0 1 2; const b = 0 1 2 3 4 5; console; // False console; // False console; // True
isEqual
Check if two sets are equal.
const a = 0 1 2; const b = 0 1 2 3 4 5; console; // True console; // False console; // True console; // False
As
It binds an alias to a set. The "as" operation is normally useful to use with select.
const A = 1 2 3; const B = A; // A and B are same sets with different alias. const C = 4 5; const AC = A; // add alias to A and C union.
Alias on cartesian/cross products
In case of cartesian/cross products an alias work as prefix, or table name, so that each individual element on resulting tuples can still be referenced.
const ab = 1 2; const AB = ab; console; // "A.a", "A.b", "B.a", "B.b";
Header
In case of cartesian/cross product it will return an array of alias (string), for normal sets it will return one alias (string).
const A = 1 2 3; const B = 1 2 3; console; // ["A"] const AxB = A; console; // ["A", "B"]
Select
A select works as a filter on set elements, like other operators it creates a new set where all set elements must comply with provided constrains.
Select(alias, {name, predicate, partial})
- alias, an array containing name header of the restrictions,
- name, the name of the constrain, it can be any string, useful for JSON serialization.
- predicate, its a function defining a constrain, it has as arguments a value and outputs a boolean.
- partial, its similar to a constrain but it may be applied on partial values.
We must define at least a predicate or a partial function.
const a = 1 3 2; const b = a; ; ; const ab = a; ; const oddSum = a; for let e of oddSum console; /* Output: [ 1, 2 ] [ 1, 4 ] [ 3, 2 ] [ 3, 4 ] [ 2, 1 ] [ 2, 3 ] [ 2, 5 ] */
Using a partial and a predicate:
const A = 1 2 3 4 5 7 8 9 10; const B = A ; console; // [[1,2,3,4],[2,3,4,5],[7,8,9,10]];
Same example with only partial definition:
const A = 1 2 3 4 5 7 8 9 10; const B = A ; console; // [[1,2,3,4],[2,3,4,5],[7,8,9,10]];
Count
It counts the elements on a set.
const a = 1 3 2; const b = a; console; // 3
Projection
Creates a subset from original set with a restricted set of attributes.
const a = 1 2; const b = 3 4; const c = 5 6; const d = 7 8; const s = a; console; /* Output: [[7, 3], [8, 3], [7, 4], [8, 4]] */
Examples
In this section I just want to show a few examples on how CSet can be used, but some of examples may not be the best use case for the lib (See Motivation section).
Puzzle: Send+More=Money
Solve expression:
S E N D
+ M O R E
M O N E Y
Where each letter on the expression is a digit (0..9) and all letters must have different values. Some people discard M=0 solutions, but in this case I will consider all solutions including M=0.
const digits = 0 1 2 3 4 5 6 7 8 9; const d = digits; const letters = "S" "E" "N" "D" "M" "O" "R" "Y"; const s = letters; // S E N D M O R Y const sendMoreMoney = s; for let S E N D M O R Y of sendMoreMoney const send = S * 1000 + E * 100 + N * 10 + D; const more = M * 1000 + O * 100 + R * 10 + E; const money = M * 10000 + O * 1000 + N * 100 + E * 10 + Y; console;
With the use of partial filter, the problem is much more clean and optimized since we are filtering all values that are distinct.
Motivation
I created CSet to try to find a way to handle domain combinatorial explosion. The main design concept of CSet is to be lazy, do as little as possible and only do it on demand, by delaying evaluation and by failing sooner than later we can save processing time and memory.
While memory and processing time is a concern of CSet design, not all combinatorial problems are suited for CSet, CSet is meant to be used as a domain/set representation library, but not to be used for example as a Constrain Solving Problem library.
Future Work
I think I would like to grow CSet features, manly set theory stuff, and optimize the engine with a planner, cache and some other database techniques.