Let’s say I store bank accounts information in an immutable Map:
val m = Map("Mark" -> 100, "Jonathan" -> 350, "Bob" -> 65)
and I want to withdraw, say, $50 from Mark’s account. I can do it as follows:
val m2 = m + ("Mark" -> (m("Mark") - 50))
But this code seems ugly to me. Is there better way to write this?
There’s no
adjustin theMapAPI, unfortunately. I’ve sometimes used a function like the following (modeled on Haskell’sData.Map.adjust, with a different order of arguments):Now
adjust(m, "Mark")(_ - 50)does what you want. You could also use the pimp-my-library pattern to get the more naturalm.adjust("Mark")(_ - 50)syntax, if you really wanted something cleaner.(Note that the short version above throws an exception if
kisn’t in the map, which is different from the Haskell behavior and probably something you’d want to fix in real code.)