set
A set is an unordered collection of unique values. Sets are useful for representing groups of items where duplicates are not allowed, and for performing mathematical set operations like union, intersection, and difference.
Operator | Function | Description |
|---|---|---|
| Computes the union of two sets. | |
| Computes the intersection of two sets. | |
| Computes the difference between two sets. | |
| Determines if one set is a subset of another. | |
| Determines if one set is a proper subset of another. | |
| Determines if one set is a superset of another. | |
| Determines if one set is a proper superset of another. | |
| Checks if an element is a member of a set. | |
| Checks if an element is not a member of a set. | |
| Computes the symdifference between two sets. |
Syntax
A set is declared using curly braces {}.
Elements are separated by commas ,. Duplicate elements are automatically removed.
{} -- An empty set
{1, 2, 3} -- A set of numbers
{"a", "b", "c"} -- A set of strings
{1, 1, 2, 3} -- Duplicates are removed
When parsed and rendered, this code will appear as:
{}--An empty set
{1, 2, 3}--A set of numbers
{"a", "b", "c"}--A set of strings
{1, 1, 2, 3}--Duplicates are removed
Sets are unordered. The order in which elements appear does not affect equality or behavior.
Kind
In general, a set has the kind:
<{T}:N>Where T is the kind of the elements, and N is the number of elements. The number of elements can be omitted to compare sets of different sizes, but strictly speaking, two sets of different sizes have different kinds.
For example:
kind: <{f64}:3>
kind: <{f64}:2>
Other examples include:
kind: <{f64}:3>
kind: <{string}:2>
kind: <{(f64,f64)}:2>
Constructing
There are three ways to create sets:
Set Literals
Set Comprehensions
From Matrices
Set Literals
An empty set is written as {}, and has kind {<{_}>}:
Non-empty sets are written with elements inside the braces, separated by commas:
Set Comprehensions
Sets can be constructed using set comprehensions, which generate elements based on patterns and predicates.
Comprehensions may contain:
One or more generators (
<-)Boolean conditions
Let bindings
Arbitrary expressions for the yielded element
For example, the following comprehension generates the set of friends-of-friends for a given user, excluding those with an ID of 9 or higher:
For more, see set/comprehensions
From Matrices
You can create a set from a matrix using a kind annotation, as long as the kinds are compatible. For example, if you have a matrix of f64 values, you can create a set of kind f64:
x := [1.1 2.2; 3.3 4.4]
s<{f64}> := x
Accessing Elements
Because sets are unordered collections, you cannot access elements by index or position like you would with lists or arrays.
Instead, elements are accessed through:
Membership tests -- checking if an element is in the set or not
Set operations -- union, intersection, difference, etc.
Iteration (via comprehensions) -- to process or transform elements one by one, but not in any specific order
Membership
Use ∈ to test membership:
For more, see set/element-of
Use ∉ for non-membership:
For more, see set/not-element-of
Assigning Elements
Sets are immutable, meaning that once they are created you cannot add or remove elements from them.
Instead, new sets are created using set operations. To add elements to a set, use the union operator ∪ to combine the existing set with a new element:
Creates a new set
To remove elements from a set, use the difference operator ∖:
Creates a new set without 2
Operations
Mech supports standard mathematical set operations.
Union
The union of two sets contains all elements that appear in either set.
For more, see set/union
Intersection
The intersection contains elements common to both sets.
For more, see set/intersection
Difference
The difference A ∖ B contains elements in A that are not in B.
For more, see set/difference
Subset and Superset
The subset operator ⊆ tests if all elements of one set are contained in another:
For more, see set/subset
Similarly, the superset operator ⊇ tests if a set contains all elements of another:
For more, see set/superset
Proper Subset and Superset
For more, see set/proper-subset
Likewise, the proper superset operator ⊃ tests if a set strictly contains another:
For more, see set/proper-superset
If the sets are equal, strict relations are false:
Also: