Last active
August 29, 2015 14:04
-
-
Save benolee/f6e964e77aba1ea2d853 to your computer and use it in GitHub Desktop.
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
# fyi: I have no idea what I'm doing :) | |
Log = Struct.new :buffer do | |
def call method | |
buffer << "Log: before #{method.name}\n" | |
method.call buffer | |
buffer << "Log: after #{method.name}\n\n" | |
self | |
end | |
end | |
# unit :: a -> M a | |
unit = -> buffer do Log[buffer] end | |
# bind :: M a -> (a -> M a) -> M a | |
bind = -> log, method do log.call method; log end.curry | |
def foo buffer | |
buffer << "hello from foo\n" | |
end | |
def bar buffer | |
buffer << "hola from bar\n" | |
end | |
foo, bar = method(:foo), method(:bar) | |
buffer = [] | |
# THE MAGIC! | |
with_logs = bind.(unit.(buffer)) | |
with_logs | |
.(foo) | |
.(bar) | |
.(foo) | |
puts buffer.join | |
__END__ | |
Log: before foo | |
hello from foo | |
Log: after foo | |
Log: before bar | |
hola from bar | |
Log: after bar | |
Log: before foo | |
hello from foo | |
Log: after foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment