Changes in table library
This commit is contained in:
parent
63f7484f9b
commit
8d7d399af5
@ -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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user