mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-22 06:04:35 +00:00
added tablex.previous_element
This commit is contained in:
parent
aee733833e
commit
9597861822
14
tablex.lua
14
tablex.lua
@ -182,6 +182,20 @@ function tablex.next_element(t, v)
|
|||||||
return t[i]
|
return t[i]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function tablex.previous_element(t, v)
|
||||||
|
local i = tablex.index_of(t, v)
|
||||||
|
--not present? just get the front of the table
|
||||||
|
if not i then
|
||||||
|
return tablex.front(t)
|
||||||
|
end
|
||||||
|
--(not using mathx to avoid inter-dependency)
|
||||||
|
i = i - 1
|
||||||
|
if i == 0 then
|
||||||
|
i = #t
|
||||||
|
end
|
||||||
|
return t[i]
|
||||||
|
end
|
||||||
|
|
||||||
--note: keyed versions of the above aren't required; you can't double
|
--note: keyed versions of the above aren't required; you can't double
|
||||||
--up values under keys
|
--up values under keys
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user