vim: better partial() function in Lua

partial() now works with any number of arguments. As a bonus, it is
implemented in terms of the new extend() function, which is also useful
on its own.
This commit is contained in:
Fernando Schauenburg 2023-07-16 18:56:34 +02:00
parent 026b4e8ddc
commit e81b46d625

View file

@ -10,13 +10,30 @@ M.flip = function(f)
end
end
-- Extend lists.
--
-- extend({'a', 'b'}, {'c', 'd'}) == {'a', 'b', 'c', 'd'}
-- extend({1, 2}, {3, 4}, {5, 6}) == {1, 2, 3, 4, 5, 6}
--
M.extend = function(...)
local result = {}
for _, tbl in ipairs {...} do
for _, v in pairs(tbl) do
result[#result+1] = v
end
end
return result
end
-- Partial function application:
--
-- partial(f, x)(...) == f(x, ...)
-- partial(f, x, y)(...) == f(x, y, ...)
--
M.partial = function(f, x)
M.partial = function(f, ...)
local argv = {...}
return function(...)
return f(x, ...)
return f(unpack(M.extend(argv, {...})))
end
end
@ -39,5 +56,8 @@ M.get_selected_text = function()
end)
end
M.use_local = function()
end
return M