sets

Set algebra: union, intersection, difference, subset tests, and filtering.

Oxigen
introduce sets

Every function here returns a new set and leaves the ones you pass in alone. Adding and removing single values are global builtins that change a set in place: push(s, x), insert(s, x), remove(s, x), pop(s), extend(s, items), clear(s), copy(s) and has(s, x). See builtins.md.

The module is sets, not set, so importing it doesn't hide the set() builtin.

FunctionSignatureReturns
unionunion(a, b)set
intersectionintersection(a, b)set
differencedifference(a, b)set
symmetric_differencesymmetric_difference(a, b)set
is_subsetis_subset(a, b)bool
is_supersetis_superset(a, b)bool
is_disjointis_disjoint(a, b)bool
to_arrayto_array(s)array
filterfilter(s, pred)set
mapmap(s, f)set

#Combining

#union(a, b)

Every value in a or b. Values from a come first, then the new ones from b.

Oxigen
println(sets.union(set(1, 2), set(2, 3)))   // set(1, 2, 3)

#intersection(a, b)

The values in both a and b, in a's order.

Oxigen
println(sets.intersection(set(1, 2, 3), set(2, 3, 4)))   // set(2, 3)

#difference(a, b)

The values in a that aren't in b.

Oxigen
println(sets.difference(set(1, 2, 3), set(2)))   // set(1, 3)

#symmetric_difference(a, b)

The values in exactly one of a and b.

Oxigen
println(sets.symmetric_difference(set(1, 2), set(2, 3)))   // set(1, 3)

#Comparing

#is_subset(a, b)

Whether every value in a is also in b. The empty set is a subset of every set, and a set is a subset of itself.

Oxigen
println(sets.is_subset(set(1, 2), set(1, 2, 3)))   // True
println(sets.is_subset(set(1, 9), set(1, 2, 3)))   // False

#is_superset(a, b)

Whether a holds every value in b: is_subset(b, a).

Oxigen
println(sets.is_superset(set(1, 2, 3), set(3)))   // True

#is_disjoint(a, b)

Whether a and b have no values in common.

Oxigen
println(sets.is_disjoint(set(1, 2), set(3, 4)))   // True
println(sets.is_disjoint(set(1, 2), set(2, 3)))   // False

To compare two sets for equality, use ==: sets are equal when they hold the same values, in any order.


#Transforms

#to_array(s)

The values as an array, in the set's order. To go the other way, use set(arr).

Oxigen
println(sets.to_array(set(3, 1, 2)))   // [3, 1, 2]

#filter(s, pred)

Keep the values for which pred returns true.

Oxigen
println(sets.filter(set(1, 2, 3, 4), fun(x) { x % 2 == 0 }))   // set(2, 4)

#map(s, f)

Apply f to every value. Results that come out equal are kept once, so the new set can be smaller.

Oxigen
println(sets.map(set(1, 2, 3), fun(x) { x % 2 }))   // set(1, 0)