From 8cd81bed92e9a699b63db2b78a7a094294bd3d9d Mon Sep 17 00:00:00 2001 From: Cody Allen Date: Fri, 6 Nov 2015 08:57:44 -0500 Subject: [PATCH 1/2] Add Show[Set] --- core/src/main/scala/cats/std/set.scala | 9 +++++++++ tests/src/test/scala/cats/tests/SetTests.scala | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/core/src/main/scala/cats/std/set.scala b/core/src/main/scala/cats/std/set.scala index 0f759ad795..ab5a5cbc2e 100644 --- a/core/src/main/scala/cats/std/set.scala +++ b/core/src/main/scala/cats/std/set.scala @@ -1,6 +1,8 @@ package cats package std +import cats.syntax.show._ + trait SetInstances extends algebra.std.SetInstances { implicit val setInstance: Foldable[Set] with MonoidK[Set] = new Foldable[Set] with MonoidK[Set] { @@ -25,4 +27,11 @@ trait SetInstances extends algebra.std.SetInstances { } implicit def setMonoid[A]: Monoid[Set[A]] = MonoidK[Set].algebra[A] + + implicit def setShow[A:Show]: Show[Set[A]] = new Show[Set[A]] { + def show(fa: Set[A]): String = { + val elements = fa.toIterator.map(_.show).mkString(", ") + s"Set($elements)" + } + } } diff --git a/tests/src/test/scala/cats/tests/SetTests.scala b/tests/src/test/scala/cats/tests/SetTests.scala index fc5a29541b..571b238b0b 100644 --- a/tests/src/test/scala/cats/tests/SetTests.scala +++ b/tests/src/test/scala/cats/tests/SetTests.scala @@ -11,4 +11,21 @@ class SetTests extends CatsSuite { checkAll("Set[Int]", FoldableTests[Set].foldable[Int, Int]) checkAll("Foldable[Set]", SerializableTests.serializable(Foldable[Set])) + + test("show"){ + Set(1, 1, 2, 3).show should === ("Set(1, 2, 3)") + Set.empty[String].show should === ("Set()") + + forAll { fs: Set[String] => + fs.show should === (fs.toString) + } + } + + test("show keeps separate entries for items that map to identical strings"){ + implicit val intShow: Show[Int] = Show.show(_ => "1") + // an implementation implemented as set.map(_.show).mkString(", ") would + // only show one entry in the result instead of 3, because Set.map combines + // duplicate items in the codomain. + Set(1, 2, 3).show should === ("Set(1, 1, 1)") + } } From 736eb84d63c5076c90f7aec9dbc6d368a83b6cab Mon Sep 17 00:00:00 2001 From: Cody Allen Date: Fri, 6 Nov 2015 09:07:02 -0500 Subject: [PATCH 2/2] Clean up show implementation for Set --- core/src/main/scala/cats/std/set.scala | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/src/main/scala/cats/std/set.scala b/core/src/main/scala/cats/std/set.scala index ab5a5cbc2e..1efeff8350 100644 --- a/core/src/main/scala/cats/std/set.scala +++ b/core/src/main/scala/cats/std/set.scala @@ -29,9 +29,7 @@ trait SetInstances extends algebra.std.SetInstances { implicit def setMonoid[A]: Monoid[Set[A]] = MonoidK[Set].algebra[A] implicit def setShow[A:Show]: Show[Set[A]] = new Show[Set[A]] { - def show(fa: Set[A]): String = { - val elements = fa.toIterator.map(_.show).mkString(", ") - s"Set($elements)" - } + def show(fa: Set[A]): String = + fa.toIterator.map(_.show).mkString("Set(", ", ", ")") } }