idiomatic “get or else update” for immutable.Map?

折月煮酒 提交于 2019-12-03 08:48:47

问题


What is the idiomatic way of a getOrElseUpdate for immutable.Map instances?. I use the snippet below, but it seems verbose and inefficient

var map = Map[Key, Value]()

def foo(key: Key) = {
  val value = map.getOrElse(key, new Value)
  map += key -> value
  value
}

回答1:


Let me summarise your problem:

  • You want to call a method on a immutable data structure
  • You want it to return some value and reassign a var
  • Because the data structure is immutable, you’ll need to
    • return a new immutable data structure, or
    • do the assignment inside the method, using a supplied closure

So, either your signature has to look like

def getOrElseUpdate(key: K): Tuple2[V, Map[K,V]]
//... use it like
val (v, m2) = getOrElseUpdate(k)
map = m2

or

def getOrElseUpdate(key: K, setter: (Map[K,V]) => Unit): V
//... use it like
val v = getOrElseUpdate(k, map = _)

If you can live with one of these solutions, you could add your own version with an implicit conversion but judging by the signatures alone, i wouldn’t think any of these is in the standard library.




回答2:


I would probably implement a getOrElseUpdated method like this:

def getOrElseUpdated[K, V](m: Map[K, V], key: K, op: => V): (Map[K, V], V) =
  m.get(key) match {
    case Some(value) => (m, value)
    case None => val newval = op; (m.updated(key, newval), newval)
  }

which either returns the original map if m has a mapping for key or another map with the mapping key -> op added. The definition of this method is similar to getOrElseUpdate of mutable.Map.




回答3:


There's no such way - map mutation (update), when you're getting a map value, is a side effect (which contradicts to immutability/functional style of programming).

When you want to make a new immutable map with the default value, if another value for the specified key doesn't exist, you can do the following:

map + (key -> map.getOrElse(key, new Value)) 



回答4:


Why not use withDefault or withDefaultValue if you have an immutable map?



来源:https://stackoverflow.com/questions/4385976/idiomatic-get-or-else-update-for-immutable-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!