From 8eb9a8ed0f20c94f6611125e7858cb21a1ec77de Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 29 May 2021 22:49:35 +0100 Subject: [PATCH] wea.table_map(): Add api function --- worldeditadditions/utils/tables.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/worldeditadditions/utils/tables.lua b/worldeditadditions/utils/tables.lua index e20bd63..cbaf2a0 100644 --- a/worldeditadditions/utils/tables.lua +++ b/worldeditadditions/utils/tables.lua @@ -65,3 +65,17 @@ function worldeditadditions.table_tostring(tbl, sep, new_line) end return ret:concat("") 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 +