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/fix Foldable extensions: findM and collectFirstSomeM #2421

Merged
merged 4 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 28 additions & 0 deletions core/src/main/scala/cats/syntax/foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,32 @@ final class FoldableOps[F[_], A](val fa: F[A]) extends AnyVal {
case s => G.pure(s)
})
).value

/**
* Find the first element matching the effectful predicate, if one exists.
*
* If there are no elements, the result is `None`. `findM` short-circuits,
* i.e. once an element is found, no further effects are produced.
*
* For example:
* {{{
* scala> import cats.implicits._
* scala> val list = List(1,2,3,4)
* scala> list.findM(n => (n >= 2).asRight[String])
* res0: Either[String,Option[Int]] = Right(Some(2))
*
* scala> list.findM(n => (n > 4).asRight[String])
* res1: Either[String,Option[Int]] = Right(None)
*
* scala> list.findM(n => Either.cond(n < 3, n >= 2, "error"))
* res2: Either[String,Option[Int]] = Right(Some(2))
*
* scala> list.findM(n => Either.cond(n < 3, false, "error"))
* res3: Either[String,Option[Int]] = Left(error)
* }}}
*/
def findM[G[_]](p: A => G[Boolean])(implicit F: Foldable[F], G: Monad[G]): G[Option[A]] =
F.foldRight(fa, Eval.now(G.pure(Option.empty[A])))((a, lb) =>
Eval.now(G.flatMap(p(a))(if (_) G.pure(Some(a)) else lb.value))
).value
Copy link
Contributor

Choose a reason for hiding this comment

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

I’m worried about stack safety here. Note findM, foldM and existsM use tailRecM to be stack safe.

Can we make Source package private so that we can use it here with tailRecM?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I assumed stack-safety is provided by safe flatMap implementation. Will rework via tailRecM.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also added stack-safety tests for other monadic folds.

}
9 changes: 8 additions & 1 deletion tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(
}
}

test(s"Foldable[$name].find/exists/forall/existsM/forallM/filter_/dropWhile_") {
test(s"Foldable[$name].find/exists/forall/findM/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.findM(k => Option(k > n)) should === (Option(iterator(fa).find(_ > 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)
Expand Down Expand Up @@ -324,6 +325,12 @@ class FoldableSuiteAdditional extends CatsSuite {
assert(F.forallM[Id, Boolean](false #:: boom)(identity) == false)
}

test(".findM short-circuiting") {
implicit val F = foldableStreamWithDefaultImpl
def boom: Stream[Int] = sys.error("boom")
assert((1 #:: boom).findM[Id](_ > 0) == Some(1))
}

test("Foldable[List] doesn't break substitution") {
val result = List.range(0,10).foldM(List.empty[Int])((accum, elt) => Eval.always(elt :: accum))

Expand Down