Difference between mapValues and transform in Map

前端 未结 3 483
甜味超标
甜味超标 2020-12-24 02:23

In Scala Map (see API) what is the difference in semantics and performance between mapValues and transform ?

For any given map

3条回答
  •  旧时难觅i
    2020-12-24 02:35

    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).

提交回复
热议问题