Kryzeth Standard Library

utils.table

Methods47

function Table.remove<V>(list: {integer, V} | V[], pos: nil | integer) -> V
function Table.sort<V>(list: V[], comp: fun(a: V, b: V) -> boolean | nil)
function Table.pack<T>(...: ...T) -> (...T) & { n: integer }
function Table.unpack<T, Start, End>(list: T, i: Start | nil, j: End | nil) -> unpack<T,Start,End>
function Table.insert(list: table, pos: integer, value: any)
Overloads
function Table.insert(list: table, value: any) -> nil
function Table.concat(list: table, sep: nil | string, i: nil | integer, j: nil | integer) -> string
function Table.map(tbl: table, func: function, ...: any) -> table

Given a mapping function, creates a transformed copy of the table by calling the function for each element and using the result as the new value for the key. Passes the index as the second argument to the function.

@usage local a = {1, 2, 3, 4, 5} table.map(a, function(v) return v * 10 end) -- {10, 20, 30, 40, 50} @usage local a = {1, 2, 3, 4, 5} table.map(a, function(v, k, x) return v * k + x end, 100) -- {101, 104, 109, 116, 125}

function Table.filter(tbl: table, func: function, ...: any) -> table

Given a filter function, creates a filtered copy of the table by calling the function for each element and removing entries for which it returns false. Passes the index as the second argument to the function.

@usage local a = {1, 2, 3, 4, 5} table.filter(a, function(v) return v % 2 == 0 end) -- {2, 4} @usage local a = {1, 2, 3, 4, 5} table.filter(a, function(v, k) return k % 2 == 1 end) -- {1, 3, 5}

function Table.find(tbl: table, func: function, ...: any) -> (value any, key any)

Searches a table and returns the first value for which the search function returns true. Passes the index as the second argument to the function.

Parameters
tbltable
funcfunction
...any
Returns
valueany

The first matching value.

keyany

The key of the first matching value.

@usage local a = {1, 2, 3, 4, 5} table.find(a, function(v) return v % 2 == 0 end) -- 2, 2 @usage local a = {1, 2, 3, 4, 5} table.find(a, function(v, k) return k % 2 == 1 end) -- 1, 1

function Table.any(tbl: table, func: function, ...: any) -> boolean

Returns true if the search function returns true for any element in the table. Passes the index as the second argument to the function.

See:

StdLib.Utils.Table.find

@usage local a = {1, 2, 3, 4, 5} table.any(a, function(v) return v % 2 == 0 end) -- true @usage local a = {1, 2, 3, 4, 5} table.any(a, function(v, k) return k % 2 == 1 end) -- true

function Table.all(tbl: table, func: function, ...: any) -> boolean

Returns true if the search function returns true for every element in the table. Passes the index as the second argument to the function.

function Table.each(tbl: table, func: function, ...: any) -> tbl table

Applies a function to each element in the table. Passes the index as the second argument to the function. Iteration stops if the function returns true for any element.

Parameters
tbltable
funcfunction
...any
Returns
tbltable

The original table.

@usage local a = {10, 20, 30, 40} table.each(a, function(v) game.print(v) end) -- prints 10, 20, 30, 40

function Table.contains(tbl: any[], value: any) -> boolean

Returns true if the unkeyed array contains the given value.

Parameters
tblany[]

The array to search.

valueany

The value to find.

Returns
boolean
function Table.flatten(tbl: any[], level: uint | nil) -> flattened any[]

Returns a recursively flattened copy of an array. Nested arrays are expanded into the resulting one-dimensional array. If level is supplied, recursion is limited to that many levels. Only integer-indexed arrays are flattened; associative tables are preserved.

Parameters
tblany[]

The array to flatten.

leveluint | nil

Maximum recursion depth.

Returns
flattenedany[]
function Table.first(tbl: any[]) -> value any

Returns the first element of an array, or nil if the array is empty.

function Table.last(tbl: any[]) -> value any

Returns the last element of an array, or nil if the array is empty.

function Table.min(tbl: number[]) -> minimum nil | number

Returns the smallest numeric value in an array, or nil if the array is empty.

function Table.max(tbl: number[]) -> maximum nil | number

Returns the largest numeric value in an array, or nil if the array is empty.

function Table.sum(tbl: number[]) -> sum number

Returns the sum of all numeric values in an array. Returns 0 for an empty array.

function Table.avg(tbl: number[]) -> average nil | number

Returns the average of all numeric values in an array, or nil if the array is empty.

function Table.slice(tbl: any[], start: nil | integer, stop: nil | integer) -> slice any[]

Returns a new array containing a slice of the given array.

Parameters
tblany[]

The array to slice.

startnil | integer

Starting index. Defaults to 1.

stopnil | integer

Ending index. Negative values count backwards from the end.

Returns
sliceany[]

@usage local a = {10, 20, 30, 40, 50} table.slice(a, 2, -2) -- {20, 30, 40}

function Table.merge(tblA: table, tblB: nil | table, array_merge: nil | boolean, raw: nil | boolean) -> tblA table

Merges two tables, with values from the second table overwriting values from the first. When array_merge is true, values from the second table are appended instead.

Parameters
tblAtable

The destination table.

tblBnil | table

The table to merge into tblA.

array_mergenil | boolean

Append values instead of merging by key.

rawnil | boolean

Use rawset when merging associative tables.

Returns
tblAtable

The merged destination table.

@usage local args = table.merge({option1 = false}, {option1 = true})

function Table.array_combine(...: table) -> combined any[]

Combines the values from multiple arrays into a new array.

Parameters
...table

Arrays to combine.

Returns
combinedany[]
function Table.dictionary_combine(...: table) -> combined table

Combines multiple dictionaries into a new table. Later tables overwrite values from earlier tables with the same key.

Parameters
...table

Dictionaries to combine.

Returns
combinedtable
function Table.dictionary_merge(tbl_a: table, tbl_b: table) -> merged table

Creates a merged dictionary without overwriting values already present in the first table.

@usage local a = {one = "A"} local b = {one = "Z", two = "B"} local merged = table.dictionary_merge(a, b) -- {one = "A", two = "B"}

function Table.deep_compare(t1: any, t2: any, ignore_mt: nil | boolean) -> boolean

Recursively compares two values for equality. Table contents are compared recursively. Based on Factorio's util.lua implementation and work by Sparr, Nexela, and luacode.org.

Parameters
t1any
t2any
ignore_mtnil | boolean

Ignore the __eq metamethod.

Returns
boolean
function Table.compare(t1: any, t2: any, ignore_mt: nil | boolean) -> boolean
Parameters
t1any
t2any
ignore_mtnil | boolean

Ignore the __eq metamethod.

Returns
boolean
function Table.deep_copy<T>(object: T) -> copy T

Creates a deep copy of a value without copying Factorio objects.

Parameters
objectT

The value to copy.

Returns
copyT

@usage local copy = table.deep_copy(data.raw["stone-furnace"]["stone-furnace"])

function Table.deepcopy<T>(object: T) -> copy T
Parameters
objectT

The value to copy.

Returns
copyT
function Table.full_copy<T>(object: T) -> copy T

Creates a deep copy without preserving shared internal table references. Repeated references to the same nested table are copied independently.

Parameters
objectT

The value to copy.

Returns
copyT

@usage local copy = table.full_copy(data.raw["stone-furnace"]["stone-furnace"])

function Table.fullcopy<T>(object: T) -> copy T
Parameters
objectT

The value to copy.

Returns
copyT
function Table.flex_copy<T>(object: T) -> copy T

Creates a flexible deep copy of a value. Tables implementing _copy_with may provide their own copy behavior.

Parameters
objectT

The value to copy.

Returns
copyT

@usage local copy = table.flex_copy(data.raw["stone-furnace"]["stone-furnace"])

function Table.flexcopy<T>(object: T) -> copy T
Parameters
objectT

The value to copy.

Returns
copyT
function Table.values(tbl: nil | table, sorted: nil | boolean, as_string: nil | boolean) -> values any[]

Returns an array containing all values from a table.

Parameters
tblnil | table

The table whose values will be copied.

sortednil | boolean

Sort the resulting array.

as_stringnil | boolean

Convert values to strings.

Returns
valuesany[]
function Table.keys(tbl: nil | table, sorted: nil | boolean, as_string: nil | boolean) -> keys any[]

Returns an array containing all keys from a table.

Parameters
tblnil | table

The table whose keys will be copied.

sortednil | boolean

Sort the resulting array.

as_stringnil | boolean

Convert keys to strings.

Returns
keysany[]
function Table.remove_keys(tbl: table, keys: any[]) -> tbl table

Removes the specified keys from a table.

Parameters
tbltable

The table to modify.

keysany[]

Keys to remove.

Returns
tbltable

The modified table.

@usage local a = {1, 2, 3, 4} table.remove_keys(a, {1, 3}) -- {nil, 2, nil, 4} @usage local b = {k1 = 1, k2 = "foo", old_key = "bar"} table.removekeys(b, {"oldkey"}) -- {k1 = 1, k2 = "foo"}

function Table.count_keys(tbl: table, func: nil | function, ...: any) -> (count integer, total integer)

Counts the keys in a table. If a filter function is supplied, also returns the number of entries for which it returns true.

Parameters
tbltable
funcnil | function

Optional filter function.

...any

Additional arguments passed to func.

Returns
countinteger

Number of matching keys.

totalinteger

Total number of keys.

@usage local a = {1, 2, 3, 4, 5} table.count_keys(a) -- 5, 5 @usage local a = {1, 2, 3, 4, 5} table.count_keys(a, function(v, k) return k % 2 == 1 end) -- 3, 5

function Table.invert(tbl: table) -> inverted table

Returns a table with its keys and values exchanged. If the original values are not unique, which key is retained depends on iteration order.

@usage local a = {k1 = "foo", k2 = "bar"} table.invert(a) -- {foo = "k1", bar = "k2"}

function Table.size(tbl: table) -> integer

Returns the number of entries in a table, using Factorio's table_size when available.

function Table.array_to_dictionary(tbl: table, as_bool: nil | boolean) -> table

Converts an array into a dictionary. String and numeric array values become keys whose values are either the original value or true. If the input is already a dictionary, it is returned unchanged.

Parameters
tbltable
as_boolnil | boolean

Map each key to true instead of its original value.

Returns
table

@usage local a = {"v1", "v2"} table.arraytodictionary(a) -- {v1 = "v1", v2 = "v2"} @usage local a = {"v1", "v2"} table.arraytodictionary(a, true) -- {v1 = true, v2 = true}

function Table.unique_values(tbl: table) -> values any[]

Returns an array containing the unique values from a table.

function Table.is_empty(tbl: table) -> boolean

Returns true if the table contains no entries.

function Table.clear(tbl: table) -> tbl table

Removes all entries from a table.

Parameters
tbltable

The table to clear.

Returns
tbltable

The cleared table.

function Table.insert_string(tbl: string[], before: string, value: string) -> tbl string[]

Inserts a string before the first occurrence of another string. If before is not found, the new value is appended to the array.

Parameters
tblstring[]

The array to modify.

beforestring

The value before which the new string will be inserted.

valuestring

The string to insert.

Returns
tblstring[]

The modified array.

function Table.remove_string(tbl: string[], target: string) -> tbl string[]

Removes the first occurrence of a string from an array. If the string is not found, the array is returned unchanged.

Parameters
tblstring[]

The array to modify.

targetstring

The string to remove.

Returns
tblstring[]

The modified array.

function Table.scale(object: table | number, scale: number) -> scaled table | number

Scales a number or all numeric values contained in a table. Tables are deep-copied before their numeric values are scaled recursively.

Parameters
objecttable | number

The number or table to scale.

scalenumber

The scale factor.

Returns
scaledtable | number
function Table.is_array(tbl: any) -> boolean

Returns true if the value is a simple array with sequential integer keys starting at 1. For example, {"a", "b", "c"} is valid, while {value1 = "a"} or {[1] = "a", [3] = "b"} are not.