lua-users home
lua-l archive

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


2013/6/12  <meino.cramer@gmx.de>:

> is it possible (and how) to create an iterator with lua, which returns
> the [n]th _and_ the [n+1]th value of in array/table with each call?

It is trivial if you are willing to create a new closure each time.

function brix(a)
   local i,n=0,#a
   return function()
     i=i+1
     if i<n then return a[i],a[i+1] end
   end
end