From e81b46d625b3b2a7dfd7ce176e6b767240a289e3 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Sun, 16 Jul 2023 18:56:34 +0200 Subject: [PATCH] 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. --- config/nvim/lua/user/util.lua | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/config/nvim/lua/user/util.lua b/config/nvim/lua/user/util.lua index fa18c15..ecf5b92 100644 --- a/config/nvim/lua/user/util.lua +++ b/config/nvim/lua/user/util.lua @@ -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)(...) == 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