Skip to content

Commit

Permalink
Merge pull request #2 from valencik/more-chain
Browse files Browse the repository at this point in the history
Use `Chain` in `traverseDirectly` helpers
  • Loading branch information
TimWSpence authored Mar 26, 2024
2 parents 0ee49e6 + a62ce4c commit 6cb787d
Show file tree
Hide file tree
Showing 5 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[Vector[B]] = {
fa.iterator.foldLeft(G.pure(Vector.empty[B])) { case (accG, a) =>
)(f: A => G[B])(implicit G: StackSafeMonad[G]): G[Chain[B]] = {
fa.iterator.foldLeft(G.pure(Chain.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[Vector[B]] = {
fa.iterator.foldLeft(G.pure(Vector.empty[B])) { case (bldrG, a) =>
)(f: A => G[Option[B]])(implicit G: StackSafeMonad[G]): G[Chain[B]] = {
fa.iterator.foldLeft(G.pure(Chain.empty[B])) { case (bldrG, a) =>
G.map2(bldrG, f(a)) {
case (acc, Some(b)) => acc :+ b
case (acc, None) => acc
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {
else
G match {
case x: StackSafeMonad[G] =>
x.map(Traverse.traverseDirectly(fa.iterator)(f)(x))(fromIterableOnce(_))
Traverse.traverseDirectly(fa.iterator)(f)(x)
case _ =>
traverseViaChain {
val as = collection.mutable.ArrayBuffer[A]()
Expand Down Expand Up @@ -1374,7 +1374,7 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {
else
G match {
case x: StackSafeMonad[G] =>
x.map(TraverseFilter.traverseFilterDirectly(fa.iterator)(f)(x))(traverse.fromIterableOnce(_))
TraverseFilter.traverseFilterDirectly(fa.iterator)(f)(x)
case _ =>
traverseFilterViaChain {
val as = collection.mutable.ArrayBuffer[A]()
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/instances/seq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ trait SeqInstances extends cats.kernel.instances.SeqInstances {
case x: StackSafeMonad[G] =>
x.map(Traverse.traverseDirectly(fa)(f)(x))(_.toList)
case _ =>
G.map(Chain.traverseViaChain(fa.toIndexedSeq)(f))(_.toVector)
G.map(Chain.traverseViaChain(fa.toIndexedSeq)(f))(_.toList)
}

override def traverse_[G[_], A, B](fa: Seq[A])(f: A => G[B])(implicit G: Applicative[G]): G[Unit] =
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] => TraverseFilter.traverseFilterDirectly(fa)(f)(x)
case x: StackSafeMonad[G] => x.map(TraverseFilter.traverseFilterDirectly(fa)(f)(x))(_.toVector)
case _ =>
G.map(Chain.traverseFilterViaChain(fa)(f))(_.toVector)
}
Expand Down

0 comments on commit 6cb787d

Please sign in to comment.