add several utility functions related to tables.

create api functions `any`, `every`, and `underride`.

`any` and `every` checks if a condition is true for any and every item
in a table, respectively.
`underride` returns a table that defaults to a provided table, for
items that aren't specified in the other provided table.
This commit is contained in:
trans_soup 2023-10-17 11:19:55 +02:00
parent 33fbd7c35e
commit 5a3d81787c
1 changed files with 23 additions and 0 deletions

View File

@ -26,3 +26,26 @@ function blockgame.shuffle (tab)
end
return result
end
function blockgame.any (tab, check)
for key, value in pairs(tab) do
if check(value, key, tab) then return true end
end
return false
end
function blockgame.every (tab, check)
for key, value in pairs(tab) do
if not check(value, key, tab) then return false end
end
return true
end
function blockgame.underride (tab, template)
local tab = tab or {}
local result = blockgame.shallow_copy_table(template)
for key, value in pairs(tab) do
result[key] = value
end
return result
end