Skip to content

Commit 23deb47

Browse files
authored
fix: couple bugs with multiple results and varargs (...) (#515)
* fix(vararg.rotate): edge cases and nil arguments - zero argument rotation returned one value (a global by the name A0) - the generic fallback dropped the first argument and trailing nils * fix: functional.partial & fun.bind These only worked for binding exactly one parameter. * Include generated rotate.lua file in linting
1 parent e739a2e commit 23deb47

File tree

7 files changed

+56
-30
lines changed

7 files changed

+56
-30
lines changed

.luacheckrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ read_globals = {
2727
exclude_files = {
2828
"lua/plenary/profile/lua_profiler.lua",
2929
"lua/plenary/profile/memory_profiler.lua",
30-
"lua/plenary/vararg/rotate.lua",
3130
"lua/plenary/async_lib/*.lua",
3231
}
3332

lua/plenary/fun.lua

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
local tbl = require "plenary.tbl"
2-
31
local M = {}
42

5-
function M.bind(fn, ...)
6-
if select("#", ...) == 1 then
7-
local arg = ...
8-
return function(...)
9-
fn(arg, ...)
10-
end
11-
end
12-
13-
local args = tbl.pack(...)
14-
return function(...)
15-
fn(tbl.unpack(args), ...)
16-
end
17-
end
3+
M.bind = require("plenary.functional").partial
184

195
function M.arify(fn, argc)
206
return function(...)

lua/plenary/functional.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@ function f.join(array, sep)
1616
return table.concat(vim.tbl_map(tostring, array), sep)
1717
end
1818

19-
function f.partial(fun, ...)
20-
local args = { ... }
21-
return function(...)
22-
return fun(unpack(args), ...)
19+
local function bind_n(fn, n, a, ...)
20+
if n == 0 then
21+
return fn
2322
end
23+
return bind_n(function(...)
24+
return fn(a, ...)
25+
end, n - 1, ...)
26+
end
27+
28+
function f.partial(fun, ...)
29+
return bind_n(fun, select("#", ...), ...)
2430
end
2531

2632
function f.any(fun, iterable)

lua/plenary/vararg/rotate.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ local tbl = require "plenary.tbl"
66

77
local rotate_lookup = {}
88

9-
rotate_lookup[0] = function()
10-
return A0
11-
end
12-
139
rotate_lookup[1] = function(A0)
1410
return A0
1511
end
@@ -71,12 +67,16 @@ rotate_lookup[15] = function(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A
7167
end
7268

7369
local function rotate_n(first, ...)
70+
local n = select("#", ...) + 1
7471
local args = tbl.pack(...)
75-
args[#args + 1] = first
76-
return tbl.unpack(args)
72+
args[n] = first
73+
return tbl.unpack(args, 1, n)
7774
end
7875

7976
local function rotate(nargs, ...)
77+
if nargs == nil or nargs < 1 then
78+
return
79+
end
8080
return (rotate_lookup[nargs] or rotate_n)(...)
8181
end
8282

scripts/vararg/rotate.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,23 @@ local tbl = require('plenary.tbl')
77

88
local rotate_lookup = {}
99

10-
{% for n in range(0, amount) %}
10+
{% for n in range(1, amount) %}
1111
rotate_lookup[{{n}}] = function ({% for n in range(n) %} A{{n}} {{ ", " if not loop.last else "" }} {% endfor %})
1212
return {% for n in range(1, n) %} A{{n}}, {% endfor %} A0
1313
end
1414
{% endfor %}
1515

1616
local function rotate_n(first, ...)
17+
local n = select("#", ...) + 1
1718
local args = tbl.pack(...)
18-
args[#args+1] = first
19-
return tbl.unpack(args)
19+
args[n] = first
20+
return tbl.unpack(args, 1, n)
2021
end
2122

2223
local function rotate(nargs, ...)
24+
if nargs == nil or nargs < 1 then
25+
return
26+
end
2327
return (rotate_lookup[nargs] or rotate_n)(...)
2428
end
2529

tests/plenary/functional_spec.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
local f = require "plenary.functional"
2+
3+
describe("functional", function()
4+
describe("partial", function()
5+
local function args(...)
6+
assert.is.equal(4, select("#", ...))
7+
return table.concat({ ... }, ",")
8+
end
9+
it("should bind correct parameters", function()
10+
local expected = args(1, 2, 3, 4)
11+
assert.is.equal(expected, f.partial(args)(1, 2, 3, 4))
12+
assert.is.equal(expected, f.partial(args, 1)(2, 3, 4))
13+
assert.is.equal(expected, f.partial(args, 1, 2)(3, 4))
14+
assert.is.equal(expected, f.partial(args, 1, 2, 3)(4))
15+
assert.is.equal(expected, f.partial(args, 1, 2, 3, 4)())
16+
end)
17+
end)
18+
end)

tests/plenary/rotate_spec.lua

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,26 @@ local eq = function(a, b)
55
end
66

77
describe("rotate", function()
8+
it("should return as many values, as the first argument", function()
9+
local args = {}
10+
for _ = 0, 20 do
11+
local n = select("#", unpack(args))
12+
assert.is.equal(n, select("#", rotate(n, unpack(args))))
13+
args[#args + 1] = n
14+
end
15+
end)
16+
817
it("should rotate varargs", function()
918
eq({ rotate(3, 1, 2, 3) }, { 2, 3, 1 })
1019
eq({ rotate(9, 1, 2, 3, 4, 5, 6, 7, 8, 9) }, { 2, 3, 4, 5, 6, 7, 8, 9, 1 })
1120
end)
1221

22+
it("should rotate zero", function()
23+
assert.is.equal(0, select("#", rotate(0)))
24+
end)
25+
1326
it("should rotate none", function()
14-
eq({ rotate() }, {})
27+
assert.is.equal(0, select("#", rotate()))
1528
end)
1629

1730
it("should rotate one", function()

0 commit comments

Comments
 (0)