lua-users home
lua-l archive

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


On Wed, Jun 10, 2009 at 1:38 PM, steve donovan<steve.j.donovan@gmail.com> wrote:
> On Wed, Jun 10, 2009 at 1:59 PM, Luiz Henrique de
> Figueiredo<lhf@tecgraf.puc-rio.br> >   > setmetatable(t,{
>>            __call = function (x,y,z) return next(x,z) end
>>    })
>>
>>    for k,v in t do print(k,v) end
>>
>> No parentheses needed.
>
> That is actually rather cute, but how would you do that for an
> ipairs-style iterator?

t = {"hello", "world", "from", "Lua"}

setmetatable(t, {__call = function(t,v,i)
  i = (i or 0) + 1
  v = t[i]
  if v then return i, v end
end})

for k, v in t do print(k, v) end
--> 1  hello
--> 2  world
--> 3  from
--> 4  Lua