mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-25 23:24:35 +00:00
Update functional.lua
This commit is contained in:
parent
c0847e1955
commit
120c9658fc
@ -63,6 +63,23 @@ function functional.map(t, f)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- map a sequence (see functional.map)
|
||||||
|
-- when the function returns an index, map it there
|
||||||
|
function functional.map_index(t, f)
|
||||||
|
local result = {}
|
||||||
|
for i = 1, #t do
|
||||||
|
local v, pos = f(t[i], i)
|
||||||
|
if v ~= nil then
|
||||||
|
if not pos then
|
||||||
|
table.insert(result, v)
|
||||||
|
else
|
||||||
|
result[pos] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
--maps a sequence inplace, modifying it {a, b, c} -> {f(a), f(b), f(c)}
|
--maps a sequence inplace, modifying it {a, b, c} -> {f(a), f(b), f(c)}
|
||||||
-- (automatically drops any nils, which can be used to simultaneously map and filter)
|
-- (automatically drops any nils, which can be used to simultaneously map and filter)
|
||||||
function functional.map_inplace(t, f)
|
function functional.map_inplace(t, f)
|
||||||
|
Loading…
Reference in New Issue
Block a user