Skip to content

Commit

Permalink
Reduce duplication in 2.12 concat, remove an unnecessary branch
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGregory084 committed May 17, 2022
1 parent b292134 commit 27a69d3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
12 changes: 2 additions & 10 deletions core/src/main/scala-2.12/cats/compat/HashMapCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,6 @@ private[data] trait HashMapCompat[K, +V] { self: HashMap[K, V] =>
* @param traversable the collection of key-value pairs to be added.
* @return a new map that contains all key-value pairs of this map and `traversable`.
*/
final def concat[VV >: V](hm: HashMap[K, VV]): HashMap[K, VV] = {
val newRootNode = hm.iterator.foldLeft(self.rootNode: HashMap.Node[K, VV]) { case (node, (k, v)) =>
node.updated(k, improve(self.hashKey.hash(k)), v, 0)
}

if (newRootNode eq self.rootNode)
this
else
new HashMap(newRootNode)
}
final def concat[VV >: V](hm: HashMap[K, VV]): HashMap[K, VV] =
concat(hm.iterator)
}
6 changes: 1 addition & 5 deletions core/src/main/scala/cats/data/HashMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,7 @@ final class HashMap[K, +V] private[data] (private[data] val rootNode: HashMap.No
final def updated[VV >: V](key: K, value: VV): HashMap[K, VV] = {
val keyHash = improve(hashKey.hash(key))
val newRootNode = rootNode.updated(key, keyHash, value, 0)

if (newRootNode eq rootNode)
this
else
new HashMap(newRootNode)
new HashMap(newRootNode)
}

/**
Expand Down

0 comments on commit 27a69d3

Please sign in to comment.