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 contains_, foldSmash and mkString_ to FoldableOps #2123

Merged
merged 9 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from 7 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
59 changes: 59 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,63 @@ final class FoldableOps[F[_], A](val fa: F[A]) extends AnyVal {

def foldr[B](b: Eval[B])(f: (A, Eval[B]) => Eval[B])(implicit F: Foldable[F]): Eval[B] =
F.foldRight(fa, b)(f)

/**
* test if `F[A]` contains an `A`, named contains_ to avoid conflict with existing contains which uses universal equality
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> val l: List[Int] = List(1, 2, 3, 4)
* scala> l.contains_(1)
* res0: Boolean = true
* scala> l.contains_(5)
* res1: Boolean = false
* }}}
*/
def contains_(v: A)(implicit ev: Eq[A], F: Foldable[F]): Boolean =
F.exists(fa)(ev.eqv(_, v))

/**
* Intercalate with a prefix and a suffix
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> val l: List[String] = List("1", "2", "3")
* scala> l.foldSmash("List(", ",", ")")
* res0: String = List(1,2,3)
* }}}
*/
def foldSmash(prefix: A, delim: A, suffix: A)(implicit A: Monoid[A], F: Foldable[F]): A =
A.combine(prefix, A.combine(F.intercalate(fa, delim), suffix))

/**
* Make a string using `Show`, named as `mkString_` to avoid conflict
*
* Example:
* {{{
* scala> import cats.implicits._
*
* scala> val l: List[Int] = List(1, 2, 3)
* scala> l.mkString_("L[", ";", "]")
* res0: String = L[1;2;3]
* scala> val el: List[Int] = List()
* scala> el.mkString_("L[", ";", "]")
* res1: String = L[]
* }}}
*/
def mkString_(prefix: String, delim: String, suffix: String)(implicit A: Show[A], F: Foldable[F]): String = {
val b = F.foldLeft(fa, new StringBuilder){ (builder, a) =>
builder append A.show(a) append delim
}
prefix + {
if (b.isEmpty)
""
else
b.toString.dropRight(delim.length)
} + suffix
}
}
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 @@ -140,6 +140,12 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(
fa.toList should === (iterator(fa).toList)
}
}

test(s"Foldable[$name] mkString_") {
forAll { (fa: F[Int]) =>
fa.mkString_("L[", ";", "]") should === fa.mkString("L[", ";", "]")
Copy link
Contributor

Choose a reason for hiding this comment

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

now is the right hand side working? don't we need a toList or something?

Copy link
Contributor

Choose a reason for hiding this comment

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

good catch. Since this is not my repo I can only edit files through github and my mental compiler is clearly not good enough.

}
}
}

class FoldableSuiteAdditional extends CatsSuite {
Expand Down