Skip to content

Commit

Permalink
Merge pull request #602 from ceedubs/set-show
Browse files Browse the repository at this point in the history
Add Show[Set]
  • Loading branch information
adelbertc committed Nov 8, 2015
2 parents 97a52dd + 736eb84 commit d4125a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/std/set.scala
Original file line number Diff line number Diff line change
@@ -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] {
Expand All @@ -25,4 +27,9 @@ 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 =
fa.toIterator.map(_.show).mkString("Set(", ", ", ")")
}
}
17 changes: 17 additions & 0 deletions tests/src/test/scala/cats/tests/SetTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}

0 comments on commit d4125a5

Please sign in to comment.