Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding WriterT Monoid #1014

Merged
merged 6 commits into from
May 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ private[data] sealed abstract class WriterTInstances extends WriterTInstances0 {
implicit def writerTShow[F[_], L, V](implicit F: Show[F[(L, V)]]): Show[WriterT[F, L, V]] = new Show[WriterT[F, L, V]] {
override def show(f: WriterT[F, L, V]): String = f.show
}

implicit def writerTIdMonoid[L:Monoid, V:Monoid]: Monoid[WriterT[Id, L, V]] =
writerTMonoid[Id, L, V]
}

private[data] sealed abstract class WriterTInstances0 extends WriterTInstances1 {
Expand All @@ -92,6 +95,9 @@ private[data] sealed abstract class WriterTInstances0 extends WriterTInstances1

implicit def writerTEq[F[_], L, V](implicit F: Eq[F[(L, V)]]): Eq[WriterT[F, L, V]] =
F.on(_.run)

implicit def writerTIdSemigroup[L:Semigroup, V:Semigroup]: Semigroup[WriterT[Id, L, V]] =
writerTSemigroup[Id, L, V]
}

private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2 {
Expand All @@ -100,13 +106,23 @@ private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2
implicit val F0: MonadFilter[F] = F
implicit val L0: Monoid[L] = L
}

implicit def writerTMonoid[F[_], L, V](implicit W: Monoid[F[(L, V)]]): Monoid[WriterT[F, L, V]] =
new WriterTMonoid[F, L, V] {
implicit val F0: Monoid[F[(L, V)]] = W
}
}
private[data] sealed abstract class WriterTInstances2 extends WriterTInstances3 {
implicit def writerTMonadWriter[F[_], L](implicit F: Monad[F], L: Monoid[L]): MonadWriter[WriterT[F, L, ?], L] =
new WriterTMonadWriter[F, L] {
implicit val F0: Monad[F] = F
implicit val L0: Monoid[L] = L
}

implicit def writerTSemigroup[F[_], L, V](implicit W: Semigroup[F[(L, V)]]): Semigroup[WriterT[F, L, V]] =
new WriterTSemigroup[F, L, V] {
implicit val F0: Semigroup[F[(L, V)]] = W
}
}

private[data] sealed abstract class WriterTInstances3 extends WriterTInstances4 {
Expand Down Expand Up @@ -236,6 +252,19 @@ private[data] sealed trait WriterTMonadCombine[F[_], L] extends MonadCombine[Wri
override implicit def F0: MonadCombine[F]
}

private[data] sealed trait WriterTSemigroup[F[_], L, A] extends Semigroup[WriterT[F, L, A]] {
implicit def F0: Semigroup[F[(L, A)]]

def combine(x: WriterT[F, L, A], y: WriterT[F, L, A]): WriterT[F, L, A] =
WriterT(F0.combine(x.run, y.run))
}

private[data] sealed trait WriterTMonoid[F[_], L, A] extends Monoid[WriterT[F, L, A]] with WriterTSemigroup[F, L, A]{
override implicit def F0: Monoid[F[(L, A)]]

def empty: WriterT[F, L, A] = WriterT(F0.empty)
}

trait WriterTFunctions {
def putT[F[_], L, V](vf: F[V])(l: L)(implicit functorF: Functor[F]): WriterT[F, L, V] =
WriterT(functorF.map(vf)(v => (l, v)))
Expand Down
22 changes: 22 additions & 0 deletions tests/src/test/scala/cats/tests/WriterTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,26 @@ class WriterTTests extends CatsSuite {
checkAll("WriterT[ListWrapper, ListWrapper[Int], ?]", MonadCombineTests[WriterT[ListWrapper, ListWrapper[Int], ?]].monadCombine[Int, Int, Int])
checkAll("MonadCombine[WriterT[ListWrapper, ListWrapper[Int], ?]]", SerializableTests.serializable(MonadCombine[WriterT[ListWrapper, ListWrapper[Int], ?]]))
}

{
// F[(L, V)] has a monoid
implicit val FLV: Monoid[ListWrapper[(Int, Int)]] = ListWrapper.monoid[(Int, Int)]

Monoid[WriterT[ListWrapper, Int, Int]]
Semigroup[WriterT[ListWrapper, Int, Int]]
checkAll("WriterT[ListWrapper, Int, Int]", kernel.laws.GroupLaws[WriterT[ListWrapper, Int, Int]].monoid)

Monoid[WriterT[Id, Int, Int]]
Semigroup[WriterT[Id, Int, Int]]
}

{
// F[(L, V)] has a semigroup
implicit val FLV: Semigroup[ListWrapper[(Int, Int)]] = ListWrapper.semigroup[(Int, Int)]

Semigroup[WriterT[ListWrapper, Int, Int]]
checkAll("WriterT[ListWrapper, Int, Int]", kernel.laws.GroupLaws[WriterT[ListWrapper, Int, Int]].semigroup)

Semigroup[WriterT[Id, Int, Int]]
}
}