Skip to content

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.

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

  • set a set of a list’s items or a string’s characters, or of all the arguments, with repeats dropped
  • power_set every set that can be made from some of s’s items, including the empty set and s itself, smallest first
  • cartesian every pair [x, y] with x from a and y from b
  • is_subset whether every item of a is also in b
  • is_superset whether a has every item of b
  • is_disjoint whether a and b have no items in common
  • add add v to the set, changing it, and return the set
  • remove remove v from the set if it’s there, changing it, and return the set
1
# set
2
set(1, 2, 3)
Example outputCtrl+Enter to run
1
# {f(x) | x in xs}: f(x) for each x
2
{x * 2 | x in [1, 2, 2]}set(2, 4)
3
# {x in xs | test}: each x that passes
4
{c in "hello" | c != "l"}set("h", "e", "o")
5
# a | b: in either (union)
6
set(1, 2) | set(2, 3)set(1, 2, 3)
7
# a & b: in both (intersection)
8
set(1, 2) & set(2, 3)set(2)
9
# a - b: in a but not b (difference)
10
set(1, 2) - set(2, 3)set(1)
11
# a ^ b: in one but not both
12
set(1, 2) ^ set(2, 3)set(1, 3)
13
# x in s: is x in the set?
14
2 in set(1, 2)true
Example outputCtrl+Enter to run
1
# long, the default: one item per line
2
pretty(set(1500, "a"))
set( 1,500 "a" )
3
# short: all on one line, each item short
4
pretty(set(1500, "a"), "short")"set(1.5K, \"a\")"
Example outputCtrl+Enter to run
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)
2
set(1, 2)
3
"hello" to setset("h", "e", "l", "o")
Example outputCtrl+Enter to run

See also unique, list

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

1
set(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)]
Example outputCtrl+Enter to run

See also combinations, cartesian

cartesian(a: set|list, b: set|list) -> list

every pair [x, y] with x from a and y from b

1
cartesian(set(1, 2), set("a", "b"))[[1, "a"], [1, "b"], [2, "a"], [2, "b"]]
Example outputCtrl+Enter to run

See also power_set, combinations

is_subset(a: set, b: set) -> bool

whether every item of a is also in b

1
set(1, 2).is_subset(set(1, 2, 3))true
Example outputCtrl+Enter to run

See also is_superset

is_superset(a: set, b: set) -> bool

whether a has every item of b

1
set(1, 2, 3).is_superset(set(1, 5))false
Example outputCtrl+Enter to run

See also is_subset

is_disjoint(a: set, b: set) -> bool

whether a and b have no items in common

1
set(1, 2).is_disjoint(set(3))true
Example outputCtrl+Enter to run

See also is_subset

add(s: set, v: any) -> set

add v to the set, changing it, and return the set

1
set(1).add(2)set(1, 2)
Example outputCtrl+Enter to run

See also remove, push

remove(s: set, v: any) -> set

remove v from the set if it’s there, changing it, and return the set

1
set(1, 2).remove(1)set(2)
Example outputCtrl+Enter to run

See also add, del

1
# distinct words
2
"the cat and the hat".split.setset("the", "cat", "and", "hat")
3
# in both
4
set(1, 2, 3) & set(2, 3, 4)set(2, 3)
5
# in either, not both
6
set(1, 2, 3) ^ set(2, 3, 4)set(1, 4)
7
# order doesn't matter
8
set(1, 2) == set(2, 1)true
9
# squares, written like math's set-builder notation
10
{x ** 2 | x in 1..=5}set(1, 4, 9, 16, 25)
11
# primes below 20
12
{x in 1..20 | is_prime(x)}set(2, 3, 5, 7, 11, 13, 17, 19)
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.