在 Scala 中的 Map 上使用`map` 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17908953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using `map` function on Map in Scala
提问by Arun Manivannan
First off, apologies for the lame question. I am reading the `Scala for the Impatient' religiously and trying to solve all the exercise questions (and doing some minimal exploration)
首先,为蹩脚的问题道歉。我正在虔诚地阅读“不耐烦的 Scala”,并试图解决所有练习题(并进行一些最小的探索)
Background :The exercise question goes like - Setup a map of prices for a number of gizmos that you covet. Then produce a second map with the same keys and the prices at a 10% discount.
背景:练习问题类似于 - 为您梦寐以求的一些小玩意设置价格地图。然后使用相同的键和 10% 的折扣生成第二张地图。
Unfortunately, at this point, most parts of the scaladoc are still cryptic to me but I understand that the map function of the Maptakes a function and returns another map after applying a function (I guess?) - def map[B](f: (A) ? B): HashMap[B]. I tried googling but couldnt get much useful results for map function for Map in scala:-)
不幸的是,在这一点上,scaladoc 的大部分内容对我来说仍然是神秘的,但我明白 map 函数Map接受一个函数并在应用一个函数后返回另一个映射(我猜?) - def map[B](f: (A) ? B): HashMap[B]。我试过谷歌搜索,但无法得到很多有用的结果map function for Map in scala:-)
My Question:As attempted in my variation 3, does using mapfunction for this purpose make any sense or should I stick with the variation 2 which actually solves my problem.
我的问题:正如我在变体 3 中所尝试的那样map,为此目的使用函数是否有意义,或者我应该坚持使用实际解决我的问题的变体 2。
Code :
代码 :
val gizmos:Map[String,Double]=Map("Samsung Galaxy S4 Zoom"-> 1000, "Mac Pro"-> 6000.10, "Google Glass"->2000)
//1. Normal for/yield
val discountedGizmos=(for ((k,v)<-gizmos) yield (k, v*0.9)) //Works fine
//2. Variation using mapValues
val discGizmos1=gizmos.mapValues(_*0.9) //Works fine
//3. Variation using only map function
val discGizmos2=gizmos.map((_,v) =>v*0.9) //ERROR : Wrong number of parameters: expected 1
回答by Shadowlands
In this case, mapValues does seem the more appropriate method to use. You would use the map method when you need to perform a transformation that requires knowledge of the keys (eg. converting a product reference into a product name, say).
在这种情况下,mapValues 似乎是更合适的方法。当您需要执行需要键知识的转换时,您将使用 map 方法(例如,将产品引用转换为产品名称)。
That said, the map method is more general as it gives you acces to both the keys and values for you to act upon, and you could emulate the mapValues method by simply transforming the values and passing the keys through untouched - and that is where you are going wrong in your code above. To use the map method correctly, you should be producing a (key, value) pair from your function, not just a key:
也就是说,map 方法更通用,因为它可以让您访问键和值以供您操作,并且您可以通过简单地转换值并将键传递给未受影响的键来模拟 mapValues 方法 - 这就是您上面的代码出错了。要正确使用 map 方法,您应该从函数中生成一个 (key, value) 对,而不仅仅是一个键:
val discGizmos2=gizmos.map{ case (k,v) => (k,v*0.9) } // pass the key through unchanged
回答by liutao
It can be also:
它也可以是:
val discGizmos2 = gizmos.map(kv => (kv._1, kv._2*0.9))

