Skip to content

Instantly share code, notes, and snippets.

@outsinre
Created April 28, 2023 03:00
Show Gist options
  • Save outsinre/384565f0a52ce273deb13bc7d6ea4bef to your computer and use it in GitHub Desktop.
Save outsinre/384565f0a52ce273deb13bc7d6ea4bef to your computer and use it in GitHub Desktop.
local function list_iter (t)
local i = 0
local n = #t
return function ()
i = i + 1
if i <= n then return t[i] end
end
end
local t = { 10, 20, 30 }
for e in list_iter(t) do
print(e)
end
print()
local a_iter = list_iter(t)
while true do
local e = a_iter()
if e == nil then
break
end
print(e)
end
print()
local b_iter = list_iter(t)
for e in b_iter do
print(e)
end
print()
-- error as 'for' expects 'c_iter' to be a factory function
local c_iter = list_iter(t)
for e in c_iter() do
print(e)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment