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[List] #601

Merged
merged 2 commits into from
Nov 8, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/std/list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import algebra.std.{ListMonoid, ListOrder}

import cats.data.Streaming
import cats.syntax.order._
import cats.syntax.show._

import scala.annotation.tailrec
import scala.collection.mutable.ListBuffer
Expand Down Expand Up @@ -70,6 +71,11 @@ trait ListInstances extends ListInstances1 {

implicit def listAlgebra[A]: Monoid[List[A]] = new ListMonoid[A]
implicit def listOrder[A: Order]: Order[List[A]] = new ListOrder[A]

implicit def listShow[A:Show]: Show[List[A]] =
new Show[List[A]] {
def show(fa: List[A]): String = s"List(${fa.map(_.show).mkString(", ")})"
Copy link
Member

Choose a reason for hiding this comment

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

If you're already using mkString: fa.map(_.show).mkString("List(", ", ", ")")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I forgot you could do that. Thanks will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To save you a comment: I'll do that in the Set PR too :)

}
}

trait ListInstances1 extends ListInstances2 {
Expand Down
8 changes: 8 additions & 0 deletions tests/src/test/scala/cats/tests/ListTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,12 @@ class ListTests extends CatsSuite {
test("toNel on empty list returns None"){
List.empty[Int].toNel should === (None)
}

test("show"){
List(1, 2, 3).show should === ("List(1, 2, 3)")
(Nil: List[Int]).show should === ("List()")
forAll { l: List[String] =>
l.show should === (l.toString)
}
}
}