mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-25 23:24:35 +00:00
Merge feature/tablex.rotate
refactored to use loops instead of recursion for simplicity
This commit is contained in:
commit
0a02d7a2da
17
tablex.lua
17
tablex.lua
@ -61,6 +61,23 @@ function tablex.swap_and_pop(t, i)
|
||||
return tablex.pop(t)
|
||||
end
|
||||
|
||||
--rotate the elements of a table t by amount slots
|
||||
-- amount 1: {1, 2, 3, 4} -> {2, 3, 4, 1}
|
||||
-- amount -1: {1, 2, 3, 4} -> {4, 1, 2, 3}
|
||||
function tablex.rotate(t, amount)
|
||||
if #t > 1 then
|
||||
while amount > 0 do
|
||||
tablex.push(t, tablex.shift(t))
|
||||
amount = amount - 1
|
||||
end
|
||||
while amount < 0 do
|
||||
tablex.unshift(t, tablex.pop(t))
|
||||
amount = amount + 1
|
||||
end
|
||||
end
|
||||
return t
|
||||
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