add ripairs (reverse ipairs)

This commit is contained in:
Westerbly Snaydley 2021-11-09 23:45:01 -05:00
parent 65d8f24a45
commit cf8e0ac81a
2 changed files with 18 additions and 0 deletions

View File

@ -78,6 +78,9 @@ function _batteries:export()
--overwrite assert wholesale (it's compatible) --overwrite assert wholesale (it's compatible)
assert = self.assert assert = self.assert
--like ipairs, but in reverse
ripairs = self.tablex.ripairs
--export the whole library to global `batteries` --export the whole library to global `batteries`
batteries = self batteries = self

View File

@ -417,4 +417,19 @@ function tablex.unpack8(t)
return t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8] return t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8]
end end
local function iter (t, i)
i = i - 1
local v = t[i]
if v then
return i, v
end
end
--iterator that works like ipairs, but in reverse order,
--with keys #t to 1. May prematurely stop at the first nil value
--if the table has gaps.
function tablex.ripairs (t)
return iter, t, #t + 1
end
return tablex return tablex