collections - In Scala, apply function to values for some keys in immutable map -
let immutable map
val m = (0 3).map {x => (x,x*10) }.tomap m: scala.collection.immutable.map[int,int] = map(0 -> 0, 1 -> 10, 2 -> 20, 3 -> 30)
a collection of keys of interest
val k = set(0,2)
and function
def f(i:int) = + 1
how apply f
onto values in map mapped keys of interest resulting map be
map(0 -> 1, 1 -> 10, 2 -> 21, 3 -> 30)
m.transform{ (key, value) => if (k(key)) f(value) else value }
Comments
Post a Comment