Skip to content

Commit

Permalink
Adds version of mkString_ with no prefix/suffix, matching the std lib.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthughes committed Jan 25, 2019
1 parent 653b56f commit 1c1da2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
24 changes: 23 additions & 1 deletion core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ final class FoldableOps[F[_], A](private val fa: F[A]) extends AnyVal {
A.combine(prefix, A.combine(F.intercalate(fa, delim), suffix))

/**
* Make a string using `Show`, named as `mkString_` to avoid conflict
* Make a string using `Show`, prefix, delimiter, and suffix.
*
* Named as `mkString_` to avoid conflict.
*
* Example:
* {{{
Expand Down Expand Up @@ -204,6 +206,26 @@ final class FoldableOps[F[_], A](private val fa: F[A]) extends AnyVal {

final class FoldableOps0[F[_], A](private val fa: F[A]) extends AnyVal {

/**
* Make a string using `Show` and delimiter.
*
* Named as `mkString_` to avoid conflict.
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> val l: List[Int] = List(1, 2, 3)
* scala> l.mkString_(",")
* res0: String = 1,2,3
* scala> val el: List[Int] = List()
* scala> el.mkString_(",")
* res1: String =
* }}}
*/
def mkString_(delim: String)(implicit A: Show[A], F: Foldable[F]): String =
new FoldableOps(fa).mkString_("", delim, "")

/**
* Fold implemented by mapping `A` values into `B` in a context `G` and then
* combining them using the `MonoidK[G]` instance.
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}
}

test(s"Foldable[$name] mkString_ delimiter only") {
forAll { (fa: F[Int]) =>
fa.mkString_(",") should ===(fa.toList.mkString(","))
}
}

test(s"Foldable[$name].collectFirstSomeM") {
forAll { (fa: F[Int], n: Int) =>
fa.collectFirstSomeM(x => (x > n).guard[Option].as(x).asRight[String]) should ===(
Expand Down

0 comments on commit 1c1da2a

Please sign in to comment.