-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 TraverseFilter.mapAccumulateFilter #4561
base: main
Are you sure you want to change the base?
Changes from 2 commits
021f5f8
17389c0
23fa3db
8040c23
cb6d411
277593b
15ec803
9adf4d5
a414be1
35706a7
778aad2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,6 +122,9 @@ 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) | ||
|
||
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 | ||
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, ... 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 |
||
|
||
/** | ||
* Removes duplicate elements from a list, keeping only the first occurrence. | ||
*/ | ||
|
@@ -184,6 +187,8 @@ object TraverseFilter { | |
typeClassInstance.filterA[G, A](self)(f)(G) | ||
def traverseEither[G[_], B, C](f: A => G[Either[C, B]])(g: (A, C) => G[Unit])(implicit G: Monad[G]): G[F[B]] = | ||
typeClassInstance.traverseEither[G, A, B, C](self)(f)(g)(G) | ||
def mapAccumulateFilter[S, B](init: S)(f: (S, A) => (S, Option[B])): (S, F[B]) = | ||
typeClassInstance.mapAccumulateFilter[S, A, B](init, self)(f) | ||
def ordDistinct(implicit O: Order[A]): F[A] = typeClassInstance.ordDistinct(self) | ||
def hashDistinct(implicit H: Hash[A]): F[A] = typeClassInstance.hashDistinct(self) | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -38,6 +38,19 @@ abstract class TraverseFilterSuite[F[_]: TraverseFilter](name: String)(implicit | |||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
implicit def T: Traverse[F] = implicitly[TraverseFilter[F]].traverse | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
test(s"TraverseFilter[$name].mapAccumulateFilter") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. Looks like it does not check the default implementation (based on For testing default implementations we usually use 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 had straightforwardly copy-pasted and updated tests from cats/tests/shared/src/test/scala/cats/tests/TraverseSuite.scala Lines 45 to 56 in 70dbf8f
If that tests doesn't test default implementation too, I can update 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 am failing to understand how to use cats/tests/shared/src/test/scala/cats/tests/ApplicativeSuite.scala Lines 102 to 104 in 70dbf8f
which I can not apply here, because we don't have 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. My apologies for the delay – I was snowed under a bit. Actually, there's cats/testkit/src/main/scala/cats/tests/ListWrapper.scala Lines 80 to 90 in 47fbad7
To test the default implementation you can either call it directly: ListWrapper.traverseFilter.mapAccumulateFilter(...) or make it an implicit in the scope: implicit val listWrapperTraverseFilter: TraverseFilter[ListWrapper] = ListWrapper.traverseFilter And then you can work with |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
forAll { (init: Int, fa: F[Int], fn: ((Int, Int)) => (Int, Option[Int])) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
val lhs = fa.mapAccumulateFilter(init)((s, a) => fn((s, a))) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
val rhs = fa.foldLeft((init, List.empty[Int])) { case ((s1, acc), a) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
val (s2, b) = fn((s1, a)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
(s2, b.fold(acc)(_ :: acc)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
assert(lhs.map(_.toList) === rhs.map(_.reverse)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
test(s"TraverseFilter[$name].ordDistinct") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
forAll { (fa: F[Int]) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
fa.ordDistinct.toList === fa.toList.distinct | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel that a scaladoc comment (perhaps, with a short usage example) wouldn't hurt and could come handy here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost done, I should fix the code violations. Is the language correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks!