Skip to content

Instantly share code, notes, and snippets.

@benolee
Last active August 29, 2015 14:04
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 benolee/f6e964e77aba1ea2d853 to your computer and use it in GitHub Desktop.
Save benolee/f6e964e77aba1ea2d853 to your computer and use it in GitHub Desktop.
# 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