Skip to content

Instantly share code, notes, and snippets.

@AlexBaranosky
Created October 5, 2011 23:58
Show Gist options
  • Save AlexBaranosky/1266102 to your computer and use it in GitHub Desktop.
Save AlexBaranosky/1266102 to your computer and use it in GitHub Desktop.
Writer Monad 0.1
case class Writer(value: Int, diary: String) {
def flatMap(f: Int => Writer) = {
f(value) match {
case Writer(result, d) => Writer(result, diary + d)
}
}
def map(f: Int => Int) = Writer(f(value), diary)
}
// This enables us to say:
new Writer(2, "").flatMap { new Writer(3, "three") }
.flatMap { new Writer(4, "four") }
// => Writer(4, "threefour")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment