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:
parent
026b4e8ddc
commit
e81b46d625
1 changed files with 23 additions and 3 deletions
|
@ -10,13 +10,30 @@ M.flip = function(f)
|
||||||
end
|
end
|
||||||
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 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 function(...)
|
||||||
return f(x, ...)
|
return f(unpack(M.extend(argv, {...})))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,5 +56,8 @@ M.get_selected_text = function()
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
M.use_local = function()
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue