mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-09-12 08:08:30 +02:00
....but it's not tested or hooked up yet. Next up: a chat command definition so we can test it.
16 lines
563 B
Lua
16 lines
563 B
Lua
|
|
|
|
--- Lua implementation of array.reduce() from Javascript.
|
|
-- @param tbl The table to iterate over.
|
|
-- @param func The function to call for every element in tbl. Will be passed the following arguments: accumulator, value, index, table. Of course, the provided function need not take this many arguments.
|
|
-- @param initial_value The initial value of the accumulator.
|
|
local function table_reduce(tbl, func, initial_value)
|
|
local acc = initial_value
|
|
for key, value in pairs(tbl) do
|
|
acc = func(acc, value, key, tbl)
|
|
end
|
|
return acc
|
|
end
|
|
|
|
return table_reduce
|