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, ...)
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

View File

@ -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