Skip to content

Instantly share code, notes, and snippets.

@mike-burns
Created August 25, 2013 02:17
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 mike-burns/6331552 to your computer and use it in GitHub Desktop.
Save mike-burns/6331552 to your computer and use it in GitHub Desktop.
Haskell's IO implemented in Ruby
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