Set
Sets are a collection of values. They are used to store unique values. They are essentially a dictionary, but each value is stored as a boolean. This means that a value can only be in a set once.
local set = { hello = true }
local newSet = Add(set, "world") -- { hello = true, world = true }
Functions
copy
Set.copy(set: {[T]: boolean}--
The set to copy.
) → {[T]: boolean}--
A copy of the set.
Creates a copy of a set.
local set = { hello = true }
local newSet = Copy(set) -- { hello = true }
fromArray
Set.fromArray(array: {T}--
The array to convert to a set.
) → {[T]: boolean}--
The set.
Converts an array to a set, where each item is mapped to true. Duplicate items are discarded.
Aliases: fromList
local array = { "hello", "world", "hello" }
local set = FromArray(array) -- { hello = true, world = true }
map
Set.map(set: {[T]: boolean},--
The set to map.
mapper: (T,{[T]: boolean}) → U--
The mapper function.
) → {[U]: boolean}--
The mapped set.
Iterates over a set, calling a mapper function for each item.
local set = { hello = true, world = true }
local mappedSet = Map(set, function(value)
return value .. "!"
end) -- { ["hello!"] = true, ["world!"] = true }
intersection
Set.intersection(...: ...{[any]: boolean}--
The sets to intersect.
) → {[T]: boolean}--
The intersection of the sets.
Creates the intersection of multiple sets. The intersection is when both sets have a value in common. Unmatched values are discarded.
local set1 = { hello = true, world = true }
local set2 = { world = true, universe = true }
local intersection = Intersection(set1, set2) -- { world = true }
difference
Returns a set of values that are in the first set, but not in the other sets.
local set1 = { hello = true, world = true }
local set2 = { cat = true, dog = true, hello = true }
local difference = Difference(set1, set2) -- { world = true }
isSuperset
Set.isSuperset(superset: {[any]: boolean},--
The superset to check.
subset: {[any]: boolean}--
The subset to check against.
) → boolean--
Whether the superset is a superset of the subset.
Checks whether a set is a superset of another set.
local set = { hello = true, world = true }
local subset = { hello = true }
local isSuperset = IsSuperset(set, subset) -- true
merge
Set.merge(...: ...any--
The sets to merge.
) → {[T]: boolean}--
The merged set.
Combines one or more sets into a single set.
Aliases: join, union
local set1 = { hello = true, world = true }
local set2 = { cat = true, dog = true, hello = true }
local merge = Merge(set1, set2) -- { hello = true, world = true, cat = true, dog = true }
filter
Set.filter(set: {[T]: boolean},--
The set to filter.
predicate?: (item: T,set: {[T]: boolean}) → any--
The function to filter the set with.
) → {[T]: boolean}--
The filtered set.
Filters a set using a predicate. Any items that do not pass the predicate will be removed from the set.
local set = { hello = true, world = true }
local newSet = Filter(set, function(value)
return value ~= "hello"
end) -- { world = true }
isSubset
Set.isSubset(subset: {[any]: boolean},--
The subset to check.
superset: {[any]: boolean}--
The superset to check against.
) → boolean--
Whether the subset is a subset of the superset.
Checks whether a set is a subset of another set.
local set = { hello = true, world = true }
local subset = { hello = true }
local isSubset = IsSubset(subset, set) -- true
count
Set.count(set: {[T]: boolean},--
The set to count.
predicate?: (item: T,set: {[T]: boolean}) → boolean?--
The predicate to use to count.
) → number--
The number of items in the set.
Counts the number of items in a set.
local set = { hello = true, world = true }
local count = Count(set) -- 2
local count = Count(set, function(item)
return item == "hello"
end) -- 1
toArray
Set.toArray(set: {[T]: boolean}--
The set to convert to an array.
) → {T}--
The array.
Converts a set to an array.
local set = { hello = true, world = true }
local array = ToArray(set) -- { "hello", "world" }
delete
Set.delete(set: {[T]: boolean},--
The set to delete from.
...: ...T--
The values to delete.
) → {[T]: boolean}--
The set with the values deleted.
Deletes values from a set.
Aliases: subtract
local set = { hello = true, world = true }
local newSet = Delete(set, "hello") -- { world = true }
has
Set.has(set: {[T]: boolean},--
The set to check.
value: any--
The value to check for.
) → boolean--
Whether the value is in the set.
Checks whether a value is in a set.
local set = { hello = true }
local has = Has(set, "hello") -- true
add
Set.add(set: {[T]: boolean},--
The set to add the value to.
...: ...T--
The values to add.
) → {[T]: boolean}--
The set with the values added.
Adds values to a set.
local set = { hello = true }
local newSet = Add(set, "world") -- { hello = true, world = true }
differenceSymmetric
Returns a set of values that are in the first set, but not in the other sets, and vice versa.
local set1 = { hello = true, world = true }
local set2 = { cat = true, dog = true, hello = true }
local differenceSymmetric = DifferenceSymmetric(set1, set2) -- { world = true, cat = true, dog = true }