# Sets
Sets are persistent hash sets: unordered collections of unique
values. Build them with the `^{ ... }` literal, and combine them with
the operations below, which return new sets and leave their inputs
unchanged. Literal syntax, membership (`in?`), iteration, and
conversion are covered in Collections;
this page is the operation catalogue. All five functions are stdlib
natives available without an import.
## Functions
| Function | Purpose |
|---|---|
| `union a b` | Set of all elements from both sets |
| `intersection a b` | Elements common to both sets |
| `difference a b` | Elements in `a` but not in `b` |
| `subset? a b` | `true` when every element of `a` is also in `b` |
| `superset? a b` | `true` when `a` contains every element of `b` |
Notes:
- The three set operations return a new set; the inputs are never
mutated.
- `subset?` and `superset?` are relation predicates returning booleans.
Every set is a subset and superset of itself, and the empty set is a
subset of every set.
- Duplicates collapse at construction: `^{ :a :a :b }` is `^{:a :b}`.
- Membership uses `in?` (`[in? $roles :admin]`), and `into ^{}` builds a
set from any sequence.
## Examples
def a ^{ 1 2 3 } def b ^{ 2 3 4 } [union $a $b] # => ^{1 2 3 4} [intersection $a $b] # => ^{2 3} [difference $a $b] # => ^{1} [subset? ^{ 1 2 } $a] # => true [superset? $a ^{ 1 2 }] # => true [subset? $a $b] # => false [superset? ^{ 1 2 } ^{ 1 2 }] # => true # Membership and construction [in? ^{ :admin :user } :admin] # => true [into ^{} [map { $it }] @{ 1 2 2 }] # => ^{1 2} ^{ :a :a :b } # => ^{:a :b} # Comparing capabilities def editors ^{ :read :write } def admins ^{ :read :write :delete } [subset? $editors $admins] # => true [difference $admins $editors] # => ^{:delete}
## See Also
- Collections — set literals, `in?`, and iteration
- Lists — persistent list operations
- Maps — persistent map operations