Extends Lua 5.2 table.
local table = require('__stdlib__/stdlib/utils/table')
Table.map(tbl, func[, ...]) | Given a mapping function, creates a transformed copy of the table by calling the function for each element in the table, and using the result as the new value for the key. |
Table.filter(tbl, func[, ...]) | Given a filter function, creates a filtered copy of the table by calling the function for each element in the table, and filtering out any key-value pairs for non-true results. |
Table.find(tbl, func[, ...]) | Given a candidate search function, iterates over the table, calling the function for each element in the table, and returns the first element the search function returned true. |
Table.any(tbl, func[, ...]) | Given a candidate search function, iterates over the table, calling the function for each element in the table, and returns true if search function returned true. |
Table.all(tbl, func[, ...]) | Given a candidate search function, iterates over the table, calling the function for each element in the table, and returns true if search function returned true for all items in the table. |
Table.each(tbl, func[, ...]) | Given a function, apply it to each element in the table. |
Table.flatten(tbl[, level]) | Returns a new array that is a one-dimensional recursive flattening of the given array. |
Table.first(tbl) | Given an array, returns the first element or nil if no element exists. |
Table.last(tbl) | Given an array, returns the last element or nil if no elements exist. |
Table.min(tbl) | Given an array of only numeric values, returns the minimum or nil if no element exists. |
Table.max(tbl) | Given an array of only numeric values, returns the maximum or nil if no element exists. |
Table.sum(tbl) | Given an array of only numeric values, return the sum of all values, or 0 for empty arrays. |
Table.avg(tbl) | Given an array of only numeric values, returns the average or nil if no element exists. |
Table.slice(tbl[, start=1][, stop=#tbl]) | Return a new array slice. |
Table.merge(tblA, tblB[, array_merge=false][, raw=false]) | Merges two tables, values from first get overwritten by the second. |
Table.dictionary_merge(tbl_a, tbl_b) | Creates a new merged dictionary, if the values in tbl_b are in tbl_a they are not overwritten. |
Table.deep_compare(t1, t2[, ignore_mt=false]) | Compares 2 tables for inner equality. |
Table.deep_copy(object) | Creates a deep copy of table without copying Factorio objects. |
Table.full_copy(object) | Creates a deep copy of a table without copying factorio objects internal table refs are also deepcopy. |
Table.flex_copy(object) | Creates a flexible deep copy of an object, recursively copying sub-objects |
Table.values(tbl[, sorted][, as_string]) | Returns a copy of all of the values in the table. |
Table.keys(tbl[, sorted][, as_string]) | Returns a copy of all of the keys in the table. |
Table.remove_keys(tbl, keys) | Removes keys from a table by setting the values associated with the keys to nil. |
Table.count_keys(tbl[, func[, ...]]) | Returns the number of keys in a table, if func is passed only count keys when the function is true. |
Table.invert(tbl) | Returns an inverted ({[value] = key,…}) copy of the given table. |
size(table) | Return the size of a table using the factorio built in table_size function |
Table.array_to_dictionary(tbl[, as_bool=false]) | For all string or number values in an array map them to a value = value table |
Table.is_empty(tbl) | Does the table contain any elements |
Table.clear(tbl) | Clear all elements in a table |
Given a mapping function, creates a transformed copy of the table by calling the function for each element in the table, and using the result as the new value for the key.
Passes the index as second argument to the function.
Parameters:a= { 1, 2, 3, 4, 5}
table.map(a, function(v) return v * 10 end) --produces: { 10, 20, 30, 40, 50 }
a = {1, 2, 3, 4, 5}
table.map(a, function(v, k, x) return v * k + x end, 100) --produces { 101, 104, 109, 116, 125}
Given a filter function, creates a filtered copy of the table by calling the function for each element in the table, and filtering out any key-value pairs for non-true results.
Passes the index as second argument to the function.
Parameters:a= { 1, 2, 3, 4, 5}
table.filter(a, function(v) return v % 2 == 0 end) --produces: { 2, 4 }
a = {1, 2, 3, 4, 5}
table.filter(a, function(v, k, x) return k % 2 == 1 end) --produces: { 1, 3, 5 }
Given a candidate search function, iterates over the table, calling the function for each element in the table, and returns the first element the search function returned true.
Passes the index as second argument to the function.
Parameters:a= { 1, 2, 3, 4, 5}
table.find(a, function(v) return v % 2 == 0 end) --produces: 2
a = {1, 2, 3, 4, 5}
table.find(a, function(v, k, x) return k % 2 == 1 end) --produces: 1
Given a candidate search function, iterates over the table, calling the function for each element in the table, and returns true if search function returned true.
Passes the index as second argument to the function.
Parameters:a= { 1, 2, 3, 4, 5}
table.any(a, function(v) return v % 2 == 0 end) --produces: true
a = {1, 2, 3, 4, 5}
table.any(a, function(v, k, x) return k % 2 == 1 end) --produces: true
Given a candidate search function, iterates over the table, calling the function for each element in the table, and returns true if search function returned true for all items in the table.
Passes the index as second argument to the function.
Parameters:Given a function, apply it to each element in the table.
Passes the index as the second argument to the function.
Iteration is aborted if the applied function returns true for any element during iteration.
Parameters:a = {10, 20, 30, 40}
table.each(a, function(v) game.print(v) end) --prints 10, 20, 30, 40, 50
Returns a new array that is a one-dimensional recursive flattening of the given array.
For every element that is an array, extract its elements into the new array.
The optional level argument determines the level of recursion to flatten. > This function flattens an integer-indexed array, but not an associative array.
Parameters:Given an array, returns the first element or nil if no element exists.
Parameters:
Given an array, returns the last element or nil if no elements exist.
Parameters:
Given an array of only numeric values, returns the minimum or nil if no element exists.
Parameters:
Given an array of only numeric values, returns the maximum or nil if no element exists.
Parameters:
Given an array of only numeric values, return the sum of all values, or 0 for empty arrays.
Parameters:
Given an array of only numeric values, returns the average or nil if no element exists.
Parameters:
Return a new array slice.
Parameters:
local tab = { 10, 20, 30, 40, 50}
slice(tab, 2, -2) --returns { 20, 30, 40 }
Merges two tables, values from first get overwritten by the second.
Parameters:
function some_func(x, y, args)
args = table.merge({option1=false}, args)
if opts.option1 == true then return x else return y end
end
some_func(1,2) -- returns 2
some_func(1,2,{option1=true}) -- returns 1
Creates a new merged dictionary, if the values in tbl_b are in tbl_a they are not overwritten.
Parameters: Returns:
local a = {one = A}
local b = {one = Z, two = B}
local merged = table.dictionary_merge(tbl_a, tbl_b)
--merged = {one = A, two = B}
Compares 2 tables for inner equality.
Modified from factorio/data/core/lualib/util.lua
Parameters: Returns:Creates a deep copy of table without copying Factorio objects.
copied from factorio/data/core/lualib/util.lua
Parameters:local copy = table.deep_copy[data.raw.["stone-furnace"]["stone-furnace"]]
-- returns a copy of the stone furnace entity
Creates a deep copy of a table without copying factorio objects internal table refs are also deepcopy.
The resulting table should
Parameters:local copy = table.fullcopy[data.raw.["stone-furnace"]["stone-furnace"]]
-- returns a deepcopy of the stone furnace entity with no internal table references.
Creates a flexible deep copy of an object, recursively copying sub-objects
Parameters:
local copy = table.flexcopy(data.raw.["stone-furnace"]["stone-furnace"])
-- returns a copy of the stone furnace entity
Returns a copy of all of the values in the table.
Parameters:
Returns a copy of all of the keys in the table.
Parameters:
Removes keys from a table by setting the values associated with the keys to nil.
Parameters:
local a = {1, 2, 3, 4}
table.remove_keys(a, {1,3}) --returns {nil, 2, nil, 4}
local b = {k1 = 1, k2 = 'foo', old_key = 'bar'}
table.remove_keys(b, {'old_key'}) --returns {k1 = 1, k2 = 'foo'}
Returns the number of keys in a table, if func is passed only count keys when the function is true.
Parameters:
local a = { 1, 2, 3, 4, 5}
table.count_keys(a) -- produces: 5, 5
local a = {1, 2, 3, 4, 5}
table.count_keys(a, function(v, k) return k % 2 == 1 end) -- produces: 3, 5
Returns an inverted ({[value] = key,…}) copy of the given table.
If the values are not unique, the assigned key depends on the order of pairs().
Parameters:local a = {k1 = 'foo', k2 = 'bar'}
table.invert(a) --returns {'foo' = k1, 'bar' = k2}
local b = {k1 = 'foo', k2 = 'bar', k3 = 'bar'}
table.invert(b) --returns {'foo' = k1, 'bar' = ?}
Return the size of a table using the factorio built in table_size function
Parameters:
For all string or number values in an array map them to a value = value table
Parameters:
local a = {"v1", "v2"}
table.array_to_bool(a) -- return {["v1"] = "v1", ["v2"]= "v2"}