Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
677 views
in Technique[技术] by (71.8m points)

scala - Idiomatic way to update value in a Map based on previous value

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...