From 8f4b73e99dd20305e2d6fc97b9efc444f5380d79 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Sun, 20 Dec 2020 15:21:03 +0100 Subject: [PATCH] Functional helpers, table.deep_add_all --- func.lua | 38 ++++++++++++++++++++++++++++++++++---- table.lua | 11 +++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/func.lua b/func.lua index 50fe6d7..1fe8f84 100644 --- a/func.lua +++ b/func.lua @@ -1,6 +1,36 @@ +no_op = function() end + function curry(func, ...) - local args = {...} - return function(...) - return func(unpack(args), ...) - end + local args = { ... } + return function(...) return func(unpack(args), ...) end +end + +function curry_tail(func, ...) + local args = { ... } + return function(...) return func(..., unpack(args)) end +end + +function call(...) + local args = { ... } + return function(func) return func(unpack(args)) end +end + +function value(val) return function() return val end end + +function values(...) + local args = { ... } + return function() return unpack(args) end +end + +function override_chain(func, override) + return function(...) + func(...) + return override(...) + end +end + +function assert(value, callback) + if not value then + error(callback()) + end end \ No newline at end of file diff --git a/table.lua b/table.lua index a551767..211bc14 100644 --- a/table.lua +++ b/table.lua @@ -235,6 +235,17 @@ function add_all(table, additions) return table end +function deep_add_all(table, additions) + for key, value in pairs(additions) do + if type(table[key]) == "table" and type(value) == "table" then + deep_add_all(table[key], value) + else + table[key] = value + end + end + return table +end + function complete(table, completions) for key, value in pairs(completions) do if table[key] == nil then