Created
February 26, 2016 10:33
-
-
Save wolfendale/b89b2c3f2307fd9dcd69 to your computer and use it in GitHub Desktop.
Monoid Stuff
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
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