Skip to content

Commit

Permalink
Merge pull request #1014 from yilinwei/master
Browse files Browse the repository at this point in the history
Adding WriterT Monoid
  • Loading branch information
adelbertc committed May 13, 2016
2 parents 53dc641 + 30cf9df commit c3a2326
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
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]]
}
}

0 comments on commit c3a2326

Please sign in to comment.