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

Order for writert #3556

Merged
merged 1 commit into from
Aug 12, 2020
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
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)
Comment on lines +324 to +325
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a normal thing to add directly to the type? I actually don't know what our idiom is here.

}

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