Standard Library: Set

Work with sets (collections of unique values)

For more information on sets see the language reference.

  1. add
  2. addAll
  3. empty
  4. has
  5. hasAll
  6. intersection
  7. isEmpty
  8. len
  9. merge
  10. of
  11. powerset
  12. remove
  13. removeAll

Set.add(set, value)

Add value to set.

s = Set.of(["a", "b", "c"])
Set.add(s, "b")
Set.add(s, "d")
Set.of(["a", "b", "c"])
Set.of(["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 = Set.of(["a", "b", "c"])
Set.addAll(s, ["b", "e"])
Set.addAll(s, ["d", "e"])
Set.of(["a", "b", "c", "e"])
Set.of(["a", "b", "c", "d", "e"])

Set.empty

The empty set.

Set.of([])

Set.has(set, value)

Check whether set contains value.

s = Set.of(["a", "b", "c"])
Set.has(s, "b")
Set.has(s, "d")
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 = Set.of(["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 = Set.of(["a", "b", "c"])
s2 = Set.of(["c", "b", "e"])
Set.intersection(s1, s2)
Set.of(["b", "c"])

Set.isEmpty(set)

Check whether set is empty.

isEmpty(Set.of([]))
isEmpty(Set.of(["a", "b", "c", "d"]))
true
false

(Accessible as a global through Overloads.isEmpty)


Set.len(set)

Return the number of elements in set.

s = Set.of(["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 = Set.of(["a", "b", "c"])
s2 = Set.of(["d", "b", "e"])
Set.merge([s1, s2])
Set.of(["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 = Table.of({ name: ["Ducky", "Clementine"], type: ["dog", "bird"] })
Set.of(t)
Set.of(["NY", "CA", "IL", "TX", "AZ", "PA", "FL"])
Set.of([{ name: "Ducky", type: "dog" }, { name: "Clementine", 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"])
[
  Set.of(["a", "b", "c"]),
  Set.of(["a", "b"]),
  Set.of(["a", "c"]),
  Set.of(["a"]),
  Set.of(["b", "c"]),
  Set.of(["b"]),
  Set.of(["c"]),
  Set.of([]),
]

Set.remove(set, value)

Remove value from set, if present.

s = Set.of(["a", "b", "c"])
Set.remove(s, "b")
Set.remove(s, "d")
Set.of(["a", "c"])
Set.of(["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 = Set.of(["a", "b", "c"])
Set.removeAll(s, ["b", "c"])
Set.removeAll(s, ["d", "c"])
Set.of(["a"])
Set.of(["a", "b"])