added tablex.previous_element

This commit is contained in:
Max Cahill 2024-04-24 13:04:30 +10:00
parent aee733833e
commit 9597861822

View File

@ -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