Merge remote-tracking branch 'origin/master'

This commit is contained in:
Max Cahill 2020-05-13 21:09:22 +10:00
commit 93e6073128

View File

@ -209,7 +209,7 @@ else
--useful when multiple references are being held --useful when multiple references are being held
--so you cannot just create a new table --so you cannot just create a new table
function tablex.clear(t) function tablex.clear(t)
assert(type(t) == "table", "table.clear - argument 't' must be a table") assert(type(t) == "table", "tablex.clear - argument 't' must be a table")
local k = next(t) local k = next(t)
while k ~= nil do while k ~= nil do
t[k] = nil t[k] = nil
@ -240,7 +240,7 @@ end
-- but doesn't clear anything out -- but doesn't clear anything out
-- (useful for deep overlays and avoiding garbage) -- (useful for deep overlays and avoiding garbage)
function tablex.copy(t, deep_or_into) function tablex.copy(t, deep_or_into)
assert(type(t) == "table", "table.copy - argument 't' must be a table") assert(type(t) == "table", "tablex.copy - argument 't' must be a table")
local is_bool = type(deep_or_into) == "boolean" local is_bool = type(deep_or_into) == "boolean"
local is_table = type(deep_or_into) == "table" local is_table = type(deep_or_into) == "table"
@ -259,14 +259,20 @@ function tablex.copy(t, deep_or_into)
return into return into
end end
--overlay one table directly onto another, shallow only --overlay tables directly onto one another, shallow only
function tablex.overlay(to, from) --takes as many tables as required,
assert(type(to) == "table", "table.overlay - argument 'to' must be a table") --overlays them in passed order onto the first,
assert(type(from) == "table", "table.overlay - argument 'from' must be a table") --and returns the first table with the overlay(s) applied
for k,v in pairs(from) do function tablex.overlay(a, b, ...)
to[k] = v assert(type(a) == "table", "tablex.overlay - argument 'a' must be a table")
assert(type(b) == "table", "tablex.overlay - argument 'b' must be a table")
for k,v in pairs(b) do
a[k] = v
end end
return to if ... then
return tablex.overlay(a, ...)
end
return a
end end
--collapse the first level of a table into a new table of reduced dimensionality --collapse the first level of a table into a new table of reduced dimensionality
@ -278,7 +284,7 @@ end
-- so they can't exist in the base level -- so they can't exist in the base level
-- (... or at least, their non-ipairs members won't survive the collapse) -- (... or at least, their non-ipairs members won't survive the collapse)
function tablex.collapse(t) function tablex.collapse(t)
assert(type(t) == "table", "table.collapse - argument 't' must be a table") assert(type(t) == "table", "tablex.collapse - argument 't' must be a table")
local r = {} local r = {}
for _, v in ipairs(t) do for _, v in ipairs(t) do
if type(v) == "table" then if type(v) == "table" then