From 0eda6e8262c75a94e4e98b0090fe24773fa20fd0 Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Fri, 15 Oct 2021 12:49:31 +1100 Subject: [PATCH] added functional.chain for conveniently mapping things like node paths and vector splines --- functional.lua | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/functional.lua b/functional.lua index 086eb0d..48bf573 100644 --- a/functional.lua +++ b/functional.lua @@ -215,7 +215,7 @@ function functional.cycle(t, f) local result = {} for i, a in ipairs(t) do 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 table.insert(result, v) end @@ -225,6 +225,25 @@ end 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