Scala 地图方法语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10430616/
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
Scala map method syntax
提问by Nabegh
The code below from http://www.scalaclass.com/book/export/html/1to do matrix dot product.
下面的代码来自http://www.scalaclass.com/book/export/html/1做矩阵点积。
I can't understand the syntax between the curly brackets.
我无法理解大括号之间的语法。
- Why are the curly brackets used, not the regular method parentheses?
- Is t an anonymous method?
- What is ._1 and ._2?
- 为什么使用大括号,而不是常规方法括号?
- 是匿名方法吗?
- ._1 和 ._2 是什么?
Thanks.
谢谢。
type Row = List[Double]
type Matrix = List[Row]
def dotProd(v1:Row, v2:Row) =
v1.zip(v2).map{ t:(Double, Double) => t._1 * t._2 }.reduceLeft(_ + _)
回答by Daniel C. Sobral
- Why are the curly brackets used, not the regular method parentheses?
- 为什么使用大括号,而不是常规方法括号?
Some people prefer to use curly braces when the parameter is an anonymous function. For one thing, curly braces enable pattern matching anonymous functions, whereas parenthesis do not. In this particular example, there's no need for curly braces.
当参数是匿名函数时,有些人更喜欢使用花括号。一方面,花括号启用模式匹配匿名函数,而圆括号则不然。在这个特定的例子中,不需要花括号。
Here's an example where curly braces are required (because of the casepattern matching):
这是一个需要大括号的示例(因为case模式匹配):
def dotProd(v1:Row, v2:Row) =
v1.zip(v2).map{ case (a, b) => a * b }.reduceLeft(_ + _)
Note that the above function accomplishes the same thing as the one in the question, in a slightly different way.
请注意,上述函数与问题中的函数完成相同的事情,但方式略有不同。
- Is
tan anonymous method?
- 是
t匿名方法吗?
No, it is a parameter. Just like v1and v2are parameters for dotProd, tis a parameter for the anonymous function being passed to map.
不,它是一个参数。就像v1和v2是 的参数一样dotProd,t是传递给 的匿名函数的参数map。
- What is
._1and._2?
- 什么是
._1和._2?
Methods on t. The parameter twas defined as being a tuple (specifically, Tuple2[Double, Double], which can be written as (Double, Double)), and tuples let you extract each member of the tuple with methods like that: _1, _2, _3, etc.
上的方法t。该参数t被定义为是一个元组(具体地,Tuple2[Double, Double],这可以写为(Double, Double)),和元组让您提取元组的每个成员与这样的方法:_1,_2,_3,等。
A Tuple2only has _1and _2, of course. Note that the first parameter is _1, not _0, because of influence from other functional languages.
ATuple2只有_1和_2,当然。请注意,由于其他函数式语言的影响,第一个参数是_1,而不是_0。
Anyway, the zipmethod will convert Row(List[Double]) into a List[(Double, Double)]. The method maptakes a function that converts the elements of the list (which are (Double, Double)tuples) into something else.
无论如何,该zip方法会将Row( List[Double]) 转换为List[(Double, Double)]. 该方法map采用一个函数将列表的元素((Double, Double)元组)转换为其他元素。
回答by om-nom-nom
In this particular case curly brackets have no advantage over plain old syntax, but in general the sweet thing about using curly brackets is that they allow you to write pattern matching expressions inside map ...:
在这种特殊情况下,大括号与普通的旧语法相比没有优势,但总的来说,使用大括号的好处在于它们允许您在内部编写模式匹配表达式map ...:
so I can rewrite this
所以我可以重写这个
.map{ t:(Double, Double) => t._1 * t._2 }
into this
进入这个
.map{ case(a: Double, b: Double) => a*b }
but this will not compile:
但这不会编译:
.map( case(a: Double, b: Double) => a*b )
._1, ._2 provides access to first, second, ... N element of N-tuple, as Lee said.
._1, ._2 提供对 N-tuple 的 first, second, ... N 元素的访问,正如 Lee 所说。
回答by Matthew Farwell
You can find a very good answer to the differences between braces {} and parentheses () in this question: What is the formal difference in Scala between braces and parentheses, and when should they be used?
你可以在这个问题中找到关于大括号{}和括号()之间差异的很好的答案:Scala中大括号和括号之间的形式差异是什么,应该在什么时候使用?
For the _1, _2, see Meaning of _2 sign in scala language.
对于 _1、_2,请参见scala 语言中 _2 符号的含义。
And yes, t:(Double, Double) => t._1 * t._2is an anonymous function (not a method actually). Difference between method and function in Scala
是的,t:(Double, Double) => t._1 * t._2是一个匿名函数(实际上不是方法)。Scala中方法和函数的区别
回答by Lee
The curly brackets denote an anonymous function which has type Tuple2[Double,Double] => Double. The argument is given the local name t, so tis a tuple of two doubles.t._1refers to the first item and t._2the second.
大括号表示具有类型的匿名函数Tuple2[Double,Double] => Double。参数被赋予本地名称t,t两个双精度元组也是如此。t._1指的是第一项和t._2第二项。
Therefore mapyields a list of the element-wise products of the components of the two vectors, and reduceLeftsums these products to calculate the dot product.
因此map产生两个向量的分量的元素乘积列表,并将reduceLeft这些乘积相加以计算点积。

