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

Add Show[Set] #602

Merged
merged 2 commits into from
Nov 8, 2015
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
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)")
}
}