Skip to content
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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions core/src/main/scala/cats/instances/queue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor Author

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.

[error]  * abstract synthetic method cats$instances$QueueInstances$_setter_$catsStdTraverseFilterForQueue_=(cats.TraverseFilter)Unit in interface cats.instances.QueueInstances is present only in current version
[error]    filter with: ProblemFilters.exclude[ReversedMissingMethodProblem]("cats.instances.QueueInstances.cats$instances$QueueInstances$_setter_$catsStdTraverseFilterForQueue_=")
[error]  * abstract method catsStdTraverseFilterForQueue()cats.TraverseFilter in interface cats.instances.QueueInstances is present only in current version
[error]    filter with: ProblemFilters.exclude[ReversedMissingMethodProblem]("cats.instances.QueueInstances.catsStdTraverseFilterForQueue")

}

private object QueueInstances {
private val catsStdTraverseFilterForQueue: TraverseFilter[Queue] = new TraverseFilter[Queue] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an instance cache.

Copy link
Contributor

@kailuowang kailuowang Oct 19, 2019

Choose a reason for hiding this comment

The 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 val instead of creating a new object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get an error that

[error]  * abstract synthetic method cats$instances$QueueInstances$_setter_$cats$instances$QueueInstances$$_catsStdTraverseFilterForQueue_=(cats.TraverseFilter)Unit in interface cats.instances.QueueInstances is present only in current version
[error]    filter with: ProblemFilters.exclude[ReversedMissingMethodProblem]("cats.instances.QueueInstances.cats$instances$QueueInstances$_setter_$cats$instances$QueueInstances$$_catsStdTraverseFilterForQueue_=")
[error]  * abstract synthetic method cats$instances$QueueInstances$$_catsStdTraverseFilterForQueue()cats.TraverseFilter in interface cats.instances.QueueInstances is present only in current version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how did you run the check? did you get that error from sbt validateBC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I ran sbt validateBC.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a val to a trait will break bincompat on all Scala versions so far because of the synthetic setter that's necessary because of the way trait initialization works. The workaround here looks reasonable to me.

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
}
}
5 changes: 4 additions & 1 deletion tests/src/test/scala/cats/tests/QueueSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package cats
package tests

import scala.collection.immutable.Queue

import cats.laws.discipline.{
AlternativeTests,
CoflatMapTests,
MonadTests,
SemigroupalTests,
SerializableTests,
TraverseFilterTests,
TraverseTests
}

Expand All @@ -28,6 +28,9 @@ class QueueSuite extends CatsSuite {
checkAll("Queue[Int] with Option", TraverseTests[Queue].traverse[Int, Int, Int, Set[Int], Option, Option])
checkAll("Traverse[Queue]", SerializableTests.serializable(Traverse[Queue]))

checkAll("Queue[Int]", TraverseFilterTests[Queue].traverseFilter[Int, Int, Int])
checkAll("TraverseFilter[Queue]", SerializableTests.serializable(TraverseFilter[Queue]))

test("show") {
Queue(1, 2, 3).show should ===("Queue(1, 2, 3)")
Queue.empty[Int].show should ===("Queue()")
Expand Down