Created
October 5, 2011 23:58
-
-
Save CampingScorpion/1266102 to your computer and use it in GitHub Desktop.
Writer Monad 0.1
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
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