14 lines
325 B
Lua
14 lines
325 B
Lua
|
---Filter elements of a table where the predicate is true
|
||
|
---@param t table
|
||
|
---@param predicate fun(x: any): boolean
|
||
|
---@return table
|
||
|
function table.filter(t, predicate)
|
||
|
local out = {}
|
||
|
for _, val in ipairs(t) do
|
||
|
if predicate(val) then
|
||
|
table.insert(out, val)
|
||
|
end
|
||
|
end
|
||
|
return out
|
||
|
end
|