# Could you do it better? Generator functions

I need a function that takes a table and each time returns its next value, starts from first and after last returns first again and so forth.
There’s a

``````
next

``````

function but it requires a last key and don’t rewind automatically.

So here’s my solution:

``````

local a = {"a", "b", "c"}

local snake = function(tbl) return coroutine.wrap(function(tbl)
local m = #tbl
while true do
k,v = next(tbl, k ~= m and k or nil)
coroutine.yield(v)
end
end)(tbl)
end

for i = 1,7 do
print(snake(a))
end

``````

prints:

It seems pretty overhead to me. Any ideas?

[lua]
local snake = coroutine.wrap(function(t)
while true do
for i = 1, #t do
coroutine.yield(t*)
end
end
end)
[/lua]

sorry meep

but the point I want to get across is that however great lua coroutines are, you shouldn’t use them for everything

``````

function iterateTable(tbl)
local i = 0
return function()
i = (i < #tbl) and (i + 1) or 1
return t*
end
end

``````

Well

``````

local a = {"a", "b", "c"}

function iterateTable(tbl)
local i = 0
return function()
i = i == #tbl and 1 or i + 1
return tbl*
end
end

local ab = iterateTable(a)

for i = 1,7 do
print(ab())
end

``````

Yes it’s better but I have to first get function then run it

I can’t think of any way to avoid the step of calling a function to get an iterator function first, or else, you would only be able to run the iterator with a one table at one time.

Is coroutine approach really unsuitable here?
I compared the time of performance your way approximately 2 (100 iterations) - 9 (1000000 iterations) times faster, but I really doubt I would run this func more than 10 times at once

I’m not saying it’s unsuitable, the coroutine approach is really nice to look at, my only point is, you don’t HAVE to use it.

(and I don’t really understand what you mean with performance, so I tested both methods myself over 5000000 iterations in luajit 2.10 and here’s what I got:

``````

F:\Work	ests>luajit bench.lua