Set ​
A Python-style set class providing common operations to create, modify, and query collections of unique values.
Quick Reference ​
Mutation:
| Function | Description |
|---|---|
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:
| Function | Description |
|---|---|
copy() | Return a shallow copy of the set. |
Set Operations:
| Function | Description |
|---|---|
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:
| Function | Description |
|---|---|
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:
| Function | Description |
|---|---|
len() | Return the number of elements in the set. |
Transform:
| Function | Description |
|---|---|
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.
local s = Set({ "a" })
s:add("b")
-- result: s contains "a", "b"clear() ​
Remove all elements from the set.
local s = Set({ "a", "b" })
s:clear()
-- result: s is emptydifference_update(set) ​
Remove elements found in another set (in place).
local s = Set({ "a", "b" })
s:difference_update(Set({ "b" }))
-- result: s contains "a"discard(v) ​
Remove an element if present, do nothing otherwise.
local s = Set({ "a", "b" })
s:discard("b")
-- result: s contains "a"intersection_update(set) ​
Keep only elements common to both sets (in place).
local s = Set({ "a", "b" })
s:intersection_update(Set({ "b", "c" }))
-- result: s contains "b"pop() ​
Remove and return an arbitrary element.
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).
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).
local s = Set({ "a" })
s:update(Set({ "b" }))
-- result: s contains "a", "b"Copying ​
copy() ​
Return a shallow copy of the set.
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.
local s = Set({ "a", "b" })
local d = s:difference(Set({ "b" }))
-- result: d contains "a"intersection(set) ​
Return elements common to both sets.
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.
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.
local s = Set({ "a" }):union(Set({ "b" }))
-- result: s contains "a", "b"Predicates ​
isdisjoint(set) ​
Return true if sets have no elements in common.
local ok = Set({ "a" }):isdisjoint(Set({ "b" }))
-- result: trueisempty() ​
Return true if the set has no elements.
local empty = Set({}):isempty()
-- result: trueissubset(set) ​
Return true if all elements of this set are also in another set.
local ok = Set({ "a" }):issubset(Set({ "a", "b" }))
-- result: trueissuperset(set) ​
Return true if this set contains all elements of another set.
local ok = Set({ "a", "b" }):issuperset(Set({ "a" }))
-- result: trueQuery ​
len() ​
Return the number of elements in the set.
local n = Set({ "a", "b" }):len()
-- result: 2Transform ​
map(fn) ​
Return a new set by mapping each value.
local s = Set({ 1, 2 }):map(function(v) return v * 10 end)
-- result: s contains 10, 20values() ​
Return a list of all values in the set.
local values = Set({ "a", "b" }):values()
-- result: { "a", "b" }