scala中的=>符号的含义

【声明】本帖的内容是copy来的,来源为stack overflow。

It has several meanings in Scala, all related to its mathematical meaning as implication.

  • In a value, it introduces a function literal, or lambda. e.g. the bit inside the curly braces in List(1,2,3).map { (x: Int) => x * 2 }
  • In a type, with symbols on both sides of the arrow (e.g. A => T, (A,B) => T, (A,B,C) => T, etc.) it‘s sugar for Function<n>[A[,B,...],T], that is, a function that takes parameters of type A[,B...], and returns a value of type T.
    • Empty parens on the left hand side (e.g. () => T) indicate that the function takes no parameters (also sometimes called a "thunk");
    • Empty parens on the right hand side denote that it returns ()—the sole value of type Unit, whose name can also be written ()—confused yet? :)

      A function that returns Unit is also known as a procedure, normally a method that‘s called only for its side effect.

  • In the type declaration for a method or function parameter, with no symbol on the left hand side (e.g. def f(param: => T)) it‘s a "by-name parameter", meaning that is evaluated every time it‘s used within the body of the function, and not before. Ordinary "by-value" parameters are evaluated before entry into the function/method.
  • In a case clause, they separate the pattern (and optional guard) from the result expression, e.g. case x => y.

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class.

For example, the type Int => String, is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String.  scala> val f: Function1[Int,String] = myInt => "my int: "+myInt.toString

  f: (Int) => String = <function1>

  scala> f(0)
  res0: String = my int: 0

  scala> val f2: Int => String = myInt => "my int v2: "+myInt.toString
  f2: (Int) => String = <function1>

  scala> f2(1)
  res1: String = my int v2: 1

 scala> val f2: Function2[Int,Int,String] = (myInt1,myInt2) => "This is my function to transfer " + myInt1 + " and " + myInt2 + " as a string component." f2: (Int, Int) => String = <function2>

 scala> f2(1,2)

  res6: String = This is my function to transfer 1 and 2 as a string component.

 

  scala> val f22:(Int,Int)=>String = (myInt1,myInt2) => "This is my function to transfer " + myInt1 + " and " + myInt2 + " as a string component."
  f22: (Int, Int) => String = <function2>

  scala> f22(2,4)
  res7: String = This is my function to transfer 2 and 4 as a string component.

 

Here myInt is binded to the argument value passed to f and f2.

() => T is the type of a function that takes no arguments and returns a T. It is equivalent to Function0[T]. () is called a zero parameter list I believe.

 scala> val f: () => Unit = () => { println("x")}
 f: () => Unit = <function0>

 scala> f()
 x

scala> val f2: Function0[Unit] = () => println("x2")
f: () => Unit = <function0>

scala> f2()
x2

As the most simplified answer, you can substitute whatever is on the left-hand side of => with the word "LEFT" and whatever is on the right-hand side with the word "RIGHT".

Then, the meaning of "LEFT => RIGHT" becomes:

Take LEFT then do RIGHT.

This means that if you have a "()=>" that you can take nothing (that is, no parameters) and then do whatever is on the right-hand side.

This is the most common meaning.

从而可以看出,这个符号主要是用在函数(匿名)的定义中。慢慢体会。这个是和java等其他语言有较大差别的地方。。。

时间: 2024-08-04 17:40:25

scala中的=>符号的含义的相关文章

svn中各种符号的含义

黄色感叹号(有冲突):--这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人的修改.要解决冲突,如果你确认你的修改是无效的,则用TSVN还原你的修改就行了:如果认为你的修改是正确的,别人的提交是无效的,那么用TSVN先标记为“解决冲突”,然后就可以提交了:如果你认为你的修改和别人的修改都有一部分是有效的,那么你就把别人的修改手动合并到你的修改中,然后使用TSVN标注为“解决

makefile中一些符号的含义

关于gnu make的详细介绍参看http://www.gnu.org/software/make/manual/make.html 规则 让我们先来粗略地看一看Makefile的规则. target ... : prerequisites ... command ... ... 目标:依赖 执行指令 ... target也就是一个目标文件,可以是Object File,也可以是执行文件.还可以是一个标签(Label). ① prerequisites就是,要生成那个target所需要的文件或是

SVN中各种符号箭头含义

黄色感叹号(有冲突): -- 这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许 你提交,防止你的提交覆盖了别人的修改.要解决冲突,如果你确认你的修改是无效的,则用TSVN还原你的修改就行了:如果认为你的修改是正确的,别人的提 交是无效的,那么用TSVN先标记为“解决冲突”,然后就可以提交了:如果你认为你的修改和别人的修改都有一部分是有效的,那么你就把别人的修改手动合并 到你的修改中,然后使用TSVN标

scala中常用但不常见(其他语言)的符号含义

本文旨在介绍Scala在其他语言中不太常见的符号含义,帮助理解Scala Code. 随着我对Scala学习的深入,我会不断增加该篇博文的内容,下面介绍Scala中的符号: :::三个冒号运算符:表示list的连接操作 val one = List(1,2,3) val two = List(4,5,6) val three = one:::two 输出结果为:three: List[Int] = List(1, 2, 3, 4, 5, 6) :: 两个冒号运算符:表示普通元素与list的连接操

正则表达式中的符号含义

. 匹配除换行符以外的任意字符\w 匹配字母或数字或下划线或汉字\s 匹配任意的空白符\d 匹配数字\b 匹配单词的开始或结束^ 匹配字符串的开始$ 匹配字符串的结束[\u4e00-\u9fa5]{2,20} 匹配2-20个汉字* 重复零次或更多次+ 重复一次或更多次? 重复零次或一次{n} 重复n次{n,} 重复n次或更多次{n,m} 重复n到m次\W 匹配任意不是字母,数字,下划线,汉字的字符\S 匹配任意不是空白符的字符\D 匹配任意非数字的字符\B 匹配不是单词开头或结束的位置[^x]

Linux Shell中的特殊符号和含义简明总结(包含了绝大部份)

case语句适用于需要进行多重分支的应用情况. case分支语句的格式如下: case $变量名 in 模式1) 命令序列1 ;; 模式2) 命令序列2        ;; *) 默认执行的命令序列     ;; esac Linux Shell中的特殊符号和含义简明总结(包含了绝大部份)_linux shell_脚本之家 在Linux Shell中有很多的特殊符号,这对于我们写Shell脚本时要特别留意:一方面要知道这些特殊符号的用法,这些符号用好了可以达到事半功倍的效果:但另一方面要避免这些

scala学习手记2 - scala中的循环

先来看一段Java中的循环: for (int i = 1; i < 4; i++) { System.out.print(i + ","); } 毫无疑问,scala可以让这个循环更加简洁.根据上一节中的内容,没有必要显示指定变量i的类型,我们甚至不需要声明这个变量.其次输出的语句也可以更加简洁一些,在scala中可以直接使用println()这个方法输出字符串.最后scala的循环结构也是非常的轻量级.好了,可以看一下代码了: for (i <- 1 to 3) { p

[转载]linux下编译php中configure参数具体含义

编译N次了   原来这么回事 原文地址:linux下编译php中configure参数具体含义作者:捷心特 php编译参数的含义 ./configure –prefix=/usr/local/php                      php 安装目录 –with-apxs2=/usr/local/apache/bin/apxs –with-config-file-path=/usr/local/php/etc      指定php.ini位置 –with-mysql=/usr/local

Scala中的特质详解

Scala中的特质与Java中的接口是比较类似的,但是Scala中的特质可以同时拥有抽象方法和具体方法,而类可以实现多个特质.下面我们详细讲解Scala中的特质这个强大的功能. 1. 把特质当作接口使用 我们定义一个trait,如下所示: 1 trait Logger { 2 def log(msg: String) 3 } 需要注意的是trait中未被实现的方法默认是抽象方法,因此不需要在方法前加abstract. 子类ConsoleLogger对Logger的实现,如下所示: 1 class