API/Set
Enumerable.prototype:ToSet(comparison_selector)
Make a new Set filled with the contents of the current Enumerable
Parameters
- comparison_selector
- optional: a function to generate a unique key per element
Return value
a Set
Set.FromArguments(comparison_selector, ...)
Construct and return a new Set
Parameters
- comparison_selector
- optional: a function to generate a unique key per element
- ...
- arguments to populate the set with
Set.FromSequenceOrItem(sequence_or_item)
Return a Set based on either a sequence or item passed in.
If a table is passed it, it will be wrapped rather than a new Set being made and copying all the data in.
Parameters
- sequence_or_item
- either a set-like table, a sequence, a Set, or another value which a Set will wrap around.
Return value
a Set
Usage
local set = Set.FromSequenceOrItem(nil) -- Set containing nil, not empty
local set = Set.FromSequenceOrItem("hey") -- Set containing "hey"
local set = Set.FromSequenceOrItem({ alpha = true, bravo = true }) -- Set containing "alpha" and "bravo"
local set = Set.FromSequenceOrItem(Enumerable.From({ 1, 2, 3 })) -- Set containing 1, 2, and 3
Set.New(sequence, comparison_selector)
Construct and return a new Set
Parameters
- sequence
- optional: The sequence to union the set with
- comparison_selector
- optional: a function to generate a unique key per element
Return value
a Set
Usage
local set = Set.New()
local set = Set.New({ 1, 2, 3, 2, 4 })
local set = Set.New(nil, tostring)
local set = Set.New({ "alpha", "Alpha", "ALPHA" }, string.upper)
Set.WrapTable(t)
Wrap a lua table such that there is parity between the passed-in table and the Set.
Unlike other tables, this will not gracefully handle nils.
Parameters
- t
- the table to wrap around
Return value
a Set
Usage
local set = Set.WrapTable({ [1] = true, [2] = true, [3] = true })
local set = Set.WrapTable({ hey = true, there = true })
Set.prototype:Add(item)
Add an element to the Set
Parameters
- item
- the element to add
Return value
whether the element was inserted properly, this is typically false if the item already exists.
Usage
local success = set:Add(5)
local success = set:Add(nil)
Set.prototype:AddMany(...)
Add multiple elements to the current Set
Parameters
- ...
- zero or more arguments to add
Usage
set:AddMany(1, 2, 3)
Set.prototype:Clear()
Clear all elements from the Set
Usage
set:Clear()
Set.prototype:Clone()
Make a shallow clone of the Set.
If the previous Set was read-only, the clone will not be.
Usage
local other = set:Clone()
Set.prototype:ConvertToReadOnly()
Make the Set read-only, preventing any further modifications.
There is no way to convert a Set back to being modifiable.
Return value
the same Set that was made read-only
Usage
set:ConvertToReadOnly()
Set.prototype:ExceptWith(sequence)
Removes all elements in the specified sequence from the current Set
Parameters
- sequence
- the sequence of items to remove from the current Set
Usage
set:ExceptWith({ 1, 2, 3 })
Set.prototype:GetEnumerator()
Get the enumerator for the Set
Return value
an Enumerator
Set.prototype:IntersectWith(sequence)
Modifies the current Set to contain only items which also exist in the provided sequence
Parameters
- sequence
- the sequence of items to include in the current Set
Usage
set:IntersectWith({ 1, 2, 3 })
Set.prototype:IsProperSubsetOf(other)
Determines whether the current Set is a proper subset of the provided sequence
Parameters
- other
- a sequence to compare against
Return value
a boolean
Set.prototype:IsProperSupersetOf(other)
Determines whether the current Set is a proper superset of the provided sequence
Parameters
- other
- a sequence to compare against
Return value
a boolean
Set.prototype:IsReadOnly()
Return whether the Set is read-only, and thus cannot have modifications made to it.
Return value
a boolean
Usage
local read_only = set:IsReadOnly()
Set.prototype:IsSubsetOf(other)
Determines whether the current Set is a subset of the provided sequence
Parameters
- other
- a sequence to compare against
Return value
a boolean
Set.prototype:IsSupersetOf(other)
Determines whether the current Set is a superset of the provided sequence
Parameters
- other
- a sequence to compare against
Return value
a boolean
Set.prototype:Overlaps(other)
Determines whether the current Set and a provided sequence share one or more common elements
Parameters
- other
- a sequence to compare against
Return value
a boolean
Set.prototype:PickAndRemoveRandom()
Randomly pick a random element in the Set, remove it, and return the value.
This will error if the Set is empty.
Return value
a random value in the Set
Usage
local value = set:PickAndRemoveRandom()
Set.prototype:Remove(item)
Remove an element from the Set
Parameters
- item
- item to try to remove from the Set
Return value
whether succesfully removed the element
Usage
local removed = set:Remove(1)
Set.prototype:RemoveWhere(predicate)
Remove all elements which match a given predicate
Parameters
- predicate
- a function which is passed each element and should return true to remove, false to keep
Return value
the number of elements removed
Usage
local num = set:RemoveWhere(function(x) return x % 2 == 0 end)
Set.prototype:SetContract(contract)
Set a contract that will be verified against any existing elements and any added elements or changed values.
This is handy for if you want to verify that all values are strings or something like that.
This will call :VerifyContract()
Parameters
- contract
- a function that is passed the element and should return whether the element is valid.
Usage
set:SetContract(function(v) return type(v) == "string" end)
Set.prototype:SetEquals(other)
Determines whether the current Set contains the same distinct elements as the provided collection
Parameters
- other
- a sequence to compare against
Return value
a boolean
Set.prototype:SymmetricExceptWith(sequence)
Modifies the current Set to only contain elements present in either that sequence or the other sequence, but not both.
Parameters
- sequence
- the sequence to compare to the current
Usage
set:SymmetricExceptWith({ 1, 2, 3 })
Set.prototype:ToTable(kind)
Convert the Set to a simple lua table, with all values being true.
This will be missing values if nil is used.
Parameters
- kind
- "set" or nil to return a table where all values are true or "list" to return a list-like table
Return value
a lua table
Usage
Set.New({ "hey", "there" }):ToTable()["hey"] == true
Set.New({ "hey", "there" }):ToTable('list')[1] == "hey"
Set.New({ "hey", "there" }):ToTable('set')["hey"] == true
Set.prototype:UnionWith(sequence)
Modify the current set to include the elements of the provided sequence
Parameters
- sequence
- the sequence to union against
Usage
set:UnionWith({ 1, 2, 3 })
set:UnionWith(Enumerable.RangeTo(1, 3))
Set.prototype:VerifyContract()
Verify that the contract for this List is valid for all elements in the List.
If there is no contract, this does nothing.
Usage
list:VerifyContract()
Comments