API/List
Enumerable.prototype:ShuffleInPlace()
Randomizes the elements of the List in-place, thus changes the contents of the list.
Usage
list:ShuffleInPlace()
Enumerable.prototype:ToList()
Make a new List filled with the contents of the current Enumerable
Return value
a List
List.FromArguments(...)
Construct and return a new list based on the arguments provided
Parameters
- ...
- a tuple of arguments to fill the list with
Return value
a List
Usage
local list = List.FromArguments()
local list = List.FromArguments(1, 2, 3)
local list = List.FromArguments(nil, nil, 5)
List.New(sequence)
Construct and return a new list
Parameters
- sequence
- optional: The sequence to fill the list with
Return value
a List
Usage
local list = List.New()
local list = List.New({ 1, 2, 3 })
local list = List.New(Enumerable.RangeTo(1, 10))
List.WrapTable(t)
Wrap a lua table such that there is parity between the passed-in table and the List.
Unlike other tables, this will not gracefully handle nils.
Parameters
- t
- the table to wrap around
Return value
a List
Usage
local list = List.WrapTable({ 1, 2, 3 })
List.prototype:Add(item)
Add an element to the List
Parameters
- item
- the element to add
Usage
list:Add(5)
list:Add(nil)
List.prototype:AddMany(...)
Add multiple elements to the current List
Parameters
- ...
- zero or more arguments to add
Usage
list:AddMany(1, 2, 3)
List.prototype:AddRange(sequence)
Add a sequence of elements to the current List
Parameters
- sequence
- a sequence of elements to add
Usage
list:AddRange(Enumerable.RangeTo(1, 10))
list:AddRange({ 1, 2, 3 })
List.prototype:Clear()
Clear all elements from the List
Usage
list:Clear()
List.prototype:Clone()
Make a shallow clone of the List.
If the previous List was read-only, the clone will not be.
Usage
local other = list:Clone()
List.prototype:ConvertToReadOnly()
Make the List read-only, preventing any further modifications.
There is no way to convert a List back to being modifiable.
Return value
the same List that was made read-only
Usage
list:ConvertToReadOnly()
List.prototype:GetEnumerator()
Return an Enumerator for the current List
Return value
an Enumerator
List.prototype:Insert(index, item)
Insert an element into a position in the list.
Parameters
- index
- the position of the element. Cannot be less than 1 or greater than count+1
- item
- the element to insert
Usage
list:Insert(1, "hey")
List.prototype:InsertMany(index, ...)
Insert multiple arguments into a position in the list.
Parameters
- index
- the position of the sequence. Cannot be less than 1 or greater than count+1
- ...
- zero or more arguments to add
Usage
list:InsertMany(1, "hey", "there")
list:InsertMany(1, 1, 2, 3, 4, 5)
List.prototype:InsertRange(index, sequence)
Insert a sequence into a position in the list.
Parameters
- index
- the position of the sequence. Cannot be less than 1 or greater than count+1
- sequence
- the sequence to insert
Usage
list:InsertRange(1, { "hey", "there" })
list:InsertRange(1, Enumerable.RangeTo(1, 5))
List.prototype:IsReadOnly()
Return whether the List is read-only, and thus cannot have modifications made to it.
Return value
a boolean
Usage
local read_only = list:IsReadOnly()
List.prototype:PickAndRemoveRandom()
Randomly pick a random element in the List, remove it, and return the value.
This will error if the List is empty.
Return value
a random value in the List
Usage
local value = list:PickAndRemoveRandom()
List.prototype:Remove(item)
Remove a given item from the list
Parameters
- item
- the item to remove
Return value
whether the item was successfully removed
Usage
local removed = list:Remove(5)
List.prototype:RemoveAt(index)
Remove an item from the list at the given index.
Parameters
- index
- the 1-based index to remove
Usage
list:RemoveAt(1)
List.prototype:RemoveRange(index, count)
Remove multiple elements from the List
Parameters
- index
- the starting 1-based index to remove
- count
- the amount of elements to remove
List.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 = list:RemoveWhere(function(x) return x % 2 == 0 end)
List.prototype:ReplaceAt(index, item)
Replace an element at the given index.
Parameters
- index
- the 1-based index to replace. Must be at least 1 and no greater than the count+1
- item
- the item to add
Usage
List.New({ 1, 2 }):ReplaceAt(1, 3) -- [3, 2]
List.New({ 1, 2 }):ReplaceAt(3, 2) -- [1, 2, 3]
List.prototype:ReplaceMany(index, ...)
Replace a sequence of elements at the given index.
Parameters
- index
- the 1-based index to replace. Must be at least 1 and no greater than the count+1
- ...
- zero or more arguments to add
Usage
List.New({ 1, 2 }):ReplaceMany(1, 3, 4) -- [3, 4]
List.New({ 1, 2 }):ReplaceMany(2, 3, 4) -- [1, 3, 4]
List.New({ 1, 2 }):ReplaceMany(3, 3, 4) -- [1, 2, 3, 4]
List.prototype:ReplaceRange(index, sequence)
Replace a sequence of elements at the given index.
Parameters
- index
- the 1-based index to replace. Must be at least 1 and no greater than the count+1
- sequence
- the sequence to add
Usage
List.New({ 1, 2 }):ReplaceRange(1, { 3, 4 }) -- [3, 4]
List.New({ 1, 2 }):ReplaceRange(2, { 3, 4 }) -- [1, 3, 4]
List.New({ 1, 2 }):ReplaceRange(3, { 3, 4 }) -- [1, 2, 3, 4]
List.prototype:ReverseInPlace()
Reverse the current List in-place, thus changing the contents of the list.
Usage
list:ReverseInPlace()
List.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
list:SetContract(function(v) return type(v) == "string" end)
List.prototype:Sort(comparer)
Sort the current List in-place based on the given comparison.
Parameters
- comparer
- a function which takes two elements and returns -1 if the first is less than the second, 1 for the opposite, and 0 if equivalent.
Usage
list:Sort(function(a, b) if #a < #b then return -1 elseif #a > #b then return 1 elseif a < b then return -1 elseif a > b then return 1 else return 0 end end)
List.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