lua-users home
lua-l archive

Iterate over vararg

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


The task is to write an iterator factory 'args' so that
 for k,v in args(...) do --[[whatever]] end
does the same as
 for k=1,select('#',...) do local v = select(k,...) --[[whatever]] end
It is quite easy by using a table, of course:
~~~~
args = function(...)
 local t = table.pack(...)
 local n = t.n
 local i = 0
 return function()
 i=i+1
 if i<=n then return i,t[i] end
 end
end
~~~~
It seems to be quite hard to do, even in the C API, without creating a
table. In fact, I have so far not succeeded.

AltStyle によって変換されたページ (->オリジナル) /