Skip to content

@AlexBaranosky /gist:1266137
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
StringBuilder Monoid
object Monoid {
implicit val StringBuilderMonoid = new Monoid[StringBuilder] {
override def zero = new StringBuilder("")
override def append(sb1: StringBuilder, sb2: StringBuilder) = {
val delimiter = if (sb1.toString == "" ||
sb2.toString == "") ""
else ", "
sb1.append(delimiter).append(sb2)
}
}
}
Writer(22, new StringBuilder("")).flatMap { n =>
Writer(n + 2, new StringBuilder("plus two"))
}.flatMap { n =>
Writer(n * 4, new StringBuilder("times four"))
})
// => Writer(96, StringBuilder("plus two, times four"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.