Table
The SD.Table
module provides utility functions for working with tables in Lua. This document details each function's purpose, parameters, return values, and usage examples.
Table.Contains
SD.Table.Contains(tbl, value)
SD.Table.Contains(tbl, value)
Purpose: Checks if a specific value exists within a table.
Parameters:
tbl
(table
): The table to search through.value
(any
): The value to search for.
Returns:
(
boolean
): Returnstrue
if the value exists in the table,false
otherwise.
Usage Example:
local myTable = {a = 1, b = 2, c = 3}
print(SD.Table.Contains(myTable, 2)) -- Output: true
Table.RemoveValue
SD.Table.RemoveValue(tbl, value)
SD.Table.RemoveValue(tbl, value)
Purpose: Removes an occurrence of a specified value from the table.
Parameters:
tbl
(table
): The table from which the value is to be removed.value
(any
): The value to remove.
Returns: None.
Usage Example:
local myTable = {1, 2, 3, 4}
SD.Table.RemoveValue(myTable, 3)
print(table.concat(myTable, ", ")) -- Output: 1, 2, 4
Table.IndexOf
SD.Table.IndexOf(tbl, object)
SD.Table.IndexOf(tbl, object)
Purpose: Returns the index of a given value in the table, if present.
Parameters:
tbl
(table
): The table to search through.object
(any
): The value whose index is to be found.
Returns:
(
integer
|nil
): The index of the value, ornil
if not found.
Usage Example:
local myTable = {10, 20, 30, 40}
print(SD.Table.IndexOf(myTable, 30)) -- Output: 3
Table.AddUnique
SD.Table.AddUnique(tbl, value)
SD.Table.AddUnique(tbl, value)
Purpose: Adds a value to the table if it is not already present.
Parameters:
tbl
(table
): The table to add the value to.value
(any
): The value to add if it is unique.
Returns: None.
Usage Example:
local myTable = {1, 2, 3}
SD.Table.AddUnique(myTable, 4)
SD.Table.AddUnique(myTable, 2)
print(table.concat(myTable, ", ")) -- Output: 1, 2, 3, 4
Table.MergeTables
SD.Table.MergeTables(tbl1, tbl2)
SD.Table.MergeTables(tbl1, tbl2)
Purpose: Merges the contents of the second table into the first table.
Parameters:
tbl1
(table
): The first table to merge into.tbl2
(table
): The second table whose values will be added to the first.
Returns: None.
Usage Example:
local table1 = {1, 2, 3}
local table2 = {4, 5}
SD.Table.MergeTables(table1, table2)
print(table.concat(table1, ", ")) -- Output: 1, 2, 3, 4, 5
Table.Filter
SD.Table.Filter(tbl, predicate)
SD.Table.Filter(tbl, predicate)
Purpose: Filters a table based on a predicate function, producing a new table.
Parameters:
tbl
(table
): The table to be filtered.predicate
(function
): The function to determine if an element should be included. It takes an element and its key as arguments.
Returns:
(
table
): A new table containing only elements that satisfy the predicate.
Usage Example:
local myTable = {1, 2, 3, 4, 5}
local even = SD.Table.Filter(myTable, function(v) return v % 2 == 0 end)
print(table.concat(even, ", ")) -- Output: 2, 4
Table.Map
SD.Table.Map(tbl, transform)
SD.Table.Map(tbl, transform)
Purpose: Creates a new table by applying a transformation function to each element of the original table.
Parameters:
tbl
(table
): The table to be mapped.transform
(function
): The transformation function applied to each element. It takes an element and its key as arguments.
Returns:
(
table
): A new table containing the results of the transform function applied to each element.
Usage Example:
local myTable = {1, 2, 3}
local squared = SD.Table.Map(myTable, function(v) return v * v end)
print(table.concat(squared, ", ")) -- Output: 1, 4, 9
Last updated