-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add TraverseFilter.mapAccumulateFilter #4561
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
021f5f8
17389c0
23fa3db
8040c23
cb6d411
277593b
15ec803
9adf4d5
a414be1
35706a7
778aad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we override this for some of the built in collections? State is rather slow so we should avoid it for List, Vector, Chain, NonEmptyList, NonEmptyVector, ...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can copy |
||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.