Scala的所有符号运算符都意味着什么?

本文翻译自:What do all of Scala's symbolic operators mean?

Scala syntax has a lot of symbols. Scala语法有很多符号。 Since these kinds of names are difficult to find using search engines, a comprehensive list of them would be helpful. 由于使用搜索引擎很难找到这些类型的名称,因此全面列出这些名称会很有帮助。

What are all of the symbols in Scala, and what does each of them do? Scala中的所有符号是什么,它们各自做了什么?

In particular, I'd like to know about -> , ||= , ++= , <= , _._ , :: , and :+= . 特别是,我想知道->||=++=<=_._:::+=


#1楼

参考:https://stackoom.com/question/X6H2/Scala的所有符号运算符都意味着什么


#2楼

As an addition to brilliant answers of Daniel and 0__, I have to say that Scala understands Unicode analogs for some of the symbols, so instead of 作为Daniel和0__的精彩答案的补充,我不得不说Scala了解某些符号的Unicode模拟,所以不是

for (n <- 1 to 10) n % 2 match {
  case 0 => println("even")
  case 1 => println("odd")
}

one may write 一个人可以写

for (n ← 1 to 10) n % 2 match {
  case 0 ⇒ println("even")
  case 1 ⇒ println("odd")
}

#3楼

I consider a modern IDE to be critical for understanding large scala projects. 我认为现代IDE对于理解大型scala项目至关重要。 Since these operators are also methods, in intellij idea I just control-click or control-b into the definitions. 由于这些运算符也是方法,因此我只需按控制点击或控制b即可进入定义。

You can control-click right into a cons operator (::) and end up at the scala javadoc saying "Adds an element at the beginning of this list." 你可以控制点击右边的一个cons运算符(::)并最终在scala javadoc说“在这个列表的开头添加一个元素”。 In user-defined operators, this becomes even more critical, since they could be defined in hard-to-find implicits... your IDE knows where the implicit was defined. 在用户定义的运算符中,这变得更加重要,因为它们可以用难以发现的含义定义......您的IDE知道隐式定义的位置。


#4楼

Just adding to the other excellent answers. 只是添加其他优秀的答案。 Scala offers two often criticized symbolic operators, /: ( foldLeft ) and :\\ ( foldRight ) operators, the first being right-associative. Scala提供了两个经常被批评的符号运算符, /: foldLeft )和:\\foldRight )运算符,第一个是右关联运算符。 So the following three statements are the equivalent: 所以以下三个陈述是等价的:

( 1 to 100 ).foldLeft( 0, _+_ )
( 1 to 100 )./:( 0 )( _+_ )
( 0 /: ( 1 to 100 ) )( _+_ )

As are these three: 这三个是:

( 1 to 100 ).foldRight( 0, _+_ )
( 1 to 100 ).:\( 0 )( _+_ )
( ( 1 to 100 ) :\ 0 )( _+_ )

#5楼

One (good, IMO) difference between Scala and other languages is that it lets you name your methods with almost any character. Scala和其他语言之间的一个(良好的,IMO)区别在于它允许您使用几乎任何字符命名您的方法。

What you enumerate is not "punctuation" but plain and simple methods, and as such their behavior vary from one object to the other (though there are some conventions). 你所列举的不是“标点符号”,而是简单明了的方法,因此它们的行为因对象而异(尽管有一些约定)。

For example, check the Scaladoc documentation for List , and you'll see some of the methods you mentioned here. 例如,检查ListScaladoc文档 ,您将看到此处提到的一些方法。

Some things to keep in mind: 要注意的一些事项:

  • Most of the times the A operator+equal B combination translates to A = A operator B , like in the ||= or ++= examples. 大多数情况下, A operator+equal B组合转换为A = A operator B ,就像在||=++=示例中一样。

  • Methods that end in : are right associative, this means that A :: B is actually B.::(A) . 结束的方法是:右关联,这意味着A :: B实际上是B.::(A)

You'll find most answers by browsing the Scala documentation. 您可以通过浏览Scala文档找到大多数答案。 Keeping a reference here would duplicate efforts, and it would fall behind quickly :) 在这里保留一个参考将重复努力,它会很快落后:)


#6楼

You can group those first according to some criteria. 您可以根据某些标准对这些进行分组。 In this post I will just explain the underscore character and the right-arrow. 在这篇文章中,我将解释下划线字符和右箭头。

_._ contains a period. _._包含一个句号。 A period in Scala always indicates a method call . Scala中的句点始终表示方法调用 So left of the period you have the receiver, and right of it the message (method name). 所以在你有接收器的那段时间左边,右边是消息(方法名称)。 Now _ is a special symbol in Scala. 现在_是Scala中的一个特殊符号 There are several posts about it, for example this blog entry all use cases. 有几篇关于它的帖子,例如这个博客条目的所有用例。 Here it is an anonymous function short cut , that is it a shortcut for a function that takes one argument and invokes the method _ on it. 这是一个匿名函数快捷方式,它是一个函数的快捷方式,它接受一个参数并在其上调用方法_ Now _ is not a valid method, so most certainly you were seeing _._1 or something similar, that is, invoking method _._1 on the function argument. 现在_不是一个有效的方法,所以你肯定会看到_._1或类似的东西,即在函数参数上调用方法_._1 _1 to _22 are the methods of tuples which extract a particular element of a tuple. _1_22是提取元组的特定元素的元组的方法。 Example: 例:

val tup = ("Hallo", 33)
tup._1 // extracts "Hallo"
tup._2 // extracts 33

Now lets assume a use case for the function application shortcut. 现在让我们假设一个功能应用程序快捷方式的用例。 Given a map which maps integers to strings: 给定一个将整数映射到字符串的映射:

val coll = Map(1 -> "Eins", 2 -> "Zwei", 3 -> "Drei")

Wooop, there is already another occurrence of a strange punctuation. Wooop,已经出现了另一个奇怪的标点符号。 The hyphen and greater-than characters, which resemble a right-hand arrow , is an operator which produces a Tuple2 . 连字符和大于字符,这类似于一个右手箭头时 ,是产生一操作者Tuple2 So there is no difference in the outcome of writing either (1, "Eins") or 1 -> "Eins" , only that the latter is easier to read, especially in a list of tuples like the map example. 因此,写入(1, "Eins")1 -> "Eins"的结果没有区别,只是后者更易于阅读,特别是在像地图示例这样的元组列表中。 The -> is no magic, it is, like a few other operators, available because you have all implicit conversions in object scala.Predef in scope. ->不是魔术,它可以像其他一些运算符一样可用,因为你在范围内的对象scala.Predef中有所有隐式转换。 The conversion which takes place here is 这里发生的转换是

implicit def any2ArrowAssoc [A] (x: A): ArrowAssoc[A] 

Where ArrowAssoc has the -> method which creates the Tuple2 . 其中ArrowAssoc具有->创建Tuple2方法。 Thus 1 -> "Eins" is actual the call Predef.any2ArrowAssoc(1).->("Eins") . 因此1 -> "Eins"实际上是调用Predef.any2ArrowAssoc(1).->("Eins") Ok. 好。 Now back to the original question with the underscore character: 现在回到带有下划线字符的原始问题:

// lets create a sequence from the map by returning the
// values in reverse.
coll.map(_._2.reverse) // yields List(sniE, iewZ, ierD)

The underscore here shortens the following equivalent code: 下划线缩短了以下等效代码:

coll.map(tup => tup._2.reverse)

Note that the map method of a Map passes in the tuple of key and value to the function argument. 请注意,Map的map方法将key和value的元组传递给function参数。 Since we are only interested in the values (the strings), we extract them with the _2 method on the tuple. 由于我们只对值(字符串)感兴趣,因此我们使用元组上的_2方法提取它们。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值