mirror of
https://github.com/1bardesign/batteries.git
synced 2024-12-01 17:24:34 +00:00
moved functional.splat below functional.map_inplace, reformatted a little
This commit is contained in:
parent
fd998c8525
commit
aec62d92ec
@ -63,21 +63,6 @@ function functional.map(t, f)
|
|||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
-- map a sequence (see functional.map)
|
|
||||||
-- the function may return an optional index
|
|
||||||
-- if no 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
|
|
||||||
pos = (pos == nil and #result + 1) or pos
|
|
||||||
result[pos] = v
|
|
||||||
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)
|
||||||
@ -99,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)
|
||||||
|
Loading…
Reference in New Issue
Block a user