Skip to content

Commit 0ee49e6

Browse files
committed
Remove mutable builder from Map traverse as its unlawful
1 parent 803107d commit 0ee49e6

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

core/src/main/scala/cats/instances/map.scala

+3-8
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,9 @@ trait MapInstances extends cats.kernel.instances.MapInstances {
4646
else
4747
G match {
4848
case x: StackSafeMonad[G] =>
49-
x.map(fa.foldLeft(G.pure(Map.newBuilder[K, B])) { case (accG, (k, a)) =>
50-
x.flatMap(accG) { acc =>
51-
G.map(f(a)) { a =>
52-
acc += k -> a
53-
acc
54-
}
55-
}
56-
})(_.result())
49+
fa.iterator.foldLeft(G.pure(Map.empty[K, B])) { case (accG, (k, a)) =>
50+
x.map2(accG, f(a)) { case (acc, b) => acc + (k -> b) }
51+
}
5752
case _ =>
5853
G.map(Chain.traverseViaChain(fa.toIndexedSeq) { case (k, a) =>
5954
G.map(f(a))((k, _))

0 commit comments

Comments
 (0)