Skip to content

Commit

Permalink
Merge pull request #928 from matt-martin/ISSUE-607
Browse files Browse the repository at this point in the history
#607: Add missing Show instances
  • Loading branch information
stew committed Mar 16, 2016
2 parents 8edf0a3 + 42a0583 commit d1b87a6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/WriterT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ final case class WriterT[F[_], L, V](run: F[(L, V)]) {

def reset(implicit monoidL: Monoid[L], functorF: Functor[F]): WriterT[F, L, V] =
mapWritten(_ => monoidL.empty)

def show(implicit F: Show[F[(L, V)]]): String = F.show(run)
}
object WriterT extends WriterTInstances with WriterTFunctions

Expand All @@ -68,6 +70,10 @@ private[data] sealed abstract class WriterTInstances extends WriterTInstances0 {
def liftT[A](ma: M[A]): WriterT[M, W, A] =
WriterT(M.map(ma)((W.empty, _)))
}

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
}
}

private[data] sealed abstract class WriterTInstances0 extends WriterTInstances1 {
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/std/tuple.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ sealed trait Tuple2Instances {
def bifoldRight[A, B, C](fab: (A, B), c: Eval[C])(f: (A, Eval[C]) => Eval[C], g: (B, Eval[C]) => Eval[C]): Eval[C] =
g(fab._2, f(fab._1, c))
}

implicit def tuple2Show[A, B](implicit aShow: Show[A], bShow: Show[B]): Show[(A, B)] = new Show[(A, B)] {
override def show(f: (A, B)): String = {
s"(${aShow.show(f._1)},${bShow.show(f._2)})"
}
}
}
23 changes: 23 additions & 0 deletions tests/src/test/scala/cats/tests/TupleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,27 @@ import cats.laws.discipline.eq.tuple2Eq
class TupleTests extends CatsSuite {
checkAll("Tuple2", BitraverseTests[Tuple2].bitraverse[Option, Int, Int, Int, String, String, String])
checkAll("Bitraverse[Tuple2]", SerializableTests.serializable(Bitraverse[Tuple2]))

test("show") {
(1, 2).show should === ("(1,2)")

forAll { fs: (String, String) =>
fs.show should === (fs.toString)
}

// Provide some "non-standard" Show instances to make sure the tuple2 is actually use the Show instances for the
// relevant types instead of blindly calling toString
case class Foo(x: Int)
implicit val fooShow: Show[Foo] = new Show[Foo] {
override def show(f: Foo): String = s"foo.x = ${f.x}"
}
case class Bar(y: Int)
implicit val barShow: Show[Bar] = new Show[Bar] {
override def show(f: Bar): String = s"bar.y = ${f.y}"
}

val foo = Foo(1)
val bar = Bar(2)
(foo, bar).show should === (s"(${fooShow.show(foo)},${barShow.show(bar)})")
}
}
5 changes: 5 additions & 0 deletions tests/src/test/scala/cats/tests/WriterTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class WriterTTests extends CatsSuite {
}
}

test("show") {
val writerT: WriterT[Id, List[String], String] = WriterT.put("foo")(List("Some log message"))
writerT.show should === ("(List(Some log message),foo)")
}

{
// F has a SemigroupK
implicit val F: SemigroupK[ListWrapper] = ListWrapper.semigroupK
Expand Down

0 comments on commit d1b87a6

Please sign in to comment.