data.sets
collections with no repeats: combine, compare and build sets
A set is a collection where each value appears at most once. Adding a value that’s already there does nothing, and two sets are equal when they hold the same values in any order.
good to know
Section titled “good to know”Sets remember the order values were first added in, and show them in that order. They can hold integers, strings, true, false and nil, but not floats, lists or maps.
Tags: collections
seta set of a list’s items or a string’s characters, or of all the arguments, with repeats droppedpower_setevery set that can be made from some of s’s items, including the empty set and s itself, smallest firstcartesianevery pair [x, y] with x from a and y from bis_subsetwhether every item of a is also in bis_supersetwhether a has every item of bis_disjointwhether a and b have no items in commonaddadd v to the set, changing it, and return the setremoveremove v from the set if it’s there, changing it, and return the set
1# set2set(1, 2, 3)syntax
Section titled “syntax”1# {f(x) | x in xs}: f(x) for each x2{x * 2 | x in [1, 2, 2]}set(2, 4)3# {x in xs | test}: each x that passes4{c in "hello" | c != "l"}set("h", "e", "o")5# a | b: in either (union)6set(1, 2) | set(2, 3)set(1, 2, 3)7# a & b: in both (intersection)8set(1, 2) & set(2, 3)set(2)9# a - b: in a but not b (difference)10set(1, 2) - set(2, 3)set(1)11# a ^ b: in one but not both12set(1, 2) ^ set(2, 3)set(1, 3)13# x in s: is x in the set?142 in set(1, 2)truepretty
Section titled “pretty”1# long, the default: one item per line2pretty(set(1500, "a"))set(
1,500
"a"
)3# short: all on one line, each item short4pretty(set(1500, "a"), "short")"set(1.5K, \"a\")"Functions
Section titled “Functions”set(xs: list|str|set) -> set / set(*items: any) -> set
a set of a list’s items or a string’s characters, or of all the arguments, with repeats dropped
1[3, 1, 3, 2].setset(3, 1, 2)2set(1, 2)3"hello" to setset("h", "e", "l", "o")power_set
Section titled “power_set”power_set(s: set) -> list
every set that can be made from some of s’s items, including the empty set and s itself, smallest first
1set(1, 2, 3).power_set[set(), set(1), set(2), set(3), set(1, 2), set(1, 3), set(2, 3), set(1, 2, 3)]See also combinations, cartesian
cartesian
Section titled “cartesian”cartesian(a: set|list, b: set|list) -> list
every pair [x, y] with x from a and y from b
1cartesian(set(1, 2), set("a", "b"))[[1, "a"], [1, "b"], [2, "a"], [2, "b"]]See also power_set, combinations
is_subset
Section titled “is_subset”is_subset(a: set, b: set) -> bool
whether every item of a is also in b
1set(1, 2).is_subset(set(1, 2, 3))trueSee also is_superset
is_superset
Section titled “is_superset”is_superset(a: set, b: set) -> bool
whether a has every item of b
1set(1, 2, 3).is_superset(set(1, 5))falseSee also is_subset
is_disjoint
Section titled “is_disjoint”is_disjoint(a: set, b: set) -> bool
whether a and b have no items in common
1set(1, 2).is_disjoint(set(3))trueSee also is_subset
add(s: set, v: any) -> set
add v to the set, changing it, and return the set
1set(1).add(2)set(1, 2)remove
Section titled “remove”remove(s: set, v: any) -> set
remove v from the set if it’s there, changing it, and return the set
1set(1, 2).remove(1)set(2)More examples
Section titled “More examples”1# distinct words2"the cat and the hat".split.setset("the", "cat", "and", "hat")3# in both4set(1, 2, 3) & set(2, 3, 4)set(2, 3)5# in either, not both6set(1, 2, 3) ^ set(2, 3, 4)set(1, 4)7# order doesn't matter8set(1, 2) == set(2, 1)true9# squares, written like math's set-builder notation10{x ** 2 | x in 1..=5}set(1, 4, 9, 16, 25)11# primes below 2012{x in 1..20 | is_prime(x)}set(2, 3, 5, 7, 11, 13, 17, 19)Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.