2
\$\begingroup\$

How's the following function to update an immutable Map's values?

scala> def updateMap[A](map: Map[A, Set[A]], item: A, toAdd: A) = { 
 map.map(x => if(x._1 == item) (x._1, (x._2 + toAdd)) else (x._1, x._2)) 
}
scala> val map = Map(1 -> Set(2), 3 -> Set(4))
scala> updateMap(map, 1, 5)
res1 ...= Map(1 -> Set(2, 5), 3 -> Set(4))
rolfl
98.1k17 gold badges219 silver badges419 bronze badges
asked Jun 27, 2014 at 5:12
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

You iterate through the whole Map, which is O(n) and therefore slow as hell. Worse, you even recreate every element and put it into another element, which is even slower.

Why not just do:

scala> val map = Map(1 -> Set(5,3), 2 -> Set(7,3))
map: scala.collection.immutable.Map[Int,scala.collection.immutable.Set[Int]] = Map(1 -> Set(5, 3), 2 -> Set(7, 3))
scala> def updateMap[A, B](map: Map[A, Set[B]], key: A, value: B) =
 map + ((key, map.getOrElse(key, Set()) + value))
updateMap: [A, B](map: Map[A,Set[B]], key: A, value: B)scala.collection.immutable.Map[A,Set[B]]
scala> updateMap(map, 1, 9)
res0: scala.collection.immutable.Map[Int,Set[Int]] = Map(1 -> Set(5, 3, 9), 2 -> Set(7, 3))

And you probably want to introduce another type parameter B to the signature of updateMap to allow values of a different type than the one of the key.

answered Jun 27, 2014 at 8:32
\$\endgroup\$
3
  • \$\begingroup\$ Using an if/else with if (map.contains (key)) would work without O(n) but it's not a succinct. Nice code above, thanks. \$\endgroup\$ Commented Jun 27, 2014 at 15:44
  • \$\begingroup\$ @Kevin I'm not sure if the original post was wrong, but the behavior is different in this answer. In the original post, if the key did not already exist in the map, the update did nothing. \$\endgroup\$ Commented Jun 27, 2014 at 19:55
  • \$\begingroup\$ And what if I want to update the map if the value is not empty only? \$\endgroup\$ Commented Dec 13, 2016 at 17:15
2
\$\begingroup\$

Pattern matching is a more readable and important feature of Scala, which you can use.

And in the else condition, you are using (x._1, x._2); you can use x in place of it.

 def updateMap[A](map: Map[A, Set[A]], item: A, toAdd: A) = { 
 map.map(x => (x._1 == item) match{ 
 case true=>(x._1, (x._2 + toAdd)) 
 case false=>x
 })
 }
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Jun 27, 2014 at 5:33
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.