Merge #48 - functional.splat

This commit is contained in:
Max Cahill 2022-01-25 10:23:07 +11:00
commit 8fd402b125

View File

@ -84,6 +84,23 @@ end
--alias --alias
functional.remap = functional.map_inplace functional.remap = functional.map_inplace
--maps a sequence into a new index space (see functional.map)
-- the function may return an index where the value will be stored in the result
-- if no index (or a nil index) is provided, it will insert as normal
function functional.splat(t, f)
local result = {}
for i = 1, #t do
local v, pos = f(t[i], i)
if v ~= nil then
if pos == nil then
pos = #result + 1
end
result[pos] = v
end
end
return result
end
--filters a sequence --filters a sequence
-- returns a table containing items where f(v, i) returns truthy -- returns a table containing items where f(v, i) returns truthy
function functional.filter(t, f) function functional.filter(t, f)