fix(vim.iter): use correct cmp function when truncating tail in take (#27998)

This commit is contained in:
Calvin Bochulak 2024-03-23 15:46:54 -06:00 committed by GitHub
parent 3d9c028a4c
commit ca6dbf3558
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View File

@ -709,7 +709,8 @@ end
---@private ---@private
function ListIter:take(n) function ListIter:take(n)
local inc = self._head < self._tail and 1 or -1 local inc = self._head < self._tail and 1 or -1
self._tail = math.min(self._tail, self._head + n * inc) local cmp = self._head < self._tail and math.min or math.max
self._tail = cmp(self._tail, self._head + n * inc)
return self return self
end end

View File

@ -247,6 +247,12 @@ describe('vim.iter', function()
eq({ 4, 3, 2, 1 }, vim.iter(t):take(5):totable()) eq({ 4, 3, 2, 1 }, vim.iter(t):take(5):totable())
end end
do
local t = { 4, 3, 2, 1 }
eq({ 1, 2, 3 }, vim.iter(t):rev():take(3):totable())
eq({ 2, 3, 4 }, vim.iter(t):take(3):rev():totable())
end
do do
local t = { 4, 3, 2, 1 } local t = { 4, 3, 2, 1 }
local it = vim.iter(t) local it = vim.iter(t)