Array
The SD.Array
module provides utility functions for working with arrays in Lua. This document details each function's purpose, parameters, return values, and usage examples.
Array.IsArray
SD.Array.IsArray(arr)
SD.Array.IsArray(arr)
Purpose: Checks if the provided table is a sequential array without gaps.
Parameters:
arr
(table
): The table to check.
Returns:
(
boolean
): Returnstrue
if the table is an array,false
otherwise.
Usage Example:
local myTable = {1, 2, 3}
print(SD.Array.IsArray(myTable)) -- Output: true
Array.Append
SD.Array.Append(arr, value)
SD.Array.Append(arr, value)
Purpose: Appends an element to the end of the array.
Parameters:
arr
(table
): The array to append to.value
(any
): The value to append.
Returns: None.
Usage Example:
local myArray = {1, 2, 3}
SD.Array.Append(myArray, 4)
print(table.concat(myArray, ", ")) -- Output: 1, 2, 3, 4
Array.RemoveAt
SD.Array.RemoveAt(arr, index)
SD.Array.RemoveAt(arr, index)
Purpose: Removes an element from an array at the specified index.
Parameters:
arr
(table
): The array to remove from.index
(integer
): The index of the element to remove.
Returns: None.
Usage Example:
local myArray = {1, 2, 3, 4}
SD.Array.RemoveAt(myArray, 2)
print(table.concat(myArray, ", ")) -- Output: 1, 3, 4
Array.IndexOf
SD.Array.IndexOf(arr, value)
SD.Array.IndexOf(arr, value)
Purpose: Finds the first index of a value in an array.
Parameters:
arr
(table
): The array to search through.value
(any
): The value to find.
Returns:
(
integer
|nil
): The index of the value, ornil
if not found.
Usage Example:
local myArray = {"apple", "banana", "cherry"}
print(SD.Array.IndexOf(myArray, "banana")) -- Output: 2
Array.Reverse
SD.Array.Reverse(arr)
SD.Array.Reverse(arr)
Purpose: Reverses the elements of an array in place.
Parameters:
arr
(table
): The array to reverse.
Returns: None.
Usage Example:
local myArray = {1, 2, 3}
SD.Array.Reverse(myArray)
print(table.concat(myArray, ", ")) -- Output: 3, 2, 1
Array.Concatenate
SD.Array.Concatenate(arr1, arr2)
SD.Array.Concatenate(arr1, arr2)
Purpose: Concatenates two arrays into a new array.
Parameters:
arr1
(table
): The first array.arr2
(table
): The second array.
Returns:
(
table
): A new array containing elements from both arrays.
Usage Example:
local array1 = {1, 2}
local array2 = {3, 4}
local result = SD.Array.Concatenate(array1, array2)
print(table.concat(result, ", ")) -- Output: 1, 2, 3, 4
Array.Filter
SD.Array.Filter(arr, predicate)
SD.Array.Filter(arr, predicate)
Purpose: Filters an array based on a predicate function.
Parameters:
arr
(table
): The array to be filtered.predicate
(function
): The predicate function to determine if an element should be included. Takes an element as an argument.
Returns:
(
table
): A new array containing only elements that satisfy the predicate.
Usage Example:
luaCopy codelocal myArray = {1, 2, 3, 4, 5}
local evenOnly = SD.Array.Filter(myArray, function(v) return v % 2 == 0 end)
print(table.concat(evenOnly, ", ")) -- Output: 2, 4
Array.Map
SD.Array.Map(arr, transform)
SD.Array.Map(arr, transform)
Purpose: Maps an array to a new array based on a transformation function.
Parameters:
arr
(table
): The array to be mapped.transform
(function
): The transformation function applied to each element. Takes an element as an argument.
Returns:
(
table
): A new array containing the results of the transform function.
Usage Example:
local myArray = {1, 2, 3}
local squared = SD.Array.Map(myArray, function(v) return v * v end)
print(table.concat(squared, ", ")) -- Output: 1, 4, 9
Last updated