scala - Returning an immutable map containing a single modified entry from another one -


i have immutable map wish change single element , return immutable map. stuck copying elements twice?

  val inmap = map('a'->1,'b'->2)   import collection.mutable   val mmap = mutable.map(inmap.tolist:_*)   mmap('b')= 3   val mmap2 = map(mmap.tolist:_*)    mmap2: scala.collection.immutable.map[char,int] = map(b -> 3, -> 1) 

just wondering if 'required' price immutability on 'frontiers' of our methods.

you use updated method:

scala> val inmap = map('a' -> 1, 'b' -> 2) inmap: scala.collection.immutable.map[char,int] = map(a -> 1, b -> 2)  scala> val updated = inmap.updated('b', 3) updated: scala.collection.immutable.map[char,int] = map(a -> 1, b -> 3) 

api doc map: updated method.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -