Skip to content

Set ​

A Python-style set class providing common operations to create, modify, and query collections of unique values.

Quick Reference ​

Mutation:

FunctionDescription
add(v)Add an element to the set.
clear()Remove all elements from the set.
difference_update(set)Remove elements found in another set (in place).
discard(v)Remove an element if present, do nothing otherwise.
intersection_update(set)Keep only elements common to both sets (in place).
pop()Remove and return an arbitrary element.
symmetric_difference_update(set)Update the set with elements not shared by both (in place).
update(set)Add all elements from another set (in place).

Copying:

FunctionDescription
copy()Return a shallow copy of the set.

Set Operations:

FunctionDescription
difference(set)Return elements in this set but not in another.
intersection(set)Return elements common to both sets.
symmetric_difference(set)Return elements not shared by both sets.
union(set)Return a new set with all elements from both.

Predicates:

FunctionDescription
isdisjoint(set)Return true if sets have no elements in common.
isempty()Return true if the set has no elements.
issubset(set)Return true if all elements of this set are also in another set.
issuperset(set)Return true if this set contains all elements of another set.

Query:

FunctionDescription
len()Return the number of elements in the set.

Transform:

FunctionDescription
map(fn)Return a new set by mapping each value.
values()Return a list of all values in the set.

Functions ​

Mutation ​

add(v) ​

Add an element to the set.

lua
local s = Set({ "a" })
s:add("b")
-- result: s contains "a", "b"

clear() ​

Remove all elements from the set.

lua
local s = Set({ "a", "b" })
s:clear()
-- result: s is empty

difference_update(set) ​

Remove elements found in another set (in place).

lua
local s = Set({ "a", "b" })
s:difference_update(Set({ "b" }))
-- result: s contains "a"

discard(v) ​

Remove an element if present, do nothing otherwise.

lua
local s = Set({ "a", "b" })
s:discard("b")
-- result: s contains "a"

intersection_update(set) ​

Keep only elements common to both sets (in place).

lua
local s = Set({ "a", "b" })
s:intersection_update(Set({ "b", "c" }))
-- result: s contains "b"

pop() ​

Remove and return an arbitrary element.

lua
local v = Set({ "a", "b" }):pop()
-- result: v is either "a" or "b"

symmetric_difference_update(set) ​

Update the set with elements not shared by both (in place).

lua
local s = Set({ "a", "b" })
s:symmetric_difference_update(Set({ "b", "c" }))
-- result: s contains "a", "c"

update(set) ​

Add all elements from another set (in place).

lua
local s = Set({ "a" })
s:update(Set({ "b" }))
-- result: s contains "a", "b"

Copying ​

copy() ​

Return a shallow copy of the set.

lua
local s = Set({ "a" })
local c = s:copy()
-- result: c is a new set with "a"

Set Operations ​

difference(set) ​

Return elements in this set but not in another.

lua
local s = Set({ "a", "b" })
local d = s:difference(Set({ "b" }))
-- result: d contains "a"

intersection(set) ​

Return elements common to both sets.

lua
local s = Set({ "a", "b" })
local i = s:intersection(Set({ "b", "c" }))
-- result: i contains "b"

symmetric_difference(set) ​

Return elements not shared by both sets.

lua
local s = Set({ "a", "b" })
local d = s:symmetric_difference(Set({ "b", "c" }))
-- result: d contains "a", "c"

union(set) ​

Return a new set with all elements from both.

lua
local s = Set({ "a" }):union(Set({ "b" }))
-- result: s contains "a", "b"

Predicates ​

isdisjoint(set) ​

Return true if sets have no elements in common.

lua
local ok = Set({ "a" }):isdisjoint(Set({ "b" }))
-- result: true

isempty() ​

Return true if the set has no elements.

lua
local empty = Set({}):isempty()
-- result: true

issubset(set) ​

Return true if all elements of this set are also in another set.

lua
local ok = Set({ "a" }):issubset(Set({ "a", "b" }))
-- result: true

issuperset(set) ​

Return true if this set contains all elements of another set.

lua
local ok = Set({ "a", "b" }):issuperset(Set({ "a" }))
-- result: true

Query ​

len() ​

Return the number of elements in the set.

lua
local n = Set({ "a", "b" }):len()
-- result: 2

Transform ​

map(fn) ​

Return a new set by mapping each value.

lua
local s = Set({ 1, 2 }):map(function(v) return v * 10 end)
-- result: s contains 10, 20

values() ​

Return a list of all values in the set.

lua
local values = Set({ "a", "b" }):values()
-- result: { "a", "b" }