Standard Library: Set
Work with sets (collections of unique values)
For more information on sets see the language reference.
Module Contents
Set.add(set, value)
Add value to set.
s = #["a", "b", "c"]
Set.add(s, "b")
Set.add(s, "d")
#["a", "b", "c"]
#["a", "b", "c", "d"]
Set.addAll(set, values)
Add each of values to set, where
values is a set, list, or table. In other words, get
the union of set and values.
s = #["a", "b", "c"]
Set.addAll(s, ["b", "e"])
Set.addAll(s, ["d", "e"])
#["a", "b", "c", "e"]
#["a", "b", "c", "d", "e"]
Set.has(set, value)
Check whether set contains value.
s = #["a", "b", "c"]
Set.has(s, "b")
Set.has(s, "d")
true
false
Note that this can also be accomplished using the
in operator.
"b" in #["a", "b", "c"]
"d" in #["a", "b", "c"]
true
false
Set.hasAll(set, values)
Check whether set contains every value in
values, where values is a set, list, or
table. In other words, check whether set is a superset
of values, and values is a subset of
set.
s = #["a", "b", "c"]
Set.hasAll(s, ["b", "c"])
Set.hasAll(s, ["d", "c"])
true
false
Set.intersection(set, values)
Get the set of elements that appear in both set and
values, where values is a set, list, or
table. In other words, get the intersection of set and
values.
s1 = #["a", "b", "c"]
s2 = #["c", "b", "e"]
Set.intersection(s1, s2)
#["b", "c"]
Set.isEmpty(set)
Check whether set is empty.
isEmpty(#[])
isEmpty(#["a", "b", "c", "d"])
true
false
(Accessible as a global through Overloads.isEmpty)
Set.len(set)
Return the number of elements in set.
s = #["a", "b", "c", "d"]
len(s)
4
(Accessible as a global through Overloads.len)
Set.merge(sets)
Merge (flatten) a list of sets into a single set. In
other words, get the union of sets.
s1 = #["a", "b", "c"]
s2 = #["d", "b", "e"]
Set.merge([s1, s2])
#["a", "b", "c", "d", "e"]
Note that sets can also be merged using the
+ operator.
s1 + s2
#["a", "b", "c", "d", "e"]
Set.of(values)
Get a set containing each value in values.
values may be a list, table, or set. If
values is a table then
Set.of(values) returns a set of the table's rows.
l = ["NY", "CA", "IL", "TX", "AZ", "PA", "TX", "CA", "TX", "FL"]
Set.of(l)
t = #{ name, type; "Ducky", "Clementine"; "dog", "bird" }
Set.of(t)
#["NY", "CA", "IL", "TX", "AZ", "PA", "FL"]
#[{ name: "Ducky", type: "Clementine" }, { name: "dog", type: "bird" }]
Set.powerset(values)
Get a list of all of the subsets of values (the
powerset of values), where values is a
set, list, or table. If values is a list or table, then
values is converted to a set prior to calculating the
powerset.
Set.powerset(["a", "b", "c"])
[
#["a", "b", "c"],
#["a", "b"],
#["a", "c"],
#["a"],
#["b", "c"],
#["b"],
#["c"],
#[],
]
Set.remove(set, value)
Remove value from set, if present.
s = #["a", "b", "c"]
Set.remove(s, "b")
Set.remove(s, "d")
#["a", "c"]
#["a", "b", "c"]
(Accessible as a global through Overloads.remove)
Set.removeAll(set, values)
Remove each of values to set, if present,
where values is a set, list, or table. In other words,
get the difference of set and values.
s = #["a", "b", "c"]
Set.removeAll(s, ["b", "c"])
Set.removeAll(s, ["d", "c"])
#["a"]
#["a", "b"]