idiomatic “get or else update” for immutable.Map?
问题 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,