wea.table_map(): Add api function

This commit is contained in:
Starbeamrainbowlabs 2021-05-29 22:49:35 +01:00
parent 1e677c1f3f
commit 8eb9a8ed0f
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -65,3 +65,17 @@ function worldeditadditions.table_tostring(tbl, sep, new_line)
end end
return ret:concat("") return ret:concat("")
end end
--- Executes the given function on every item in the given table.
-- Ignores return values that are nil and doesn't insert them into the table.
-- @param tbl table The table to operate on.
-- @param func function The function to execute on every item in the table.
-- @returns table A new table containing the return values of the function.
function worldeditadditions.table_map(tbl, func)
local result = {}
for i,value in ipairs(tbl) do
local newval = func(value, i)
if newval ~= nil then table.insert(tbl, newval) end
end
end