Functional helpers, table.deep_add_all

master
Lars Mueller 2020-12-20 15:21:03 +01:00
parent ccff2f7673
commit 8f4b73e99d
2 changed files with 45 additions and 4 deletions

View File

@ -1,6 +1,36 @@
no_op = function() end
function curry(func, ...) function curry(func, ...)
local args = {...} local args = { ... }
return function(...) return function(...) return func(unpack(args), ...) end
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 end

View File

@ -235,6 +235,17 @@ function add_all(table, additions)
return table return table
end 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) function complete(table, completions)
for key, value in pairs(completions) do for key, value in pairs(completions) do
if table[key] == nil then if table[key] == nil then