本文翻译自: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. 例如,检查List的Scaladoc文档 ,您将看到此处提到的一些方法。

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 . 连字符和大于字符,这类似于一个右手箭头时 ,是产生一操作者Tuple2So 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方法提取它们。

Scala的所有符号运算符都意味着什么?相关推荐

  1. scala运算符_Scala的所有符号运算符是什么意思?

    scala运算符 Scala的符号运算符 (Scala's symbolic operators) The symbolic operators in Scala are symbols that h ...

  2. 九阴真经--scala入门+变量+数据类型+运算符

    一.入门 Spark由scala编写,为后续学习Spark打基础 scala基于java进行开发,把scala代码编译成class文件,运作在jvm上 scala是一种多范式.支持面向对象和函数式编程 ...

  3. Scala编程学习之三-运算符篇

    1.运算符介绍 运算符是一种特殊的符号,用以表示数据的运算.赋值和比较等. 1)算术运算符 2)赋值运算符 3)比较运算符(关系运算符) 4)逻辑运算符[与,或,非] 5)位运算符 (位运算 ~ | ...

  4. Linux命令中21个不太好搜索其含义的特殊符号你都知道吗?

    Linux命令中有一些常用符号,看到时不一定好查询它们的功能和含义,这里列举一些常见的符号和解释,欢迎大家补充完善. |: 管道符,在两个相邻命令之间传递数据:如ls | head把ls命令的输出传给 ...

  5. 这21个不太好搜索其含义的特殊符号你都知道吗?

    Linux命令中有一些常用符号,看到时不一定好查询它们的功能和含义,这里列举一些常见的符号和解释,欢迎大家补充完善. |: 管道符,在两个相邻命令之间传递数据:如ls | head把ls命令的输出传给 ...

  6. 这些21个不太好搜索其含义的特殊符号你都知道吗?

    Linux命令中有一些常用符号,看到时不一定好查询它们的功能和含义,这里列举一些常见的符号和解释,欢迎大家补充完善. |: 管道符,在两个相邻命令之间传递数据:如ls | head把ls命令的输出传给 ...

  7. 一个员工的离职,背后都意味着什么?

    来源 | web前端开发 一个员工的离职成本,很恐怖! 一个员工离职后留下的坑,并不是再找一个人填上就万事大吉了.一般来说,核心人才的流失,至少有1-2个月的招聘期.3个月的适应期,6个月的融入期:此 ...

  8. python运算符有哪些_python中算数运算符都有哪些

    慕码人8056858 1. 比较运算符:如果比较式成立,返回True:不成立,则返回False.常见的比较运算符如下:除了简单的两值相比之外,比较运算符还支持比较复杂的比较.(1)多条件比较:可以先给 ...

  9. c语言赋值符号,运算符赋值运算符 - C语言教程

    赋值运算符 下表列出了 C 语言支持的赋值运算符: 运算符 描述 实例 \= 简单的赋值运算符,把右边操作数的值赋给左边操作数 C = A + B 将把 A + B 的值赋给 C += 加且赋值运算符 ...

最新文章

  1. 使用Pygame制作微信打飞机游戏PC版
  2. Dbml文件提取建表TSql-CodeSmith
  3. AI理论知识整理(3)-正定矩阵
  4. centos6安装mysql并远程连接_如何开启phpstudy中mysql的远程连接
  5. 【C语言进阶深度学习记录】二 有符号与无符号
  6. 一年发表603篇论文、研究被引近3.9万次,学者操纵引文遭质疑
  7. JavaScript常用算法
  8. EOS源码分析:transaction的一生
  9. 调查 10,500 名 Java 开发者发现,收费的 OracleJDK 仍是主流、IntelliJ IDEA 最受欢迎...
  10. WinEdt LaTex(五)—— 内容的排版
  11. 【FPGA+BP神经网络】基于FPGA的简易BP神经网络verilog设计
  12. VMware ESXI 5.5 注册码
  13. gurobi和python_Gurobi Python建模环境使用介绍 第一部分准备 (v12最后更新2012
  14. 中科银谷:企业工商数据工商查询API接口应该如何选择?
  15. Excel 2010 SQL应用117 分组统计之GROUP BY 与First
  16. JavaScript之封装Math函数
  17. 脉冲星 6 月脉动 | Pulsar Summit 北美峰会圆满结束,ApacheCon 议程官宣
  18. linux常用命令及通过浏览器访问linux文件功能
  19. 根据励磁电感、谐振电感(漏感)、功率等级、频率设计LLC变换器的变压器
  20. 安卓linux层,Android 系统架构各层介绍

热门文章

  1. Kakao Talk母公司第四季净利润暴跌80%
  2. 【Python第三篇】Python装饰器、生成器、迭代器
  3. WebService、RMI、RPC、XML-RPC、JSON-RPC、SOAP、REST(rest-api、restful)等web服务实现方案概念...
  4. 使用R7版NDK搭建Android开发环境[不使用Cgywin]
  5. Java基础学习笔记 -- 9(数组)
  6. Office365—Exchange管理4—通讯组和安全组
  7. oracle 新手注意事项
  8. spring ioc控制反转
  9. H3C IGP与EGP
  10. python pdf转word