Wenn ich filterKeys
oder mapValues
auf einem Map
verwenden, um einen neuen Map
zu erstellen, die Funktionen, die ich in diese Methoden übergeben habe, scheint jedes einzelne Mal, wenn ich die neue Map
verwenden ausgeführt werden . Sollten sie nicht nur einmal ausgeführt werden, um den neuen Map
zu generieren? In diesem Beispiel sehen Sie, wie die println
s mehrmals vorkommen.`filterKeys` und` mapValues` erhalten mehrmals ausgeführt
// printlns happen when creating the map
scala> val myMap = Map(1 -> 1, 2 -> 2, 3 -> 3).filterKeys { i =>
println(s"filterKeys$i")
i < 5
}.mapValues { i =>
println(s"mapValues$i")
i + 1
}
filterKeys1
mapValues1
filterKeys2
mapValues2
filterKeys3
mapValues3
myMap: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 2 -> 3, 3 -> 4)
// printlns happen again!
scala> myMap.toString()
filterKeys1
mapValues1
filterKeys2
mapValues2
filterKeys3
mapValues3
res29: String = Map(1 -> 2, 2 -> 3, 3 -> 4)
// and again!
scala> myMap + (4 -> 5)
filterKeys1
mapValues1
filterKeys2
mapValues2
filterKeys3
mapValues3
res30: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 2 -> 3, 3 -> 4, 4 -> 5)
würde ich das Verhalten erwartet die gleichen wie .map()
zu sein. Die Funktion zur Karte übergeben, wird nur eine für jedes Element ausgeführt und wird nicht erneut ausgeführt werden, wenn die resultierende Karte in der Zukunft verwendet wird:
// printlns happen when creating the map
scala> val myMap = Map(1 -> 1, 2 -> 2, 3 -> 3).map { i =>
println(s"map$i")
i
}
map(1,1)
map(2,2)
map(3,3)
myMap: scala.collection.immutable.Map[Int,Int] = Map(1 -> 1, 2 -> 2, 3 -> 3)
// printlns not run again :)
scala> myMap.toString
res32: String = Map(1 -> 1, 2 -> 2, 3 -> 3)
Dies wurde auch darüber gebloggt [hier] (http://blog.bruchez.name/2013/02/mapmap-vs-mapmapvalues.html) – theon