Skip to content

Instantly share code, notes, and snippets.

@AlexBaranosky
Created October 6, 2011 00:10
Show Gist options
  • Save AlexBaranosky/1266134 to your computer and use it in GitHub Desktop.
Save AlexBaranosky/1266134 to your computer and use it in GitHub Desktop.
Improved StringMonoid
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