From 64d2a73476dcb3b79ceadb253b38cadd52ab59e7 Mon Sep 17 00:00:00 2001 From: Zhaoyu Zhang Date: Thu, 23 Mar 2017 03:45:01 +0000 Subject: [PATCH 1/3] Simplified Chinese Translation of the cheatsheet translated the cheatsheet into Chinese, using the English and Japanese version as references. --- zh-cn/cheatsheets/index.md | 88 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 zh-cn/cheatsheets/index.md diff --git a/zh-cn/cheatsheets/index.md b/zh-cn/cheatsheets/index.md new file mode 100644 index 0000000000..12f57d52bb --- /dev/null +++ b/zh-cn/cheatsheets/index.md @@ -0,0 +1,88 @@ +--- +layout: cheatsheet +istranslation: true +title: Scalacheat +by: Brendan O'Connor +about: 感谢 Brendan O'Connor, 本速查表可以用于快速地查找Scala语法结构。Licensed by Brendan O'Connor under a CC-BY-SA 3.0 license. +languages: [zh-cn] +--- + +###### Contributed by {{ page.by }} + +| | | +| ------ | ------ | +| 变量 | | +| `var x = 5` | 变量 | +| Good `val x = 5`
Bad `x=6` | 常量 | +| `var x: Double = 5` | 指定数据类型 | +| 函数 | | +| Good `def f(x: Int) = { x*x }`
Bad `def f(x: Int) { x*x }` | 定义函数
隐藏错误: 没有“=”程序会返回Unit类型,这将会引起重大灾难。 | +| Good `def f(x: Any) = println(x)`
Bad `def f(x) = println(x)` | 定义函数
语法错误: 每个参数都需要指定类型。 | +| `type R = Double` | 类型别名 | +| `def f(x: R)` vs.
`def f(x: => R)` | 按值调用
按名称调用 (惰性参数) | +| `(x:R) => x*x` | 匿名函数 | +| `(1 to 5).map(_*2)` vs.
`(1 to 5).reduceLeft( _+_ )` | 匿名函数: 下划线是arg参数的占位符。 | +| `(1 to 5).map( x => x*x )` | 匿名函数: 必须命名以后才可以使用一个arg参数两次。 | +| Good `(1 to 5).map(2*)`
Bad `(1 to 5).map(*2)` | 匿名函数: 边界前缀方法,理智的人都用`2*_`。 | +| `(1 to 5).map { x => val y=x*2; println(y); y }` | 匿名函数: 上个方法的程序块风格。 | +| `(1 to 5) filter {_%2 == 0} map {_*2}` | 匿名函数: 管道风格(或者叫括号风格)。 | +| `def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))`
`val f = compose({_*2}, {_-1})` | 匿名函数: 要传入多个程序块的话,需要外部括号。 | +| `val zscore = (mean:R, sd:R) => (x:R) => (x-mean)/sd` | 柯里化, 很显然的语法。 | +| `def zscore(mean:R, sd:R) = (x:R) => (x-mean)/sd` | 柯里化, 很显然的语法。 | +| `def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd` | 柯里化,语法糖,然并卵。 | +| `val normer = zscore(7, 0.4) _` | 需要接上下划线才能得到偏微商(只适用于语法糖版本)。 | +| `def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)` | 泛型 | +| `5.+(3); 5 + 3`
`(1 to 5) map (_*2)` | 前缀语法糖 | +| `def sum(args: Int*) = args.reduceLeft(_+_)` | 可变参数 | +| | | +| `import scala.collection._` | 通配符导入 | +| `import scala.collection.Vector`
`import scala.collection.{Vector, Sequence}` | 选择性导入 | +| `import scala.collection.{Vector => Vec28}` | 重命名导入 | +| `import java.util.{Date => _, _}` | 导入java.util包里除Date之外的所有文件. | +| `package pkg` _at start of file_
`package pkg { ... }` | 声明一个包 | +| 数据结构 | | +| `(1,2,3)` | 元组 (`Tuple3`) | +| `var (x,y,z) = (1,2,3)` | 解构绑定:通过模式匹配来解构元组。 | +| Bad`var x,y,z = (1,2,3)` | 隐藏错误:每一个变量都被赋值了整个元组。 | +| `var xs = List(1,2,3)` | 列表 (不可变). | +| `xs(2)` | 用括号索引 ([slides](http://www.slideshare.net/Odersky/fosdem-2009-1013261/27)) | +| `1 :: List(2,3)` | 在列表开头添加元素 | +| `1 to 5` _same as_ `1 until 6`
`1 to 10 by 2` | 范围(语法糖) | +| `()` _(empty parens)_ | Unit类型的专有成员 (相当于 C/Java 里的void). | +| 控制结构 | | +| `if (check) happy else sad` | 条件 | +| `if (check) happy` _same as_
`if (check) happy else ()` | 条件(语法糖) | +| `while (x < 5) { println(x); x += 1}` | while循环 | +| `do { println(x); x += 1} while (x < 5)` | do while循环 | +| `import scala.util.control.Breaks._`
`breakable {`
` for (x <- xs) {`
` if (Math.random < 0.1) break`
` }`
`}`| break. ([slides](http://www.slideshare.net/Odersky/fosdem-2009-1013261/21)) | +| `for (x <- xs if x%2 == 0) yield x*10` _same as_
`xs.filter(_%2 == 0).map(_*10)` | for循环: filter/map | +| `for ((x,y) <- xs zip ys) yield x*y` _same as_
`(xs zip ys) map { case (x,y) => x*y }` | for循环: 解构绑定 | +| `for (x <- xs; y <- ys) yield x*y` _same as_
`xs flatMap {x => ys map {y => x*y}}` | for循环: 叉乘 | +| `for (x <- xs; y <- ys) {`
`println("%d/%d = %.1f".format(x, y, x/y.toFloat))`
`}` | for循环: 势在必行的格式
[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax) | +| `for (i <- 1 to 5) {`
`println(i)`
`}` | for循环: 包括上边界的遍历 | +| `for (i <- 1 until 5) {`
`println(i)`
`}` | for循环: 忽略上边界的遍历 | +| 模式匹配 | | +| Good `(xs zip ys) map { case (x,y) => x*y }`
Bad `(xs zip ys) map( (x,y) => x*y )` | 模式匹配中,函数的参数需要使用case。 | +| Bad
`val v42 = 42`
`Some(3) match {`
` case Some(v42) => println("42")`
` case _ => println("Not 42")`
`}` | "v42" 被解释为可以匹配任何Int类型值的名称,打印输出"42"。 | +| Good
`val v42 = 42`
`Some(3) match {`
`` case Some(`v42`) => println("42")``
`case _ => println("Not 42")`
`}` | 有倒引号的 "\`v42\`" 被解释为已经存在的val `v42`,所以输出的是 "Not 42". | +| Good
`val UppercaseVal = 42`
`Some(3) match {`
` case Some(UppercaseVal) => println("42")`
` case _ => println("Not 42")`
`}` | `UppercaseVal` 被视作已经存在的 val,而不是一个新的模式变量,因为它是以大写字母开头的,所以`UppercaseVal` 包含的值是 `3`, 结果输出的是"Not 42"。| +| 面向对象 | | +| `class C(x: R)` _same as_
`class C(private val x: R)`
`var c = new C(4)` | 构造器参数 - 私有 | +| `class C(val x: R)`
`var c = new C(4)`
`c.x` | 构造器参数 - 公有 | +| `class C(var x: R) {`
`assert(x > 0, "positive please")`
`var y = x`
`val readonly = 5`
`private var secret = 1`
`def this = this(42)`
`}`|
类本身就是构造器
声明一个公有成员变量
声明一个可获得但不可设置的成员变量
声明一个私有变量r
可选构造器| +| `new{ ... }` | 匿名类 | +| `abstract class D { ... }` | 定义一个抽象类。(不可创建) | +| `class C extends D { ... }` | 定义一个继承子类。 | +| `class D(var x: R)`
`class C(x: R) extends D(x)` | 继承与构造器参数(愿望清单: 默认自动传参) +| `object O extends D { ... }` | 定义一个单例模式(模块化的) | +| `trait T { ... }`
`class C extends T { ... }`
`class C extends D with T { ... }` | 特性
带有实现的接口,没有构造参数。 [mixin-able]({{ site.baseurl }}/tutorials/tour/mixin-class-composition.html). +| `trait T1; trait T2`
`class C extends T1 with T2`
`class C extends D with T1 with T2` | 多重特性 | +| `class C extends D { override def f = ...}` | 必须声明函数的重载。 | +| `new java.io.File("f")` | 创建类。 | +| Bad `new List[Int]`
Good `List(1,2,3)` | 类型错误: 抽象类型
相反,习惯上:调用的“工厂“会推测类型 | +| `classOf[String]` | 类字面常量 | +| `x.isInstanceOf[String]` | 类型检查 (运行时) | +| `x.asInstanceOf[String]` | 类型强制转换 (运行时) | +| `x: String` | 归属 (编译时) | + + From 5cb3c9f44b58d090c30793289c1021bb904067c9 Mon Sep 17 00:00:00 2001 From: Zhaoyu Zhang Date: Mon, 3 Apr 2017 22:55:19 +0100 Subject: [PATCH 2/3] update the changes according to @xring and @dcaoyuan @kunkun-tang --- zh-cn/cheatsheets/index.md | 48 +++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/zh-cn/cheatsheets/index.md b/zh-cn/cheatsheets/index.md index 12f57d52bb..f823b1277d 100644 --- a/zh-cn/cheatsheets/index.md +++ b/zh-cn/cheatsheets/index.md @@ -14,25 +14,25 @@ languages: [zh-cn] | 变量 | | | `var x = 5` | 变量 | | Good `val x = 5`
Bad `x=6` | 常量 | -| `var x: Double = 5` | 指定数据类型 | +|  `var x: Double = 5`                                                                                     | 显式类型 | | 函数 | | | Good `def f(x: Int) = { x*x }`
Bad `def f(x: Int) { x*x }` | 定义函数
隐藏错误: 没有“=”程序会返回Unit类型,这将会引起重大灾难。 | | Good `def f(x: Any) = println(x)`
Bad `def f(x) = println(x)` | 定义函数
语法错误: 每个参数都需要指定类型。 | | `type R = Double` | 类型别名 | -| `def f(x: R)` vs.
`def f(x: => R)` | 按值调用
按名称调用 (惰性参数) | +|  `def f(x: R)` vs.
`def f(x: => R)`                                                                 | 传值调用
传名调用 (惰性参数) | | `(x:R) => x*x` | 匿名函数 | | `(1 to 5).map(_*2)` vs.
`(1 to 5).reduceLeft( _+_ )` | 匿名函数: 下划线是arg参数的占位符。 | | `(1 to 5).map( x => x*x )` | 匿名函数: 必须命名以后才可以使用一个arg参数两次。 | | Good `(1 to 5).map(2*)`
Bad `(1 to 5).map(*2)` | 匿名函数: 边界前缀方法,理智的人都用`2*_`。 | -| `(1 to 5).map { x => val y=x*2; println(y); y }` | 匿名函数: 上个方法的程序块风格。 | +|  `(1 to 5).map { x => val y=x*2; println(y); y }`                                                             | 匿名函数: 程序块风格,返回的是最后一个表达式。 | | `(1 to 5) filter {_%2 == 0} map {_*2}` | 匿名函数: 管道风格(或者叫括号风格)。 | | `def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))`
`val f = compose({_*2}, {_-1})` | 匿名函数: 要传入多个程序块的话,需要外部括号。 | | `val zscore = (mean:R, sd:R) => (x:R) => (x-mean)/sd` | 柯里化, 很显然的语法。 | | `def zscore(mean:R, sd:R) = (x:R) => (x-mean)/sd` | 柯里化, 很显然的语法。 | -| `def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd` | 柯里化,语法糖,然并卵。 | -| `val normer = zscore(7, 0.4) _` | 需要接上下划线才能得到偏微商(只适用于语法糖版本)。 | +| `def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd` | 柯里化,语法糖。 | +|  `val normer = zscore(7, 0.4) _`                                                                         | 需要在后面加下划线来部分应用(只适用于语法糖版本)。 | | `def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)` | 泛型 | -| `5.+(3); 5 + 3`
`(1 to 5) map (_*2)` | 前缀语法糖 | +|  `5.+(3); 5 + 3`
`(1 to 5) map (_*2)`                                                               | 中缀语法糖 | | `def sum(args: Int*) = args.reduceLeft(_+_)` | 可变参数 | | | | | `import scala.collection._` | 通配符导入 | @@ -41,46 +41,46 @@ languages: [zh-cn] | `import java.util.{Date => _, _}` | 导入java.util包里除Date之外的所有文件. | | `package pkg` _at start of file_
`package pkg { ... }` | 声明一个包 | | 数据结构 | | -| `(1,2,3)` | 元组 (`Tuple3`) | +|  `(1,2,3)`                                                                                               | 元组字面量 (`Tuple3`) | | `var (x,y,z) = (1,2,3)` | 解构绑定:通过模式匹配来解构元组。 | | Bad`var x,y,z = (1,2,3)` | 隐藏错误:每一个变量都被赋值了整个元组。 | | `var xs = List(1,2,3)` | 列表 (不可变). | | `xs(2)` | 用括号索引 ([slides](http://www.slideshare.net/Odersky/fosdem-2009-1013261/27)) | -| `1 :: List(2,3)` | 在列表开头添加元素 | -| `1 to 5` _same as_ `1 until 6`
`1 to 10 by 2` | 范围(语法糖) | -| `()` _(empty parens)_ | Unit类型的专有成员 (相当于 C/Java 里的void). | +|  `1 :: List(2,3)`                                                                                       | cons | +|  `1 to 5` _等价于_ `1 until 6`
`1 to 10 by 2`                                                     | Range类型(语法糖) | +|  `()` _(空括号)_                                                                                   | Unit类型的专有成员 (相当于 C/Java 里的void). | | 控制结构 | | | `if (check) happy else sad` | 条件 | | `if (check) happy` _same as_
`if (check) happy else ()` | 条件(语法糖) | | `while (x < 5) { println(x); x += 1}` | while循环 | | `do { println(x); x += 1} while (x < 5)` | do while循环 | | `import scala.util.control.Breaks._`
`breakable {`
` for (x <- xs) {`
` if (Math.random < 0.1) break`
` }`
`}`| break. ([slides](http://www.slideshare.net/Odersky/fosdem-2009-1013261/21)) | -| `for (x <- xs if x%2 == 0) yield x*10` _same as_
`xs.filter(_%2 == 0).map(_*10)` | for循环: filter/map | -| `for ((x,y) <- xs zip ys) yield x*y` _same as_
`(xs zip ys) map { case (x,y) => x*y }` | for循环: 解构绑定 | -| `for (x <- xs; y <- ys) yield x*y` _same as_
`xs flatMap {x => ys map {y => x*y}}` | for循环: 叉乘 | -| `for (x <- xs; y <- ys) {`
`println("%d/%d = %.1f".format(x, y, x/y.toFloat))`
`}` | for循环: 势在必行的格式
[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax) | +|  `for (x <- xs if x%2 == 0) yield x*10` _等价于_
`xs.filter(_%2 == 0).map(_*10)`                   | for循环: filter/map | +|  `for ((x,y) <- xs zip ys) yield x*y` _等价于_
`(xs zip ys) map { case (x,y) => x*y }`             | for循环: 解构绑定 | +| `for (x <- xs; y <- ys) yield x*y` _等价于_
`xs flatMap {x => ys map {y => x*y}}` | for循环: 叉乘 | +|  `for (x <- xs; y <- ys) {`
   `println("%d/%d = %.1f".format(x, y, x/y.toFloat))`
`}`                     | for循环: 不可避免的格式
[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax) | | `for (i <- 1 to 5) {`
`println(i)`
`}` | for循环: 包括上边界的遍历 | | `for (i <- 1 until 5) {`
`println(i)`
`}` | for循环: 忽略上边界的遍历 | | 模式匹配 | | -| Good `(xs zip ys) map { case (x,y) => x*y }`
Bad `(xs zip ys) map( (x,y) => x*y )` | 模式匹配中,函数的参数需要使用case。 | +|  Good `(xs zip ys) map { case (x,y) => x*y }`
Bad `(xs zip ys) map( (x,y) => x*y )` | 在函数的参数中使用模式匹配的例子。 | | Bad
`val v42 = 42`
`Some(3) match {`
` case Some(v42) => println("42")`
` case _ => println("Not 42")`
`}` | "v42" 被解释为可以匹配任何Int类型值的名称,打印输出"42"。 | -| Good
`val v42 = 42`
`Some(3) match {`
`` case Some(`v42`) => println("42")``
`case _ => println("Not 42")`
`}` | 有倒引号的 "\`v42\`" 被解释为已经存在的val `v42`,所以输出的是 "Not 42". | -| Good
`val UppercaseVal = 42`
`Some(3) match {`
` case Some(UppercaseVal) => println("42")`
` case _ => println("Not 42")`
`}` | `UppercaseVal` 被视作已经存在的 val,而不是一个新的模式变量,因为它是以大写字母开头的,所以`UppercaseVal` 包含的值是 `3`, 结果输出的是"Not 42"。| +|  Good
`val v42 = 42`
`Some(3) match {`
``   case Some(`v42`) => println("42")``
`case _ => println("Not 42")`
`}` | 有反引号的 "\`v42\`" 被解释为已经存在的val `v42`,所以输出的是 "Not 42". | +|  Good
`val UppercaseVal = 42`
`Some(3) match {`
` case Some(UppercaseVal) => println("42")`
`   case _ => println("Not 42")`
`}` |  `UppercaseVal` 被视作已经存在的 val,而不是一个新的模式变量,因为它是以大写字母开头的,所以`UppercaseVal` 所包含的值(42)和检查的值(3)不匹配,输出"Not 42"。| | 面向对象 | | | `class C(x: R)` _same as_
`class C(private val x: R)`
`var c = new C(4)` | 构造器参数 - 私有 | | `class C(val x: R)`
`var c = new C(4)`
`c.x` | 构造器参数 - 公有 | -| `class C(var x: R) {`
`assert(x > 0, "positive please")`
`var y = x`
`val readonly = 5`
`private var secret = 1`
`def this = this(42)`
`}`|
类本身就是构造器
声明一个公有成员变量
声明一个可获得但不可设置的成员变量
声明一个私有变量r
可选构造器| +|  `class C(var x: R) {`
`assert(x > 0, "positive please")`
`var y = x`
`val readonly = 5`
`private var secret = 1`
`def this = this(42)`
`}`|
构造函数就是类的主体
声明一个公有成员变量
声明一个可get但不可set的成员变量
声明一个私有变量
可选构造器| | `new{ ... }` | 匿名类 | | `abstract class D { ... }` | 定义一个抽象类。(不可创建) | | `class C extends D { ... }` | 定义一个继承子类。 | | `class D(var x: R)`
`class C(x: R) extends D(x)` | 继承与构造器参数(愿望清单: 默认自动传参) | `object O extends D { ... }` | 定义一个单例模式(模块化的) | -| `trait T { ... }`
`class C extends T { ... }`
`class C extends D with T { ... }` | 特性
带有实现的接口,没有构造参数。 [mixin-able]({{ site.baseurl }}/tutorials/tour/mixin-class-composition.html). -| `trait T1; trait T2`
`class C extends T1 with T2`
`class C extends D with T1 with T2` | 多重特性 | -| `class C extends D { override def f = ...}` | 必须声明函数的重载。 | -| `new java.io.File("f")` | 创建类。 | -| Bad `new List[Int]`
Good `List(1,2,3)` | 类型错误: 抽象类型
相反,习惯上:调用的“工厂“会推测类型 | -| `classOf[String]` | 类字面常量 | +|  `trait T { ... }`
`class C extends T { ... }`
`class C extends D with T { ... }`                 | 特质
带有实现的接口,没有构造参数。 [mixin-able]({{ site.baseurl }}/tutorials/tour/mixin-class-composition.html). +|  `trait T1; trait T2`
`class C extends T1 with T2`
`class C extends D with T1 with T2`             | (混入)多个特质 | +|  `class C extends D { override def f = ...}`                                                           | 必须声明函数的重写。 | +|  `new java.io.File("f")`                                                                             | 创建对象。 | +|  Bad `new List[Int]`
Good `List(1,2,3)` | 类型错误: 抽象类型
相反,习惯上:调用工厂(方法)会自动推测类型 | +| `classOf[String]` | 类字面量 | | `x.isInstanceOf[String]` | 类型检查 (运行时) | | `x.asInstanceOf[String]` | 类型强制转换 (运行时) | | `x: String` | 归属 (编译时) | From 5760230b4cbaf101f2584e444ce15c86061cc2e5 Mon Sep 17 00:00:00 2001 From: Zhaoyu Zhang Date: Thu, 6 Apr 2017 08:18:41 +0100 Subject: [PATCH 3/3] update the newest version according to the suggestions by @dcaoyuan. --- zh-cn/cheatsheets/index.md | 44 +++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/zh-cn/cheatsheets/index.md b/zh-cn/cheatsheets/index.md index f823b1277d..156103e494 100644 --- a/zh-cn/cheatsheets/index.md +++ b/zh-cn/cheatsheets/index.md @@ -12,25 +12,25 @@ languages: [zh-cn] | | | | ------ | ------ | | 变量 | | -| `var x = 5` | 变量 | +|  `var x = 5`                                                                                             | 可变量       | | Good `val x = 5`
Bad `x=6` | 常量 | |  `var x: Double = 5`                                                                                     | 显式类型 | | 函数 | | -| Good `def f(x: Int) = { x*x }`
Bad `def f(x: Int) { x*x }` | 定义函数
隐藏错误: 没有“=”程序会返回Unit类型,这将会引起重大灾难。 | +|  Good `def f(x: Int) = { x*x }`
Bad `def f(x: Int)   { x*x }` | 定义函数
隐藏出错: 不加“=”号将会是一段返回Unit类型的过程,这将会导致意想不到的错误。 | | Good `def f(x: Any) = println(x)`
Bad `def f(x) = println(x)` | 定义函数
语法错误: 每个参数都需要指定类型。 | | `type R = Double` | 类型别名 | |  `def f(x: R)` vs.
`def f(x: => R)`                                                                 | 传值调用
传名调用 (惰性参数) | | `(x:R) => x*x` | 匿名函数 | | `(1 to 5).map(_*2)` vs.
`(1 to 5).reduceLeft( _+_ )` | 匿名函数: 下划线是arg参数的占位符。 | | `(1 to 5).map( x => x*x )` | 匿名函数: 必须命名以后才可以使用一个arg参数两次。 | -| Good `(1 to 5).map(2*)`
Bad `(1 to 5).map(*2)` | 匿名函数: 边界前缀方法,理智的人都用`2*_`。 | -|  `(1 to 5).map { x => val y=x*2; println(y); y }`                                                             | 匿名函数: 程序块风格,返回的是最后一个表达式。 | -| `(1 to 5) filter {_%2 == 0} map {_*2}` | 匿名函数: 管道风格(或者叫括号风格)。 | -| `def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))`
`val f = compose({_*2}, {_-1})` | 匿名函数: 要传入多个程序块的话,需要外部括号。 | -| `val zscore = (mean:R, sd:R) => (x:R) => (x-mean)/sd` | 柯里化, 很显然的语法。 | -| `def zscore(mean:R, sd:R) = (x:R) => (x-mean)/sd` | 柯里化, 很显然的语法。 | -| `def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd` | 柯里化,语法糖。 | -|  `val normer = zscore(7, 0.4) _`                                                                         | 需要在后面加下划线来部分应用(只适用于语法糖版本)。 | +|  Good `(1 to 5).map(2*)`
Bad `(1 to 5).map(*2)` | 匿名函数: 绑定前缀方法,理智的方法是`2*_`。 | +|  `(1 to 5).map { x => val y=x*2; println(y); y }`                                                             | 匿名函数: 程序块风格,最后一个表达式作为返回值。 | +|  `(1 to 5) filter {_%2 == 0} map {_*2}`                                                                 | 匿名函数: 管道风格(或者用圆括号)。 | +|  `def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))`
`val f = compose({_*2}, {_-1})`                   | 匿名函数: 要传入多个程序块的话,需要外围括号。 | +| `val zscore = (mean:R, sd:R) => (x:R) => (x-mean)/sd` | 柯里化, 显然的语法。 | +| `def zscore(mean:R, sd:R) = (x:R) => (x-mean)/sd` | 柯里化, 显然的语法。 | +|  `def zscore(mean:R, sd:R)(x:R) = (x-mean)/sd`                                                           | 柯里化,语法糖。然后:) | +|  `val normer = zscore(7, 0.4) _`                                                                         | 需要在尾部加下划线来变成偏函数(只对语法糖版本适用)。 | | `def mapmake[T](g:T=>T)(seq: List[T]) = seq.map(g)` | 泛型 | |  `5.+(3); 5 + 3`
`(1 to 5) map (_*2)`                                                               | 中缀语法糖 | | `def sum(args: Int*) = args.reduceLeft(_+_)` | 可变参数 | @@ -39,28 +39,28 @@ languages: [zh-cn] | `import scala.collection.Vector`
`import scala.collection.{Vector, Sequence}` | 选择性导入 | | `import scala.collection.{Vector => Vec28}` | 重命名导入 | | `import java.util.{Date => _, _}` | 导入java.util包里除Date之外的所有文件. | -| `package pkg` _at start of file_
`package pkg { ... }` | 声明一个包 | +|  `package pkg` _at start of file_
`package pkg { ... }`                                             | 声明这是一个包 | | 数据结构 | | |  `(1,2,3)`                                                                                               | 元组字面量 (`Tuple3`) | | `var (x,y,z) = (1,2,3)` | 解构绑定:通过模式匹配来解构元组。 | -| Bad`var x,y,z = (1,2,3)` | 隐藏错误:每一个变量都被赋值了整个元组。 | +|  Bad`var x,y,z = (1,2,3)`                                           | 隐藏出错:每一个变量都被赋值了整个元组。 | | `var xs = List(1,2,3)` | 列表 (不可变). | | `xs(2)` | 用括号索引 ([slides](http://www.slideshare.net/Odersky/fosdem-2009-1013261/27)) | -|  `1 :: List(2,3)`                                                                                       | cons | +|  `1 :: List(2,3)`                                                                                       | Cons(构成) | |  `1 to 5` _等价于_ `1 until 6`
`1 to 10 by 2`                                                     | Range类型(语法糖) | -|  `()` _(空括号)_                                                                                   | Unit类型的专有成员 (相当于 C/Java 里的void). | +|  `()` _(空括号)_                                                                                   | Unit类型的唯一成员 (相当于 C/Java 里的void). | | 控制结构 | | | `if (check) happy else sad` | 条件 | | `if (check) happy` _same as_
`if (check) happy else ()` | 条件(语法糖) | | `while (x < 5) { println(x); x += 1}` | while循环 | | `do { println(x); x += 1} while (x < 5)` | do while循环 | | `import scala.util.control.Breaks._`
`breakable {`
` for (x <- xs) {`
` if (Math.random < 0.1) break`
` }`
`}`| break. ([slides](http://www.slideshare.net/Odersky/fosdem-2009-1013261/21)) | -|  `for (x <- xs if x%2 == 0) yield x*10` _等价于_
`xs.filter(_%2 == 0).map(_*10)`                   | for循环: filter/map | -|  `for ((x,y) <- xs zip ys) yield x*y` _等价于_
`(xs zip ys) map { case (x,y) => x*y }`             | for循环: 解构绑定 | -| `for (x <- xs; y <- ys) yield x*y` _等价于_
`xs flatMap {x => ys map {y => x*y}}` | for循环: 叉乘 | -|  `for (x <- xs; y <- ys) {`
   `println("%d/%d = %.1f".format(x, y, x/y.toFloat))`
`}`                     | for循环: 不可避免的格式
[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax) | -| `for (i <- 1 to 5) {`
`println(i)`
`}` | for循环: 包括上边界的遍历 | -| `for (i <- 1 until 5) {`
`println(i)`
`}` | for循环: 忽略上边界的遍历 | +|  `for (x <- xs if x%2 == 0) yield x*10` _等价于_
`xs.filter(_%2 == 0).map(_*10)`                   | for comprehension (for 导出): filter/map | +|  `for ((x,y) <- xs zip ys) yield x*y` _等价于_
`(xs zip ys) map { case (x,y) => x*y }`             | for comprehension (for 导出): 解构绑定 | +| `for (x <- xs; y <- ys) yield x*y` _等价于_
`xs flatMap {x => ys map {y => x*y}}` | for comprehension (for 导出): 叉乘 | +|  `for (x <- xs; y <- ys) {`
   `println("%d/%d = %.1f".format(x, y, x/y.toFloat))`
`}`                     | for comprehension (for 导出): 不可避免的格式
[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax) | +| `for (i <- 1 to 5) {`
`println(i)`
`}` | for comprehension (for 导出): 包括上边界的遍历 | +| `for (i <- 1 until 5) {`
`println(i)`
`}` | for comprehension (for 导出): 忽略上边界的遍历 | | 模式匹配 | | |  Good `(xs zip ys) map { case (x,y) => x*y }`
Bad `(xs zip ys) map( (x,y) => x*y )` | 在函数的参数中使用模式匹配的例子。 | | Bad
`val v42 = 42`
`Some(3) match {`
` case Some(v42) => println("42")`
` case _ => println("Not 42")`
`}` | "v42" 被解释为可以匹配任何Int类型值的名称,打印输出"42"。 | @@ -74,10 +74,10 @@ languages: [zh-cn] | `abstract class D { ... }` | 定义一个抽象类。(不可创建) | | `class C extends D { ... }` | 定义一个继承子类。 | | `class D(var x: R)`
`class C(x: R) extends D(x)` | 继承与构造器参数(愿望清单: 默认自动传参) -| `object O extends D { ... }` | 定义一个单例模式(模块化的) | +|  `object O extends D { ... }`                                                                           | 定义一个单例(和模块一样) | |  `trait T { ... }`
`class C extends T { ... }`
`class C extends D with T { ... }`                 | 特质
带有实现的接口,没有构造参数。 [mixin-able]({{ site.baseurl }}/tutorials/tour/mixin-class-composition.html). |  `trait T1; trait T2`
`class C extends T1 with T2`
`class C extends D with T1 with T2`             | (混入)多个特质 | -|  `class C extends D { override def f = ...}`                                                           | 必须声明函数的重写。 | +|  `class C extends D { override def f = ...}`                                                           | 必须声明覆盖该方法。 | |  `new java.io.File("f")`                                                                             | 创建对象。 | |  Bad `new List[Int]`
Good `List(1,2,3)` | 类型错误: 抽象类型
相反,习惯上:调用工厂(方法)会自动推测类型 | | `classOf[String]` | 类字面量 |