In this section, you will find multiple functions to work with sets. In Lua (or Luau), sets are defined as tables where keys all map to true values.
In your code, it is encouraged to put this part of the library into a variable:
local Set = Disk.SetIf you are not familiar with this kind of utility functions, don't get overwhelmed and start with these:
Returns the number of entries in the given set.
local result = Set.count({ a = true, b = true })
-- result is 2Related isEmpty
Creates a set from only elements that satisfy a condition.
local set = Set.fromArray({ 1, 2, 3, 4, 5, 6 })
local result = Set.filter(set, function(value)
return value % 2 == 0
end)
-- result is { 2 = true, 4 = true, 6 = true }Related removeValues
Creates a new Set from an array of values.
local result = Set.fromArray({ "a", "b", "c" })
-- result is { a = true, b = true, c = true }Related toArray
Returns true if a set has no entries.
local result = Set.isEmpty({ }) --> true
local result = Set.isEmpty({ key = true }) -- falseRelated count
Returns a new set containing only the values that are present in all given sets.
local set1 = Set.fromArray({ "a", "b", "c" })
local set2 = Set.fromArray({ "b", "c", "d" })
local result = Set.intersect(set1, set2)
-- result is { b = true, c = true }Related merge
Returns a new set where each value is converted with a mapping function. When the mapping function returns a nil value, the entry is removed.
local set = Set.fromArray({ 1, 2, 3 })
local result = Set.map(set, function(value)
return value * 2
end)
-- result is { 2 = true, 4 = true, 6 = true }Merge multiple sets into a single set.
local set1 = Set.fromArray({ "a", "b" })
local set2 = Set.fromArray({ "b", "c" })
local result = Set.merge(set1, set2)
-- result is { a = true, b = true, c = true }The function will skip nil values when merging.
Related intersect
Returns a new set with all the given values removed.
local set = Set.fromArray({ "a", "b", "c" })
local result = Set.removeValues(set, "b")
-- result is { a = true, c = true }Note: This function returns the same set if no values are given or if the values to remove are not in the set.
Related filter
Creates an array from a set.
local set = { a = true, b = true, c = true }
local result = Set.toArray(set)
-- result is { "a", "b", "c" } but the ordering is not guaranteedNote: Lua/Luau does not guarantee the iteration order of dictionary-like tables, so this function does not guarantee the ordering of the values in the resulting array.
Related fromArray