Last active
August 29, 2015 14:08
-
-
Save marcoonroad/871133c2978d801e2e38 to your computer and use it in GitHub Desktop.
If and Else as methods (as like Smalltalk)...
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
-- 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