lua-users home
lua-l archive

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


On Thu, Jun 7, 2012 at 5:39 AM, Geoff Leyland <geoff_leyland@fastmail.fm> wrote:
> I did, and it was slower than using intermediate tables (and more complicated.  It did save some memory though).  Is it a dead-end idea, or is it that my implementation [1] is brain dead?

Well, ipairs is _always_ going to be slower than numeric-for, so I'd
expect any performance-conscious person to override __len as well.

This is then definitely going to be faster:

function imapi (f,t)
  local i,n = 0,#t
  return function()
    i = i + 1
    if i > n then return end
    return i,f(t[i])
  end
end

The explicit comparison against the length allows you to iterate over
sparse arrays that can 'contain' nils.

(Note that LuaJIT has a Lua-5.2 compatibility mode which allows nice
things like __pairs and __len)

steve d.