Skip to content

Commit 778aad2

Browse files
committed
Add scaladoc for mapAccumulateFilter
1 parent 35706a7 commit 778aad2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

core/src/main/scala/cats/TraverseFilter.scala

+12
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ trait TraverseFilter[F[_]] extends FunctorFilter[F] {
122122
override def mapFilter[A, B](fa: F[A])(f: A => Option[B]): F[B] =
123123
traverseFilter[Id, A, B](fa)(f)
124124

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

0 commit comments

Comments
 (0)