Skip to content

Instantly share code, notes, and snippets.

@actboy168
Last active May 30, 2019 11:30
Show Gist options
  • Save actboy168/6a93eee91e644df05fc40a0b9ed2a470 to your computer and use it in GitHub Desktop.
Save actboy168/6a93eee91e644df05fc40a0b9ed2a470 to your computer and use it in GitHub Desktop.
local function if_then(toclose)
if not toclose then
return function() end
end
return function (_, w)
if not w then
return toclose
end
end, nil, nil, toclose
end
local function Class(def)
return function(...)
local res = def.create(...)
if not res then
return
end
return setmetatable(res, { __close = def.remove })
end
end
local human = Class {
create = function(name)
if #name > 4 then
return
end
local w = {
Name = name
}
print(w.Name, ": born")
return w
end,
remove = function(w)
print(w.Name, ": die")
end,
}
for o in if_then(human "Alice") do
print(o.Name, ": do")
end
for o in if_then(human "Bob") do
print(o.Name, ": do")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment