Skip to content

Commit

Permalink
backported #3015, updated traverseFilter implementation for List to…
Browse files Browse the repository at this point in the history
… use `foldRight` from Foldable
  • Loading branch information
gagandeepkalra authored and travisbrown committed Mar 11, 2020
1 parent 9bac921 commit 6a02aaa
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions core/src/main/scala/cats/instances/list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,15 @@ private[instances] trait ListInstancesBinCompat0 {
override def flattenOption[A](fa: List[Option[A]]): List[A] = fa.flatten

def traverseFilter[G[_], A, B](fa: List[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[List[B]] =
fa.foldRight(Eval.now(G.pure(List.empty[B])))(
traverse
.foldRight(fa, Eval.now(G.pure(List.empty[B])))(
(x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ :: o))
)
.value

override def filterA[G[_], A](fa: List[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[List[A]] =
fa.foldRight(Eval.now(G.pure(List.empty[A])))(
traverse
.foldRight(fa, Eval.now(G.pure(List.empty[A])))(
(x, xse) => G.map2Eval(f(x), xse)((b, list) => if (b) x :: list else list)
)
.value
Expand Down

0 comments on commit 6a02aaa

Please sign in to comment.