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 adjust
in the Map
API, unfortunately. I've sometimes used a function like the following (modeled on Haskell's Data.Map.adjust
, with a different order of arguments):
def adjust[A, B](m: Map[A, B], k: A)(f: B => B) = m.updated(k, f(m(k)))
Now adjust(m, "Mark")(_ - 50)
does what you want. You could also use the pimp-my-library pattern to get the more natural m.adjust("Mark")(_ - 50)
syntax, if you really wanted something cleaner.
(Note that the short version above throws an exception if k
isn't in the map, which is different from the Haskell behavior and probably something you'd want to fix in real code.)