Skip to content

Instantly share code, notes, and snippets.

@marcoonroad
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcoonroad/871133c2978d801e2e38 to your computer and use it in GitHub Desktop.
Save marcoonroad/871133c2978d801e2e38 to your computer and use it in GitHub Desktop.
If and Else as methods (as like Smalltalk)...
-- if and else as methods (test) --
-- object --
local logic = { main = nil }
-- if method --
function logic: so(code)
if self.main then
code( )
end
return self
end
-- else method --
function logic: otherwise(code)
if not self.main then
code( )
end
return
end
-- closure to emulate a block --
function say(message)
return function ( )
print(message)
end
end
------------------------------------
function logic: assign(value)
self.main = value
return
end
function logic: valid(expression)
self: assign(expression)
return self
end
------------------------------------
logic: valid(15 <= 5):
so(say "Yes, less or equal than!"):
otherwise(say "Not! Greater than...")
-- end of script --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment