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 existsM and forallM to Foldable #1784

Merged
merged 2 commits into from
Aug 7, 2017
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
50 changes: 50 additions & 0 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,56 @@ import simulacrum.typeclass
if (p(a)) lb else Eval.False
}.value

/**
* Check whether at least one element satisfies the predicate.
*
* If there are no elements, the result is `false`.
*
* All effects are sequenced, i.e. later effects aren't skipped even if a `true` is encountered.
*
* For example:
*
* {{{
* scala> import cats.implicits._
* scala> val F = Foldable[List]
* scala> F.existsM(List(1,2,3,4))(n => Option(n <= 4))
* res0: Option[Boolean] = Some(true)
*
* scala> F.existsM(List(1,2,3,4))(n => if (n <= 2) Option(true) else Option(false))
* res1: Option[Boolean] = Some(true)
Copy link
Contributor

Choose a reason for hiding this comment

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

might be better to have a negative test case here, e.g.

   * scala> F.existsM(List(1,2,3,4))(n => if (n > 4) Option(true) else Option(false))
   * res1: Option[Boolean] = Some(false)

*
* scala> F.existsM(List(1,2,3,4))(n => if (n <= 2) Option(true) else None)
* res2: Option[Boolean] = None
* }}}
*/
def existsM[G[_], A](fa: F[A])(p: A => G[Boolean])(implicit G: Monad[G]): G[Boolean] =
foldM(fa, false)((z, a) => G.map(p(a))(b => b || z))

/**
* Check whether all elements satisfy the predicate.
*
* If there are no elements, the result is `true`.
*
* All effects are sequenced, i.e. later effects aren't skipped even if a `false` is encountered.
*
* For example:
*
* {{{
* scala> import cats.implicits._
* scala> val F = Foldable[List]
* scala> F.forallM(List(1,2,3,4))(n => Option(n <= 4))
* res0: Option[Boolean] = Some(true)
*
* scala> F.forallM(List(1,2,3,4))(n => if (n <= 2) Option(true) else Option(false))
* res1: Option[Boolean] = Some(false)
*
* scala> F.forallM(List(1,2,3,4))(n => if (n <= 2) Option(false) else None)
* res2: Option[Boolean] = None
* }}}
*/
def forallM[G[_], A](fa: F[A])(p: A => G[Boolean])(implicit G: Monad[G]): G[Boolean] =
foldM(fa, true)((z, a) => G.map(p(a))(b => b && z))

/**
* Convert F[A] to a List[A].
*/
Expand Down
4 changes: 3 additions & 1 deletion tests/src/test/scala/cats/tests/FoldableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ abstract class FoldableCheck[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}
}

test(s"Foldable[$name].find/exists/forall/filter_/dropWhile_") {
test(s"Foldable[$name].find/exists/forall/existsM/forallM/filter_/dropWhile_") {
forAll { (fa: F[Int], n: Int) =>
fa.find(_ > n) should === (iterator(fa).find(_ > n))
fa.exists(_ > n) should === (iterator(fa).exists(_ > n))
fa.forall(_ > n) should === (iterator(fa).forall(_ > n))
fa.existsM(k => Option(k > n)) should === (Option(iterator(fa).exists(_ > n)))
fa.forallM(k => Option(k > n)) should === (Option(iterator(fa).forall(_ > n)))
fa.filter_(_ > n) should === (iterator(fa).filter(_ > n).toList)
fa.dropWhile_(_ > n) should === (iterator(fa).dropWhile(_ > n).toList)
fa.takeWhile_(_ > n) should === (iterator(fa).takeWhile(_ > n).toList)
Expand Down