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

Adds version of mkString_ with no prefix/suffix, matching the std lib. #2709

Merged
merged 4 commits into from
Jan 31, 2019
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
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
2 changes: 2 additions & 0 deletions testkit/src/main/scala/cats/tests/CatsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import cats.syntax.{
AllSyntaxBinCompat1,
AllSyntaxBinCompat2,
AllSyntaxBinCompat3,
AllSyntaxBinCompat4,
EqOps
}
import org.scalactic.anyvals.{PosInt, PosZDouble, PosZInt}
Expand Down Expand Up @@ -59,6 +60,7 @@ trait CatsSuite
with AllSyntaxBinCompat1
with AllSyntaxBinCompat2
with AllSyntaxBinCompat3
with AllSyntaxBinCompat4
with StrictCatsEquality { self: FunSuiteLike =>

implicit override val generatorDrivenConfig: PropertyCheckConfiguration =
Expand Down
10 changes: 6 additions & 4 deletions tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}

test("Foldable#partitionEitherM retains size") {
import cats.syntax.foldable._
forAll { (fi: F[Int], f: Int => Either[String, String]) =>
val vector = Foldable[F].toList(fi).toVector
val result = Foldable[Vector].partitionEitherM(vector)(f.andThen(Option.apply)).map {
Expand All @@ -91,7 +90,6 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}

test("Foldable#partitionEitherM consistent with List#partition") {
import cats.syntax.foldable._
forAll { (fi: F[Int], f: Int => Either[String, String]) =>
val list = Foldable[F].toList(fi)
val partitioned = Foldable[List].partitionEitherM(list)(f.andThen(Option.apply))
Expand All @@ -108,7 +106,6 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}

test("Foldable#partitionEitherM to one side is identity") {
import cats.syntax.foldable._
forAll { (fi: F[Int], f: Int => String) =>
val list = Foldable[F].toList(fi)
val g: Int => Option[Either[Double, String]] = f.andThen(Right.apply).andThen(Option.apply)
Expand All @@ -123,7 +120,6 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}

test("Foldable#partitionEitherM remains sorted") {
import cats.syntax.foldable._
forAll { (fi: F[Int], f: Int => Either[String, String]) =>
val list = Foldable[F].toList(fi)

Expand Down Expand Up @@ -222,6 +218,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