mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 22:24:35 +00:00
add ripairs (reverse ipairs)
This commit is contained in:
parent
65d8f24a45
commit
cf8e0ac81a
3
init.lua
3
init.lua
@ -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
|
||||||
|
|
||||||
|
15
tablex.lua
15
tablex.lua
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user