mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-29 08:14:35 +00:00
[tablex] Add rotate
This commit is contained in:
parent
1d59b3623e
commit
8a069c38d4
12
tablex.lua
12
tablex.lua
@ -61,6 +61,18 @@ function tablex.swap_and_pop(t, i)
|
||||
return tablex.pop(t)
|
||||
end
|
||||
|
||||
--rotate the elements of a table by amount
|
||||
function tablex.rotate(t, amount)
|
||||
if amount == 0 then return t end
|
||||
if amount > 0 then
|
||||
tablex.push(t, tablex.shift(t))
|
||||
return tablex.rotate(t, amount - 1)
|
||||
elseif amount < 0 then
|
||||
tablex.unshift(t, tablex.pop(t))
|
||||
return tablex.rotate(t, amount + 1)
|
||||
end
|
||||
end
|
||||
|
||||
--default comparison; hoisted for clarity
|
||||
--(shared with sort.lua and suggests the sorted functions below should maybe be refactored there)
|
||||
local function default_less(a, b)
|
||||
|
Loading…
Reference in New Issue
Block a user