Created
August 25, 2013 02:17
-
-
Save mike-burns/6331552 to your computer and use it in GitHub Desktop.
Haskell's IO implemented in Ruby
This file contains hidden or 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
class Io | |
attr_reader :action | |
def initialize(&action) | |
@action = action | |
end | |
def bind(&f) | |
Io.new do | |
result = @action.call | |
f.call(result) | |
end | |
end | |
# This is what the compiler calls. | |
def run | |
result = @action.call | |
if result.is_a?(Io) | |
result.run | |
else | |
result | |
end | |
end | |
end | |
def get_line(io = $stdin) | |
Io.new { io.gets } | |
end | |
def put_line(s, io = $stdout) | |
Io.new { io.puts s } | |
end | |
# You type this: | |
main = get_line.bind { |s| put_line s } | |
# And then the compiler does this: | |
main.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment