Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active July 19, 2018 19:57
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 JoshCheek/1c442e755ec3df0bbef3654cf693e5c0 to your computer and use it in GitHub Desktop.
Save JoshCheek/1c442e755ec3df0bbef3654cf693e5c0 to your computer and use it in GitHub Desktop.
SiB Example
# Something I thought of at 25:06 in this talk: https://www.youtube.com/watch?time_continue=1&v=FDs-sSxo2iY
class If
def initialize(condition)
@condition = condition
end
def then(result)
@condition.then result
end
def inspect
"<if #{@condition.inspect}>"
end
end
class ReturnThen
def initialize(result)
@result = result
end
def else(_)
@result
end
def inspect
"<returnThen>"
end
end
class ReturnElse
def else(result)
result
end
def inspect
"<returnElse>"
end
end
class True
def then(result)
ReturnThen.new result
end
def inspect
"<true>"
end
end
class False
def then(result)
ReturnElse.new
end
def inspect
"<false>"
end
end
class Zero
def succ
Number.new self
end
def inspect
"<zero>"
end
end
class Number
def initialize(pred)
@pred = pred
end
def succ
Number.new self
end
def inspect
@pred.inspect + "+" # I like a plus here better, since it's the successor
end
end
# we'll ignore that constants are cheating ;)
TRUE = True.new
FALSE = False.new
ZERO = Zero.new
ONE = ZERO.succ
TWO = ONE.succ
# you don't have to assign the result to a variable,
# it's smart enough to look at the previous line
If.new(TRUE)
.then(ONE)
.else(TWO)
# => <zero>+
# you can also look at the intermediate values, which might be useful
If.new(FALSE) # => <if <false>>
.then(ONE) # => <returnElse>
.else(TWO) # => <zero>++
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment