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

Implement NonEmptyList#mkShowString methods #1518

Closed
wants to merge 1 commit into from
Closed
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
56 changes: 56 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,62 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {

override def toString: String = s"NonEmpty$toList"

/**
* Displays all elements of this `NonEmptyList` in a string
* without any separation while using the implicit `Show` implementation
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> import cats.instances.int._
* scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
* scala> nel.mkString
* res0: String = 12345
* }}}
*/
def mkString[AA >: A](implicit showImpl: Show[AA]) : String = {
val sb = new StringBuilder()
sb ++= showImpl.show(head)
tail.foldLeft(sb)(_ ++= showImpl.show(_))
sb.toString()
}

/**
* Displays all elements of this `NonEmptyList` in a string using a
* separator string, with the implicit `Show` implementation
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> import cats.instances.int._
* scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
* scala> nel.mkString("->")
* res0: String = 1->2->3->4->5
* }}}
*/
def mkString[AA >: A](separator: String)(implicit showImpl: Show[AA]) : String =
tail match {
case Nil => showImpl.show(head)
case _ =>
showImpl.show(head) + tail.foldLeft("")(_ + separator + showImpl.show(_))
}

/**
* Displays all elements of this `NonEmptyList` in a string
* using a start, end and separator strings, with the implicit
* `Show` implementation.
*
* {{{
* scala> import cats.data.NonEmptyList
* scala> import cats.instances.int._
* scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
* scala> nel.mkString("(", ",", ")")
* res0: String = (1,2,3,4,5)
* }}}
*/
def mkString[AA >: A](start: String,
separator: String,
end: String)(implicit showImpl: Show[AA]) : String =
start + mkString[AA](separator)(showImpl) + end

/**
* Remove duplicates. Duplicates are checked using `Order[_]` instance.
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/src/test/scala/cats/tests/NonEmptyListTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ class NonEmptyListTests extends CatsSuite {
}
}

test("NonEmptyList#mkString is consistent with List#mkString") {
forAll { (nel: NonEmptyList[Int]) =>
// Implicitly using a Show[Int] implementation doing `Int.toString` internally
val list = nel.toList
nel.mkString should ===(list.mkString)
}
}

test("NonEmptyList#mkString(String) is consistent with List#mkString(String)") {
forAll { (nel: NonEmptyList[Int], sep: String) =>
// Implicitly using a Show[Int] implementation doing `Int.toString` internally
val list = nel.toList
nel.mkString(sep) should ===(list.mkString(sep))
}
}

test("NonEmptyList#mkString(String, String, String) is consistent with List#mkString(String, String, String)") {
forAll { (nel: NonEmptyList[Int], start: String, separator: String, end: String) =>
// Implicitly using a Show[Int] implementation doing `Int.toString` internally
val list = nel.toList
nel.mkString(start, separator, end) should ===(list.mkString(start, separator, end))
}
}

test("reduceLeft consistent with foldLeft") {
forAll { (nel: NonEmptyList[Int], f: (Int, Int) => Int) =>
nel.reduceLeft(f) should === (nel.tail.foldLeft(nel.head)(f))
Expand Down