Re: Iteration and nils
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Iteration and nils
- From: David Manura <dm.lua@...>
- Date: 2007年8月17日 00:42:16 +0000 (UTC)
Jerome Vuarand <jerome.vuarand <at> ubisoft.com> writes:
> I found out while looking for 'apairs'...
> here is a simple (tested) implementation:
>
> function apairs(...)
> local n = select('#', ...)
> return function(a, i)
> if i < n then
> return i+1,a[i+1]
> end
> end, {...}, 0
> end
Here's a similar form that avoids creating the closure:
local function helper(a, i)
if i < a.n then return i+1,a[i+1] end
end
function apairs(...)
return helper, {n=select('#', ...), ...}, 0
end
-- test
function f(...)
for i,a in apairs(...) do print(i, a) end
end
f("foo", nil, 32, nil)