-
-
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 instance for Queue. #3103
Changes from all commits
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 |
---|---|---|
|
@@ -152,4 +152,33 @@ trait QueueInstances extends cats.kernel.instances.QueueInstances { | |
def show(fa: Queue[A]): String = | ||
fa.iterator.map(_.show).mkString("Queue(", ", ", ")") | ||
} | ||
|
||
implicit def catsStdTraverseFilterForQueue: TraverseFilter[Queue] = QueueInstances.catsStdTraverseFilterForQueue | ||
} | ||
|
||
private object QueueInstances { | ||
private val catsStdTraverseFilterForQueue: TraverseFilter[Queue] = new TraverseFilter[Queue] { | ||
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. This is just an instance cache. 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 don't see this pattern as problematic but it's not very consistent with rest of the code base, can we just make the one in the trait a 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 get an error that
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. how did you run the check? did you get that error from 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. Yes, I ran 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. Adding a |
||
val traverse: Traverse[Queue] = cats.instances.queue.catsStdInstancesForQueue | ||
|
||
override def mapFilter[A, B](fa: Queue[A])(f: (A) => Option[B]): Queue[B] = | ||
fa.collect(Function.unlift(f)) | ||
|
||
override def filter[A](fa: Queue[A])(f: (A) => Boolean): Queue[A] = fa.filter(f) | ||
|
||
override def collect[A, B](fa: Queue[A])(f: PartialFunction[A, B]): Queue[B] = fa.collect(f) | ||
|
||
override def flattenOption[A](fa: Queue[Option[A]]): Queue[A] = fa.flatten | ||
|
||
def traverseFilter[G[_], A, B](fa: Queue[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Queue[B]] = | ||
fa.foldRight(Eval.now(G.pure(Queue.empty[B])))( | ||
(x, xse) => G.map2Eval(f(x), xse)((i, o) => i.fold(o)(_ +: o)) | ||
) | ||
.value | ||
|
||
override def filterA[G[_], A](fa: Queue[A])(f: (A) => G[Boolean])(implicit G: Applicative[G]): G[Queue[A]] = | ||
fa.foldRight(Eval.now(G.pure(Queue.empty[A])))( | ||
(x, xse) => G.map2Eval(f(x), xse)((b, vec) => if (b) x +: vec else vec) | ||
) | ||
.value | ||
} | ||
} |
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 don't know why , but
implicit val catsStdTraverseFilterForQueue
breaks binary compatibility.