Skip to content

Commit

Permalink
Merge pull request #3556 from TimWSpence/order-for-writert
Browse files Browse the repository at this point in the history
Order for writert
  • Loading branch information
djspiewak authored Aug 12, 2020
2 parents bb8baa7 + eacd388 commit 0640b67
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ final case class WriterT[F[_], L, V](run: F[(L, V)]) {
G.map(
F.traverse(run)(lv => G.tupleLeft(f(lv._2), lv._1))
)(WriterT.apply)

def compare(that: WriterT[F, L, V])(implicit Ord: Order[F[(L, V)]]): Int =
Ord.compare(run, that.run)
}

object WriterT extends WriterTInstances with WriterTFunctions with WriterTFunctions0 {
Expand Down Expand Up @@ -377,6 +380,11 @@ sealed abstract private[data] class WriterTInstances1 extends WriterTInstances2

implicit def catsDataFoldableForWriterTId[L](implicit F: Foldable[Id]): Foldable[WriterT[Id, L, *]] =
catsDataFoldableForWriterT[Id, L](F)

implicit def catsDataOrderForWriterT[F[_], L, V](implicit Ord: Order[F[(L, V)]]): Order[WriterT[F, L, V]] =
new Order[WriterT[F, L, V]] {
def compare(x: WriterT[F, L, V], y: WriterT[F, L, V]): Int = x.compare(y)
}
}

sealed abstract private[data] class WriterTInstances2 extends WriterTInstances3 {
Expand Down
15 changes: 15 additions & 0 deletions tests/src/test/scala/cats/tests/WriterTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import cats.syntax.option._
import cats.syntax.eq._
import org.scalacheck.Prop._
import org.scalacheck.Test.Parameters
import cats.kernel.laws.discipline.OrderTests

class WriterTSuite extends CatsSuite {
type Logged[A] = Writer[ListWrapper[Int], A]
Expand Down Expand Up @@ -386,6 +387,20 @@ class WriterTSuite extends CatsSuite {
checkAll("Writer[Int, Int]", SemigroupTests[Writer[Int, Int]].semigroup)
}

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

Order[WriterT[ListWrapper, Int, Int]]
checkAll("WriterT[ListWrapper, Int, Int]", OrderTests[WriterT[ListWrapper, Int, Int]].order)
checkAll("Order[WriterT[ListWrapper, Int, Int]]",
SerializableTests.serializable(Order[WriterT[ListWrapper, Int, Int]])
)

Order[Writer[Int, Int]]
checkAll("Writer[Int, Int]", OrderTests[Writer[Int, Int]].order)
}

{
// F has an Applicative and L has a Monoid
implicit val L: Monoid[ListWrapper[Int]] = ListWrapper.monoid[Int]
Expand Down

0 comments on commit 0640b67

Please sign in to comment.