lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Nov 25, 2013 at 2:32 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> On Mon, Nov 25, 2013 at 12:19 PM, John Hind <john.hind@zen.co.uk> wrote:
>> Having the existing single element table.remove and table.insert encourages users to write [inefficient] code like:
>
> Well, no language is safe from inexperienced users.  It's only really
> a problem if the resulting program is too slow for its purpose.
>
>> t = {}
>> for i=1, 10000 do t[i] = i end
>> for i=11, 9899 do t[i] = t[91+i] end
>> for i=10000, 9900, -1 do t[i] = nil end
>
> What is the magic number 91 doing here? I'm curious....
>
> steve d.
>

There were 91 items deleted by the calls to table.remove(), so in
order to update the indices on the resulting loop to match the
behavior of the original code, the items in positions 101..10000 need
to be shifted left by 91 slots.

/s/ Adam