Module extensions.table

Extensions to the Lua standard table library.

Functions

copy (tbl) Returns a new table with a single layer of keys-values copied.
deepcopy (tbl) Returns a new table with a all layers of keys-values copied.
count (tbl) Returns the number of keys in the table.
map (tbl, fn) Returns a new table with new key-value pairs sourced from the map function.
filter (tbl, fn) Returns a new table with only key-value pairs that cause fn to return true.
reduce (tbl, fn, initial) Iterates over the array portion of a table in order, calling fn on each value, passing in the return value from the previous element.
reverse (tbl, dest) Reverses the contents of the array.
shift (tbl, index, count) Shifts every index after index in the table to the right by count.
slice (tbl[, start[, stop[, step]]]) Returns a slice of the table, works similarly to string.sub except on a table.
join (a, b) Appends all elements of b into a.
merge (a, b) Merges all key-value pairs of b into a.
move (src, first, stop, src_offset, dest) This works exactly like table.move introduced in Lua 5.3.
swapremove (tbl, i) Removes the value at the index from an array and replaces it with the item at the end of the array.
search (tbl, value) Looks for a specific value in a table and returns the key it was first found at.
keys (tbl) Returns an array of keys available in the table.
values (tbl) Returns an array of values available in the table.
makeset (tbl) Takes all the values in a table and creates a new table where keys are the taken values and values are all true.
isempty (tbl) Returns whether or not the table is empty.
shuffle (tbl) Shuffles a table in place.
randomipair (tbl) Returns a random key, value index from an array.
randomvalue (tbl) Returns a random key, value index from an array.
randompair (tbl) Returns a random key, value index from a table.
fill (tbl, value, len, start) Fills a table from start to start + len with value.


Functions

copy (tbl) line 14
Returns a new table with a single layer of keys-values copied.

Parameters:

  • tbl table

Returns:

    table
deepcopy (tbl) line 28
Returns a new table with a all layers of keys-values copied. Tables are copied recursively.

Parameters:

  • tbl table

Returns:

    table
count (tbl) line 41
Returns the number of keys in the table.

Parameters:

  • tbl table

Returns:

    number
map (tbl, fn) line 57
Returns a new table with new key-value pairs sourced from the map function. The map function has a signature of: fn(value, key) new_value, new_key? If new_key is omitted, the key will remain the same.

Parameters:

  • tbl table
  • fn function

Returns:

    table
filter (tbl, fn) line 78
Returns a new table with only key-value pairs that cause fn to return true. The filter function has a signature of: fn(value, key) boolean

Parameters:

  • tbl table
  • fn function

Returns:

    table
reduce (tbl, fn, initial) line 99
Iterates over the array portion of a table in order, calling fn on each value, passing in the return value from the previous element. If initial is not provided, the first element of the array is used, and the iteration starts at the second element. The reduce function has a signature of: fn(previous, value, index?) boolean

Parameters:

  • tbl table
  • fn fun(previous: any, value: any, index?: integer): boolean
  • initial any

Returns:

    any
reverse (tbl, dest) line 118
Reverses the contents of the array. If dest is provided, the source array will not be modified

Parameters:

  • tbl table
  • dest ? table

Returns:

    table
shift (tbl, index, count) line 141
Shifts every index after index in the table to the right by count.

Parameters:

  • tbl table
  • index number
  • count number
slice (tbl[, start[, stop[, step]]]) line 164
Returns a slice of the table, works similarly to string.sub except on a table.

Parameters:

  • tbl table
  • start number (optional)
  • stop number (optional)
  • step number (optional)

Returns:

    table
join (a, b) line 178
Appends all elements of b into a. Does not consider non-array elements.

Parameters:

  • a table
  • b table

Returns:

    table
merge (a, b) line 190
Merges all key-value pairs of b into a. Will overwrite any existing keys.

Parameters:

  • a table
  • b table

Returns:

    table
move (src, first, stop, src_offset, dest) line 205
This works exactly like table.move introduced in Lua 5.3.

Parameters:

  • src table
  • first integer
  • stop integer
  • src_offset integer
  • dest table
swapremove (tbl, i) line 220
Removes the value at the index from an array and replaces it with the item at the end of the array.

Parameters:

  • tbl table
  • i number

Returns:

    any
search (tbl, value) line 233
Looks for a specific value in a table and returns the key it was first found at.

Parameters:

  • tbl table
  • value any

Returns:

    any|nil
keys (tbl) line 246
Returns an array of keys available in the table.

Parameters:

  • tbl table

Returns:

    table
values (tbl) line 259
Returns an array of values available in the table.

Parameters:

  • tbl table

Returns:

    table
makeset (tbl) line 273
Takes all the values in a table and creates a new table where keys are the taken values and values are all true.

Parameters:

  • tbl table

Returns:

    table
isempty (tbl) line 286
Returns whether or not the table is empty.

Parameters:

  • tbl table

Returns:

    boolean
shuffle (tbl) line 293
Shuffles a table in place.

Parameters:

  • tbl table

Returns:

    table
randomipair (tbl) line 305
Returns a random key, value index from an array.

Parameters:

  • tbl table

Returns:

    any, any
randomvalue (tbl) line 313
Returns a random key, value index from an array.

Parameters:

  • tbl table

Returns:

    any, any
randompair (tbl) line 321
Returns a random key, value index from a table.

Parameters:

  • tbl table

Returns:

    any, any
fill (tbl, value, len, start) line 338
Fills a table from start to start + len with value.

Parameters:

  • tbl table
  • value any
  • len ? integer
  • start ? integer

Returns:

    table