On this page:
Set
Set.of
Readable  Set
Mutable  Set
Set.by
Mutable  Set.by
Set
Set
Set.by
Set.by
Set
Readable  Set
Set.by
Set
Mutable  Set
Mutable  Set.by
Set.empty
Set.empty
Readable  Set.empty
Readable  Set.empty
Set.length
Set.get
Set.append
Set.union
Set.intersect
Set.remove
Mutable  Set.set
Mutable  Set.delete
Set.to_  list
Set.copy
Set.snapshot
Set.to_  sequence
8.12

6.35 Sets🔗ℹ

Immutable sets can be constructed using the syntax {val_expr, ...}, which creates a set containing the values of the val_exprs. More precisely, a use of curly braces with no preceding expression is parsed as an implicit use of the #%braces form.

A set is indexable, and [] after a set expression with an expression for a value checks for membership: the result is a boolean indicating whether the value is in the set. Mutable sets can be updated with a combination of [] and the := operator, where a #false result on the right-hand side of := removes an element from a set, and any other right-hand side result causes the value to be included in the set. (Use ++ to functionally add to an immutable set.) These uses of [] are implemented by #%index. A set can be used as sequence, in which case it supplies its elements in an unspecified order.

The . operator can be used on a readable set (immutable or mutable) expression as equivalent to calling Set functions:

st.length()

 is 

Set.length(st)

st.get(v)

 is 

Set.get(st, v)

st.to_list(try_sort, ...)

 is 

Set.to_list(st, try_sort, ...)

st.copy()

 is 

Set.copy(st)

st.snapshot()

 is 

Set.snapshot(st)

st.to_sequence()

 is 

Set.to_sequence(st)

The . operator can be used on a set (immutable only) expression as equivalent to calling Set functions:

st.append(st2, ...)

 is 

Set.append(st, st2, ...)

st.union(st2, ...)

 is 

Set.union(st, st2, ...)

st.intersect(st2, ...)

 is 

Set.intersect(st, st2, ...)

st.remove(v)

 is 

Set.remove(st, v)

The . operator can be used on a mutable set expression as equivalent to calling MutableSet functions:

st.set(v, in)

 is 

MutableSet.set(st, v, in)

st.delete(v)

 is 

MutableSet.delete(st, v)

annotation

Set

 

annotation

Set.of(annot)

 

annotation

ReadableSet

 

annotation

MutableSet

 

annotation

Set.by(key_comp)

 

annotation

Set.by(key_comp).of(annot)

 

annotation

MutableSet.by(key_comp)

The Set annotation matches any set. The of variants match a set whose values satisfy annot.

ReadableSet matches both mutable and immutable maps, while MutableSet matches mutable maps (created with, for example, the MutableSet constructor).

The Set.by and MutableSet.by annotation variants match only sets that use the hash and equality procedures specified by key_comp.

Static information associated by Set, etc., makes an expression acceptable to for in static mode.

expression

Set{expr_or_splice, ...}

 

repetition

Set{repet_or_splice, ...}

 

expr_or_splice

 = 

expr

 | 

repet , ellipses

 | 

& set_expr

 

ellipses

 = 

ellipsis

 | 

ellipses , ellipsis

 

ellipsis

 = 

...

 

function

fun Set(val :: Any, ...) :: Set

 

expression

Set.by(key_comp){expr_or_splice, ...}

 

expression

Set.by(key_comp)

 

repetition

Set.by(key_comp){repet_or_splice, ...}

Constructs an immutable set containing given values, equivalent to using {expr_or_splice, ...} to form a set (see #%braces).

Note that Set{} and Set() produce an empty set while {} does not, since {} produces an empty map instead.

The Set.by variants create a map that uses the equality and hashing functions specified by key_comp.

> def s = Set{"x", 1, "y", 2}

> s

{1, 2, "x", "y"}

> s["x"]

#true

> s[1]

#true

> s[42]

#false

> Set("x", 1, "y", 2)

{1, 2, "x", "y"}

binding operator

Set{expr, ...}

 

binding operator

Set{expr, ..., rest}

 

binding operator

Set(expr, ...)

 

binding operator

Set(expr, ..., rest)

 

binding operator

ReadableSet{expr, ...}

 

binding operator

ReadableSet{expr, ..., rest}

 

binding operator

ReadableSet(expr, ...)

 

binding operator

ReadableSet(expr, ..., rest)

 

binding operator

Set.by(key_comp){expr, ...}

 

binding operator

Set.by(key_comp){expr, ..., rest}

 

binding operator

Set.by(key_comp)(expr, ...)

 

binding operator

Set.by(key_comp)(expr, ..., rest)

 

rest

 = 

& set_bind

 | 

rest_bind , ellipsis

 

ellipsis

 = 

...

Matches a set containing at least the values computed by the exprs. The matched set may have additional values. If & set_bind is supplied, the rest of the set excluding the values of the given exprs must match the set_bind. Static information associated by Set is propagated to set_bind. If rest_bind followed by ... is supplied, the rest of the set excluding the given exprs must have individual values that match rest_bind, and identifiers in rest_bind are bound as repetitions. Values matching rest_bind are extracted eagerly and preserved in an internal list to implement the repetition.

The Set binding forms match only immutable sets, while ReadableSet forms match both immutable and mutable sets. For ReadableSet, the & set_bind will match a snapshot (in the sense of Set.snapshot) of the rest of the set. The Set.by binding forms match only immutable sets constructed using key_comp.

> def Set{"x", "y"} = {"x", "y"}

> def Set{"x", "y"} = {"x"}

def: value does not satisfy annotation

  value: {"x"}

  annotation: matching(Set{"x", "y"})

> def Set{"a"} = {"a", "b"}

> def Set{"a", & rst} = {"a", "b", "c"}

> rst

{"b", "c"}

> def Set{"a", val, ...} = {"a", "b", "c"}

> [val, ...]

["b", "c"]

reducer

Set

A reducer used with for, accumulates values into a set.

expression

MutableSet{val_expr, ...}

 

function

fun MutableSet(val :: Any, ...) :: MutableSet

 

expression

MutableSet.by(key_comp){val_expr, ...}

 

expression

MutableSet.by(key_comp)

Similar to Set as a constructor, but creates a mutable set that can be updated using :=.

Note that ... and & are not supported for construction mutable sets, only immutable sets.

> def m = MutableSet{"x", 1, "y", 2}

> m

MutableSet{1, 2, "x", "y"}

> m["x"]

#true

> m["x"] := #false

> m

MutableSet{1, 2, "y"}

> m["x"] := #true

> m

MutableSet{1, 2, "x", "y"}

value

def Set.empty :: Set = Set{}

 

binding operator

Set.empty

 

value

def ReadableSet.empty :: ReadableSet = Set{}

 

binding operator

ReadableSet.empty

An empty set. The Set.empty binding form differs from from Set{}, because Set.empty matches only an empty immutable set, while Set{} matches any immutable set.

The ReadableSet.empty binding form matches an empty set whether it is mutable or immutable.

Corresponding to the binding forms, Set.empty and ReadableSet.empty are bound to Set{} with approapriate static information.

> Set.empty

Set{}

> match Set()

  | Set.empty: "empty set"

  | _: #false

"empty set"

> match MutableSet()

  | Set.empty: "empty immutable set"

  | _: #false

#false

> match MutableSet()

  | ReadableSet.empty: "empty set for now"

  | _: #false

"empty set for now"

function

fun Set.length(st :: ReadableSet) :: Int

Returns the number of values in st.

> Set.length({"a", "b"})

2

> Set.length(Set{})

0

> {"a", "b"}.length()

2

> Set{}.length()

0

function

fun Set.get(st :: ReadableSet, val :: Any) :: Boolean

Equivalent to st[val] (with the default implicit #%index form). Returns #true if val is in st, #false otherwise.

> {"a", "b"}.get("a")

#true

> {"a", "b"}["a"]

#true

function

fun Set.append(st :: Set, ...) :: Set

 

function

fun Set.union(st :: Set, ...) :: Set

 

function

fun Set.intersect(st :: Set, ...) :: Set

Returns the union of the sts in the case of Set.append or Set.union, or the intersection of the sets in the case of Set.intersect.

> {1, 2, 3}.append({2, 3}, {3, 4})

{1, 2, 3, 4}

> {1, 2, 3}.union({2, 3}, {3, 4})

{1, 2, 3, 4}

> {1, 2, 3}.intersect({2, 3}, {3, 4})

{3}

function

fun Set.remove(st :: Set, val :: Any) :: Set

Returns a set like st but without val, if it is present.

> {1, 2, 3}.remove(2)

{1, 3}

> {1, 2, 3}.remove(4)

{1, 2, 3}

function

fun MutableSet.set(st :: MutableSet,

                   val :: Any, in :: Any)

  :: Void

Equivalent to st[val] := in (with the default implicit #%index form). Changes st to remove val if in is #false, otherwise add val.

> def s = MutableSet{1, 2, 3}

> s.set(1, #false)

> s

MutableSet{2, 3}

> s[1] := #true

> s

MutableSet{1, 2, 3}

function

fun MutableSet.delete(st :: MutableSet, val :: Any)

  :: Void

Changes st to remove val, if it is present.

> def s = MutableSet{1, 2, 3}

> s.delete(2)

> s

MutableSet{1, 3}

function

fun Set.to_list(st :: ReadableSet,

                try_sort :: Any = #false)

  :: List

Returns the elements of st in a list. If try_sort is true, then the elements are sorted to the degree that a built-in comparison can sort them. Note that sets do not implement Listable, because the order of elements is unspecified.

> {1, 2, 3}.to_list(#true)

[1, 2, 3]

Creates a mutable set whose initial content matches st.

function

fun Set.snapshot(st :: ReadableSet) :: Set

Returns an immutable set whose content matches st. If st is immutable, then it is the result.

Implements Sequenceable by returning a sequence of st’s elements in an unspecified order.