mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 22:24:35 +00:00
added functional.chain for conveniently mapping things like node paths and vector splines
This commit is contained in:
parent
eae1849c16
commit
0eda6e8262
@ -215,7 +215,7 @@ function functional.cycle(t, f)
|
|||||||
local result = {}
|
local result = {}
|
||||||
for i, a in ipairs(t) do
|
for i, a in ipairs(t) do
|
||||||
local b = t[mathx.wrap(i + 1, 1, #t + 1)]
|
local b = t[mathx.wrap(i + 1, 1, #t + 1)]
|
||||||
local v = f(a, b, i)
|
local v = f(a, b)
|
||||||
if v ~= nil then
|
if v ~= nil then
|
||||||
table.insert(result, v)
|
table.insert(result, v)
|
||||||
end
|
end
|
||||||
@ -225,6 +225,25 @@ end
|
|||||||
|
|
||||||
functional.map_cycle = functional.cycle
|
functional.map_cycle = functional.cycle
|
||||||
|
|
||||||
|
--maps a sequence {a, b, c} -> { f(a, b), f(b, c) }
|
||||||
|
-- useful for inter-dependent data
|
||||||
|
-- (automatically drops any nils, same as map)
|
||||||
|
|
||||||
|
function functional.chain(t, f)
|
||||||
|
local result = {}
|
||||||
|
for i = 2, #t do
|
||||||
|
local a = t[i-1]
|
||||||
|
local b = t[i]
|
||||||
|
local v = f(a, b)
|
||||||
|
if v ~= nil then
|
||||||
|
table.insert(result, v)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
functional.map_chain = functional.chain
|
||||||
|
|
||||||
|
|
||||||
-----------------------------------------------------------
|
-----------------------------------------------------------
|
||||||
--generating data
|
--generating data
|
||||||
|
Loading…
Reference in New Issue
Block a user