Created
October 6, 2011 00:10
-
-
Save CampingScorpion/1266134 to your computer and use it in GitHub Desktop.
Improved StringMonoid
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
object Monoid { | |
implicit val StringMonoid = new Monoid[String] { | |
override def zero = "" | |
override def append(s1: String, s2: String) = { | |
val delimiter = if (s1 == zero || s2 == zero) "" | |
else ", " | |
s1 + delimiter + s2 | |
} | |
} | |
} | |
Writer(5).flatMap { n => Writer(n + 3, “added three”) | |
.flatMap { n => Writer(n * 5, "times five") } | |
.flatMap { n => Writer(n + 2, "plusTwo") } | |
// => Writer(42, "added three, times 5, plus two") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment