John
unread,Mar 26, 2012, 4:20:16 PM3/26/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to scala-user
I'm trying to iterate over a map, changing the values of various keys
only if they meet certain conditions. Say I've got this map:
val m1 = Map[String, Int]("x"->0, "y"->20)
If I want to update EVERY value, this is fairly straightforward. For
example, if I want to add 1 to each entry, I could do this:
m1.mapValues(_ + 1)
and my output is Map(1, 21)
But what if I want to update only positive values? What if I want to
add 1 only to values > 0 so that my output is Map(0, 21)?
How do I do that? I need a way to retrieve the value of each key, make
a Boolean evaluation, and then conditionally apply my function. I
can't figure out how to do this. Can somebody help?
Thanks, John