Changes in table library

This commit is contained in:
Nikita Kruglickiy 2021-11-23 00:49:32 +03:00
parent 63f7484f9b
commit 8d7d399af5

View File

@ -57,18 +57,18 @@ end
function table.map(tbl, action)
local res = {}
for _, field in ipairs(tbl) do
table.insert(res, action(field))
for idx, field in ipairs(tbl) do
table.insert(res, action(field, idx))
end
return res
end
function table.filter(tbl, predicate)
function table.filter(tbl, filterFunc)
local res = {}
for _, field in ipairs(tbl) do
if predicate(field) == true then
for idx, field in ipairs(tbl) do
if filterFunc(field, idx) == true then
table.insert(res, field)
end
end
@ -84,13 +84,11 @@ function table.find(tbl, predicate)
end
end
function table.reduce(tbl, func, init)
init = init or 0
function table.reduce(tbl, reducer, init)
local accum = init
for _, field in ipairs(tbl) do
accum = accum + func(field)
for idx, field in ipairs(tbl) do
accum = reducer(accum, field, idx)
end
return accum