Skip to content

Instantly share code, notes, and snippets.

@wolfendale
Created February 26, 2016 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfendale/b89b2c3f2307fd9dcd69 to your computer and use it in GitHub Desktop.
Save wolfendale/b89b2c3f2307fd9dcd69 to your computer and use it in GitHub Desktop.
Monoid Stuff
sealed trait Foobar
case object Foo extends Foobar
case object Bar extends Foobar
case class Baz(foo: Boolean = false, bar: Boolean = false)
implicit val bazMon: Monoid[Baz] =
new Monoid[Baz] {
override def zero: Baz = Baz()
override def append(f1: Baz, f2: => Baz): Baz =
Baz(
f1.foo || f2.foo,
f1.bar || f2.bar
)
}
def convert
(a: Set[Foobar])
(implicit mon: Monoid[Baz]): Baz =
a.foldLeft(mon.zero) {
case (m, n) =>
mon.append(m, n match {
case Foo => Baz(foo = true)
case Bar => Baz(bar = true)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment