简体   繁体   English

xs映射(f)和xs之间的差异。 斯卡拉地图(f)

[英]difference between xs map (f) and xs . map (f) in Scala

This is a syntax question. 这是一个语法问题。 I noticed that both of the following produce the same result 我注意到以下两个结果均相同

(1 until 10) map (square)
(1 until 10) . map (square)

where 哪里

def square(x : Int) = x * x

Is there any difference between the two? 两者之间有什么区别吗? If so, what? 如果是这样,该怎么办?

I did notice that 我确实注意到了

(1 until 10) map square

works, but 可行,但是

(1 until 10) . map square

is an error (missing arguments for method map in trait TraversableLike; follow this method with `_' if you want to treat it as a partially applied function), but I wasn't quite able to interpret the error message. 是一个错误(缺少特征TraversableLike中的方法映射的参数;如果要将它视为部分应用的函数,请在此方法后面加上_),但是我无法完全解释该错误消息。

You can call a method with infix notation (omitting the dot and the parenthesis) or normally with the dot. 您可以使用带前缀的符号(省略点和括号)或通常带点的方法来调用方法。 If you use normal notation the parentheses are mandatory. 如果使用普通符号,则括号是必需的。 See http://docs.scala-lang.org/style/method-invocation.html 参见http://docs.scala-lang.org/style/method-invocation.html

Scala offers 2 syntax for invoking methods of arity-1 : Scala提供了两种用于调用arity-1方法的语法:

a.m(b)
a m b

Mixing the both is not supported. 不支持将两者混合。

Personally, i find that space around the dot make code difficult to read 就个人而言,我发现点周围的空格使代码难以阅读

You could watch Martin Odersky's talk on Scala with style at http://www.parleys.com/play/51c1994ae4b0d38b54f4621b/chapter0/about , he explains the different styles and when (in his opinion) you should use them. 您可以在http://www.parleys.com/play/51c1994ae4b0d38b54f4621b/chapter0/about上观看Martin Odersky关于Scala的演讲,他解释了不同的风格,以及(他认为)何时应该使用它们。 Personally I stay away from the space notation for consistency but he thinks it's ok to use it for single expressions, not entire lists of function calls after function calls. 就我个人而言,我不愿使用空格表示法来保持一致性,但他认为可以将其用于单个表达式,而不是在函数调用之后使用整个函数调用列表。

You would use them for operators such as + and - of course. 当然,您可以将它们用于+和-等运算符。

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

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