Created
April 28, 2023 03:00
-
-
Save outsinre/384565f0a52ce273deb13bc7d6ea4bef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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