Skip to content

Commit

Permalink
Remove mutable builder from Map traverse as its unlawful
Browse files Browse the repository at this point in the history
  • Loading branch information
TimWSpence committed Mar 7, 2024
1 parent 803107d commit 0ee49e6
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions core/src/main/scala/cats/instances/map.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,9 @@ trait MapInstances extends cats.kernel.instances.MapInstances {
else
G match {
case x: StackSafeMonad[G] =>
x.map(fa.foldLeft(G.pure(Map.newBuilder[K, B])) { case (accG, (k, a)) =>
x.flatMap(accG) { acc =>
G.map(f(a)) { a =>
acc += k -> a
acc
}
}
})(_.result())
fa.iterator.foldLeft(G.pure(Map.empty[K, B])) { case (accG, (k, a)) =>
x.map2(accG, f(a)) { case (acc, b) => acc + (k -> b) }
}
case _ =>
G.map(Chain.traverseViaChain(fa.toIndexedSeq) { case (k, a) =>
G.map(f(a))((k, _))
Expand Down

0 comments on commit 0ee49e6

Please sign in to comment.