Skip to content

Commit

Permalink
Add scaladoc for mapAccumulateFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
Masynchin committed Apr 6, 2024
1 parent 35706a7 commit 778aad2
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/src/main/scala/cats/TraverseFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ trait TraverseFilter[F[_]] extends FunctorFilter[F] {
override def mapFilter[A, B](fa: F[A])(f: A => Option[B]): F[B] =
traverseFilter[Id, A, B](fa)(f)

/**
* Like [[mapAccumulate]], but allows `Option` in supplied accumulating function,
* keeping only `Some`s.
*
* Example:
* {{{
* scala> import cats.syntax.all._
* scala> val sumAllAndKeepOdd = (s: Int, n: Int) => (s + n, Option.when(n % 2 == 1)(n))
* scala> List(1, 2, 3, 4).mapAccumulateFilter(0, sumAllAndKeepOdd)
* res1: (Int, List[Int]) = (10, List(1, 3))
* }}}
*/
def mapAccumulateFilter[S, A, B](init: S, fa: F[A])(f: (S, A) => (S, Option[B])): (S, F[B]) =
traverseFilter(fa)(a => State(s => f(s, a))).run(init).value

Expand Down

0 comments on commit 778aad2

Please sign in to comment.