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