简体   繁体   English

Scala map方法语法

[英]Scala map method syntax

The code below from http://www.scalaclass.com/book/export/html/1 to 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(_ + _)
  • 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 case pattern 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 t an anonymous method? t匿名方法?

No, it is a parameter. 不,这是一个参数。 Just like v1 and v2 are parameters for dotProd , t is a parameter for the anonymous function being passed to map . 就像v1v2dotProd参数dotProdt是传递给map的匿名函数的参数。

  • What is ._1 and ._2 ? 什么是._1._2

Methods on t . 关于t方法。 The parameter t was 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被定义为一个元组(具体来说, Tuple2[Double, Double] ,可以写成(Double, Double) ),元组允许你用这样的方法提取元组的每个成员: _1_2_3

A Tuple2 only has _1 and _2 , of course. 当然, Tuple2只有_1_2 Note that the first parameter is _1 , not _0 , because of influence from other functional languages. 请注意,由于其他函数语言的影响,第一个参数是_1 ,而不是_0

Anyway, the zip method will convert Row ( List[Double] ) into a List[(Double, Double)] . 无论如何, zip方法会将RowList[Double] )转换为List[(Double, Double)] The method map takes a function that converts the elements of the list (which are (Double, Double) tuples) into something else. 方法map采用将列表元素( (Double, Double)元组)转换为其他元素的函数。

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. Lee说,._1,._2可以访问N元组的第一,第二,...... N元素。

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._2 is an anonymous function (not a method actually). 是的, t:(Double, Double) => t._1 * t._2是一个匿名函数(实际上不是一个方法)。 Difference between method and function in Scala Scala中方法和函数之间的区别

The curly brackets denote an anonymous function which has type Tuple2[Double,Double] => Double . 花括号表示一个匿名函数,其类型为Tuple2[Double,Double] => Double The argument is given the local name t , so t is a tuple of two doubles. 参数给出了本地名称t ,因此t是两个双精度的元组。 t._1 refers to the first item and t._2 the second. t._1表示第一个项目, t._2表示第二个项目。

Therefore map yields a list of the element-wise products of the components of the two vectors, and reduceLeft sums these products to calculate the dot product. 因此, map生成两个向量的组件的元素乘积列表, reduceLeft这些reduceLeft求和以计算点积。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM