In Scala Map
(see API) what is the difference in semantics and performance between mapValues
and transform
?
For any given map
Here's a couple of unmentioned differences:
mapValues
creates a Map that is NOT serializable, without any indication that it's just a view (the type is Map[_, _]
, but just try to send one across the wire).
Since mapValues
is just a view, every instance contains the real Map
- which could be another result of mapValues
. Imagine you have an actor with some state, and every mutation of the state sets the new state to be a mapValues
on the previous state...in the end you have deeply nested maps with a copy of each previous state of the actor (and, yes, both of these are from experience).