Last active
May 30, 2019 11:30
-
-
Save actboy168/6a93eee91e644df05fc40a0b9ed2a470 to your computer and use it in GitHub Desktop.
This file contains 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 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