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

TraverseFilter won't short circuit #3327

Closed
gagandeepkalra opened this issue Feb 28, 2020 · 2 comments · Fixed by #3328
Closed

TraverseFilter won't short circuit #3327

gagandeepkalra opened this issue Feb 28, 2020 · 2 comments · Fixed by #3328

Comments

@gagandeepkalra
Copy link
Contributor

TraverseFilter won't short-circuit for Set, LazyList, Vector and Queue. For other instances, it's done correctly.

e.g.

import cats.implicits._
import scala.collection.immutable.Queue

scala> val f: Int => Either[String, Option[Int]] = i => {
  print(s"$i ")
  if (i > 0) Right(Some(i)) else Left("not positive")
}

scala> List(2, 1, 0, -1).traverseFilter(f)
2 1 0 res0: Either[String,List[Int]] = Left(not positive)

scala> Vector(2, 1, 0, -1).traverseFilter(f)
-1 0 1 2 res1: Either[String,scala.collection.immutable.Vector[Int]] = Left(not positive)

scala> Queue(2, 1, 0, -1).traverseFilter(f)
-1 0 1 2 res2: Either[String,scala.collection.immutable.Queue[Int]] = Left(not positive)

Conversation for reference.

@gagandeepkalra gagandeepkalra changed the title TraverseFilter won't short-circuit TraverseFilter won't short circuit Feb 28, 2020
@travisbrown
Copy link
Contributor

Thanks for opening this, @gagandeepkalra! I'm planning to take a closer look at this in the next few days.

@travisbrown
Copy link
Contributor

One thing that's worth noting here: the bug doesn't extend to laziness of effect evaluation, in which respect (as far as I've checked) all of these instances are correct. The issue is that for strict types with a failed state (like Either) the function is applied in cases where it doesn't need to be, and where it isn't for e.g. the List instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants