Currently, if we want to iterate backwards through a table we would do something like this:
for i = #t, 1, -1 do
local index = t[i]
-- code
end
Whereas if we want to iterate forwards through a table, we can do something like this:
for index, value in ipairs(t) do
-- code
end
Because of the nature of Roblox lua, we are limited in the speeds of our methods. That is to say, built-in methods are noticeably faster than custom methods. What I am proposing is the introduction of a method named ipairs2()
which acts similar to the method named ipairs()
, except that ipairs2()
will iterate backwards through the table, instead of forwards. Not only would this provide a noticeable increase in speed, it would also make it far easier to use backwards table iteration.
This can be accomplished in Roblox lua currently, at the expense of being ~5-10x slower by doing something like this:
local function ipairs2(a, i)
i = i and i - 1 or #a
local v = a[i]
if v then
return i, v
end
end
You can then call this as follows:
for index, value in ipairs2(t) do
-- code
end
Now, what use does this have? If you have ever had to include a j
or offset
variable in your for loop to account for objects being removed from a table, you should be using backwards iteration instead because it doesn’t care if something is removed at a given index, because it starts from the end and goes to the beginning. What this means is that you can remove an unnecessary variable from your code by using backwards table iteration.
If Roblox is able to address this issue, it would improve my development experience because it would make backwards table iteration easier to use, and noticeably faster.