Skip to content

Commit

Permalink
Vector-based optimized traverse and traverseFilter in the same commit
Browse files Browse the repository at this point in the history
for the sake of benchmarking
  • Loading branch information
TimWSpence committed Dec 19, 2023
1 parent 2d5f4d7 commit 702ab8b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/Traverse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ object Traverse {

private[cats] def traverseDirectly[G[_], A, B](
fa: IterableOnce[A]
)(f: A => G[B])(implicit G: StackSafeMonad[G]): G[Chain[B]] = {
fa.iterator.foldLeft(G.pure(Chain.empty[B])) { case (accG, a) =>
)(f: A => G[B])(implicit G: StackSafeMonad[G]): G[Vector[B]] = {
fa.iterator.foldLeft(G.pure(Vector.empty[B])) { case (accG, a) =>
G.map2(accG, f(a)) { case (acc, x) =>
acc :+ x
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/TraverseFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ object TraverseFilter {

private[cats] def traverseFilterDirectly[G[_], A, B](
fa: IterableOnce[A]
)(f: A => G[Option[B]])(implicit G: StackSafeMonad[G]): G[Chain[B]] = {
fa.iterator.foldLeft(G.pure(Chain.empty[B])) { case (bldrG, a) =>
)(f: A => G[Option[B]])(implicit G: StackSafeMonad[G]): G[Vector[B]] = {
fa.iterator.foldLeft(G.pure(Vector.empty[B])) { case (bldrG, a) =>
G.map2(bldrG, f(a)) {
case (acc, Some(b)) => acc :+ b
case (acc, None) => acc
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {
else
G match {
case x: StackSafeMonad[G] =>
Traverse.traverseDirectly(fa.iterator)(f)(x)
x.map(Traverse.traverseDirectly(fa.iterator)(f)(x))(fromIterableOnce(_))
case _ =>
traverseViaChain {
val as = collection.mutable.ArrayBuffer[A]()
Expand Down Expand Up @@ -1355,7 +1355,7 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {
}

implicit val catsDataTraverseFilterForChain: TraverseFilter[Chain] = new TraverseFilter[Chain] {
def traverse: Traverse[Chain] = Chain.catsDataInstancesForChain
def traverse: Traverse[Chain] with Alternative[Chain] = Chain.catsDataInstancesForChain

override def filter[A](fa: Chain[A])(f: A => Boolean): Chain[A] = fa.filter(f)

Expand All @@ -1372,7 +1372,7 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {
else
G match {
case x: StackSafeMonad[G] =>
TraverseFilter.traverseFilterDirectly(fa.iterator)(f)(x)
x.map(TraverseFilter.traverseFilterDirectly(fa.iterator)(f)(x))(traverse.fromIterableOnce(_))
case _ =>
traverseFilterViaChain {
val as = collection.mutable.ArrayBuffer[A]()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/vector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private[instances] trait VectorInstancesBinCompat0 {

def traverseFilter[G[_], A, B](fa: Vector[A])(f: (A) => G[Option[B]])(implicit G: Applicative[G]): G[Vector[B]] =
G match {
case x: StackSafeMonad[G] => x.map(TraverseFilter.traverseFilterDirectly(fa)(f)(x))(_.toVector)
case x: StackSafeMonad[G] => TraverseFilter.traverseFilterDirectly(fa)(f)(x)
case _ =>
G.map(Chain.traverseFilterViaChain(fa)(f))(_.toVector)
}
Expand Down

0 comments on commit 702ab8b

Please sign in to comment.