From 702ab8b9d1ebad207bb7c97eb671da333e667ea0 Mon Sep 17 00:00:00 2001 From: Tim Spence Date: Tue, 19 Dec 2023 11:50:56 +0000 Subject: [PATCH] Vector-based optimized traverse and traverseFilter in the same commit for the sake of benchmarking --- core/src/main/scala/cats/Traverse.scala | 4 ++-- core/src/main/scala/cats/TraverseFilter.scala | 4 ++-- core/src/main/scala/cats/data/Chain.scala | 6 +++--- core/src/main/scala/cats/instances/vector.scala | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/scala/cats/Traverse.scala b/core/src/main/scala/cats/Traverse.scala index cc2993ee69..f1856fdc53 100644 --- a/core/src/main/scala/cats/Traverse.scala +++ b/core/src/main/scala/cats/Traverse.scala @@ -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 } diff --git a/core/src/main/scala/cats/TraverseFilter.scala b/core/src/main/scala/cats/TraverseFilter.scala index 0907ef3be7..25521b35f2 100644 --- a/core/src/main/scala/cats/TraverseFilter.scala +++ b/core/src/main/scala/cats/TraverseFilter.scala @@ -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 diff --git a/core/src/main/scala/cats/data/Chain.scala b/core/src/main/scala/cats/data/Chain.scala index e113cae684..721cc66a50 100644 --- a/core/src/main/scala/cats/data/Chain.scala +++ b/core/src/main/scala/cats/data/Chain.scala @@ -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]() @@ -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) @@ -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]() diff --git a/core/src/main/scala/cats/instances/vector.scala b/core/src/main/scala/cats/instances/vector.scala index 06f93b4fef..b927ede1f1 100644 --- a/core/src/main/scala/cats/instances/vector.scala +++ b/core/src/main/scala/cats/instances/vector.scala @@ -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) }