| description | A set class providing common operations to create, modify, and query collections of unique values. |
|---|
A set class providing common operations to create, modify, and query collections of unique values.
Set = require "mods.Set"
s = Set({ "a" })
print(s:contains("a")) --> trueMutation:
| 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). |
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. |
difference(set) |
Return elements in this set but not in another. |
intersection(set) |
Return elements common to both sets. |
remove(v) |
Remove an element if present, do nothing otherwise. |
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. |
equals(set) |
Return true when both sets contain exactly the same members. |
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 |
|---|---|
contains(v) |
Return true if the set contains v. |
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. |
Metamethods:
| Function | Description |
|---|---|
__add(set) |
Return the union of two sets using +. |
__bor(set) |
Return the union of two sets using |. |
__band(set) |
Return the intersection of two sets using &. |
__bxor(set) |
Return elements present in exactly one set using ^. |
__eq(set) |
Return true if both sets contain exactly the same members using ==. |
__le(set) |
Return true if the left set is a subset of the right set using <=. |
__lt(set) |
Return true if the left set is a proper subset of the right set using <. |
__sub(set) |
Return the difference of two sets using -. |
In-place operations that mutate the current set.
Add an element to the set.
Parameters:
v(any): Value to add.
Return:
self(T): Current set instance.
Example:
s = Set({ "a" }):add("b") --> s contains "a", "b"Remove all elements from the set.
Return:
self(T): Current set instance.
Example:
s = Set({ "a", "b" }):clear() --> s is emptyRemove elements found in another set (in place).
Parameters:
set(T): Other set value.
Return:
self(T): Current set instance.
Example:
s = Set({ "a", "b" }):difference_update(Set({ "b" })) --> s contains "a"Keep only elements common to both sets (in place).
Parameters:
set(T): Other set value.
Return:
self(T): Current set instance.
Example:
s = Set({ "a", "b" }):intersection_update(Set({ "b", "c" }))
--> s contains "b"Remove and return an arbitrary element.
Return:
value(any): Removed value, ornilwhen the set is empty.
Example:
v = Set({ "a", "b" }):pop() --> v is either "a" or "b"Update the set with elements not shared by both (in place).
Parameters:
set(T): Other set value.
Return:
self(T): Current set instance.
Example:
s = Set({ "a", "b" }):symmetric_difference_update(Set({ "b", "c" }))
--> s contains "a", "c"Add all elements from another set (in place).
Parameters:
set(T): Other set value.
Return:
self(T): Current set instance.
Example:
s = Set({ "a" }):update(Set({ "b" })) --> s contains "a", "b"Non-mutating set operations that return new set instances.
Return a shallow copy of the set.
Return:
set(mods.Set): New set.
Example:
c = Set({ "a" }):copy() --> c is a new set with "a"Return elements in this set but not in another.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
d = Set({ "a", "b" }):difference(Set({ "b" })) --> d contains "a"Note
difference is also available as the __sub (-) operator.
a:difference(b) is equivalent to a - b.
Return elements common to both sets.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
i = Set({ "a", "b" }):intersection(Set({ "b", "c" })) --> i contains "b"Note
intersection is also available as __band (&) on Lua 5.3+.
Remove an element if present, do nothing otherwise.
Parameters:
v(any): Value to remove.
Return:
self(T): Current set instance.
Example:
s = Set({ "a", "b" }):remove("b") --> s contains "a"Return elements not shared by both sets.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
d = Set({ "a", "b" }):symmetric_difference(Set({ "b", "c" }))
--> d contains "a", "c"Note
symmetric_difference is also available as __bxor (^) on Lua 5.3+.
Return a new set with all elements from both.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
s = Set({ "a" }):union(Set({ "b" })) --> s contains "a", "b"Note
union is available as __add (+) and __bor (|) on Lua 5.3+.
a:union(b) is equivalent to a + b and a | b.
Boolean checks about set relationships and emptiness.
Return true if sets have no elements in common.
Parameters:
set(T): Other set value.
Return:
ok(boolean): True when sets have no elements in common.
Example:
ok = Set({ "a" }):isdisjoint(Set({ "b" })) --> trueReturn true when both sets contain exactly the same members.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
ok(boolean): True when both sets contain the same members.
Example:
a = Set({ "a", "b" })
b = Set({ "b", "a" })
ok = a:equals(b) --> trueNote
equals is also available as the __eq (==) operator. a:equals(b) is
equivalent to a == b.
Return true if the set has no elements.
Return:
ok(boolean): True when the set has no elements.
Example:
empty = Set({}):isempty() --> trueReturn true if all elements of this set are also in another set.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
ok(boolean): True when every element ofselfexists inset.
Example:
ok = Set({ "a" }):issubset(Set({ "a", "b" })) --> trueNote
issubset is also available as the __le (<=) operator. a:issubset(b) is
equivalent to a <= b.
Return true if this set contains all elements of another set.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
ok(boolean): True whenselfcontains every element ofset.
Example:
ok = Set({ "a", "b" }):issuperset(Set({ "a" })) --> trueRead-only queries for membership and size.
Return true if the set contains v.
Parameters:
v(any): Value to check.
Return:
ok(boolean): True whenvis present in the set.
Example:
ok = Set({ "a", "b" }):contains("a") --> true
ok = Set({ "a", "b" }):contains("z") --> falseReturn the number of elements in the set.
Return:
count(integer): Element count.
Example:
n = Set({ "a", "b" }):len() --> 2Value-to-value transformations and projection helpers.
Return a new set by mapping each value.
Parameters:
fn(fun(v:any):any): Mapping function.
Return:
set(mods.Set): New set.
Example:
s = Set({ 1, 2 }):map(function(v) return v * 10 end) --> s contains 10, 20Return a list of all values in the set.
Return:
values(mods.List): List of set values.
Example:
values = Set({ "a", "b" }):values() --> { "a", "b" }Return the union of two sets using +.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
a = Set({ "a", "b" })
b = Set({ "b", "c" })
u = a + b --> { a = true, b = true, c = true }Note
__add is the operator form of :union(set).
Return the union of two sets using |.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
a = Set({ "a", "b" })
b = Set({ "b", "c" })
u = a | b --> { a = true, b = true, c = true }Note
__bor is the operator form of :union(set) on Lua 5.3+.
Return the intersection of two sets using &.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
a = Set({ "a", "b" })
b = Set({ "b", "c" })
i = a & b --> { b = true }Note
__band is the operator form of :intersection(set) on Lua 5.3+.
Return elements present in exactly one set using ^.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
a = Set({ "a", "b" })
b = Set({ "b", "c" })
d = a ^ b --> { a = true, c = true }Note
__bxor is the operator form of :symmetric_difference(set) on Lua 5.3+.
Return true if both sets contain exactly the same members using ==.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
ok(boolean): True when both sets contain the same members.
Example:
ok = Set({ "a", "b" }) == Set({ "b", "a" }) --> trueNote
__eq is the operator form of :equals(set).
Return true if the left set is a subset of the right set using <=.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
ok(boolean): True whenselfis a subset ofset.
Example:
a = Set({ "a" })
b = Set({ "a", "b" })
ok = a <= b --> trueNote
__le is the operator form of :issubset(set).
Return true if the left set is a proper subset of the right set using <.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
ok(boolean): True whenselfis a proper subset ofset.
Example:
a = Set({ "a" })
b = Set({ "a", "b" })
ok = a < b --> trueReturn the difference of two sets using -.
Parameters:
set(mods.Set|table<any,true>): Other set value.
Return:
set(mods.Set): New set.
Example:
a = Set({ "a", "b" })
b = Set({ "b", "c" })
d = a - b --> { a = true }Note
__sub is the operator form of :difference(set).