-
Notifications
You must be signed in to change notification settings - Fork 1k
Fix Chinese Translation of the cheatsheet #1223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ title: Scalacheat | |
partof: cheatsheet | ||
|
||
by: Brendan O'Connor | ||
about: 感谢 <a href="http://brenocon.com/">Brendan O'Connor</a>, 本速查表可以用于快速地查找Scala语法结构。Licensed by Brendan O'Connor under a CC-BY-SA 3.0 license. | ||
about: 感谢 <a href="http://brenocon.com/">Brendan O'Connor</a>,本速查表可以用于快速地查找Scala语法结构。Licensed by Brendan O'Connor under a CC-BY-SA 3.0 license. | ||
|
||
language: "zh-cn" | ||
--- | ||
|
@@ -15,39 +15,39 @@ language: "zh-cn" | |
|
||
|
||
| <span id="variables" class="h2">变量</span> | | | ||
| `var x = 5` | 可变量 | | ||
| `var x = 5` | 可变变量 | | ||
| <span class="label success">Good</span> `val x = 5`<br> <span class="label important">Bad</span> `x=6` | 常量 | | ||
| `var x: Double = 5` | 显式类型 | | ||
| <span id="functions" class="h2">函数</span> | | | ||
| <span class="label success">Good</span> `def f(x: Int) = { x*x }`<br> <span class="label important">Bad</span> `def f(x: Int) { x*x }` | 定义函数 <br> 隐藏出错: 不加“=”号将会是一段返回Unit类型的过程,这将会导致意想不到的错误。 | | ||
| <span class="label success">Good</span> `def f(x: Int) = { x*x }`<br> <span class="label important">Bad</span> `def f(x: Int) { x*x }` | 定义函数 <br> 隐蔽的错误: 不加“=”号将会是一段返回Unit类型的过程,这将会导致意想不到的错误。 | | ||
| <span class="label success">Good</span> `def f(x: Any) = println(x)`<br> <span class="label important">Bad</span> `def f(x) = println(x)` | 定义函数 <br> 语法错误: 每个参数都需要指定类型。 | | ||
| `type R = Double` | 类型别名 | | ||
| `def f(x: R)` vs.<br> `def f(x: => R)` | 传值调用 <br> 传名调用 (惰性参数) | | ||
| `(x:R) => x*x` | 匿名函数 | | ||
| `(1 to 5).map(_*2)` vs.<br> `(1 to 5).reduceLeft( _+_ )` | 匿名函数: 下划线是arg参数的占位符。 | | ||
| `(1 to 5).map( x => x*x )` | 匿名函数: 必须命名以后才可以使用一个arg参数两次。 | | ||
| <span class="label success">Good</span> `(1 to 5).map(2*)`<br> <span class="label important">Bad</span> `(1 to 5).map(*2)` | 匿名函数: 绑定前缀方法,理智的方法是`2*_`。 | | ||
| `(1 to 5).map { x => val y=x*2; println(y); y }` | 匿名函数: 程序块风格,最后一个表达式作为返回值。 | | ||
| `(1 to 5).map(_*2)` vs.<br> `(1 to 5).reduceLeft( _+_ )` | 匿名函数: 下划线是参数的占位符。 | | ||
| `(1 to 5).map( x => x*x )` | 匿名函数: 必须命名以后才可以多次使用同一个参数。 | | ||
| <span class="label success">Good</span> `(1 to 5).map(2*)`<br> <span class="label important">Bad</span> `(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))` <br> `val f = compose({_*2}, {_-1})` | 匿名函数: 要传入多个程序块的话,需要外围括号。 | | ||
| `def compose(g:R=>R, h:R=>R) = (x:R) => g(h(x))` <br> `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` <br> `(1 to 5) map (_*2)` | 中缀语法糖 | | ||
| `def sum(args: Int*) = args.reduceLeft(_+_)` | 可变参数 | | ||
| `def sum(args: Int*) = args.reduceLeft(_+_)` | 变长参数 | | ||
| <span id="packages" class="h2">包</span> | | | ||
| `import scala.collection._` | 通配符导入 | | ||
| `import scala.collection.Vector` <br> `import scala.collection.{Vector, Sequence}` | 选择性导入 | | ||
| `import scala.collection.{Vector => Vec28}` | 重命名导入 | | ||
| `import java.util.{Date => _, _}` | 导入java.util包里除Date之外的所有文件. | | ||
| `import java.util.{Date => _, _}` | 导入java.util包里除Date之外的一切。 | | ||
| `package pkg` _at start of file_ <br> `package pkg { ... }` | 声明这是一个包 | | ||
| <span id="data_structures" class="h2">数据结构</span> | | | ||
| `(1,2,3)` | 元组字面量 (`Tuple3`) | | ||
| `var (x,y,z) = (1,2,3)` | 解构绑定:通过模式匹配来解构元组。 | | ||
| <span class="label important">Bad</span>`var x,y,z = (1,2,3)` | 隐藏出错:每一个变量都被赋值了整个元组。 | | ||
| `var xs = List(1,2,3)` | 列表 (不可变). | | ||
| <span class="label important">Bad</span>`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 to 5` _等价于_ `1 until 6` <br> `1 to 10 by 2` | Range类型(语法糖) | | ||
|
@@ -58,12 +58,12 @@ language: "zh-cn" | |
| `while (x < 5) { println(x); x += 1}` | while循环 | | ||
| `do { println(x); x += 1} while (x < 5)` | do while循环 | | ||
| `import scala.util.control.Breaks._`<br>`breakable {`<br>` for (x <- xs) {`<br>` if (Math.random < 0.1) break`<br>` }`<br>`}`| break. ([slides](http://www.slideshare.net/Odersky/fosdem-2009-1013261/21)) | | ||
| `for (x <- xs if x%2 == 0) yield x*10` _等价于_ <br>`xs.filter(_%2 == 0).map(_*10)` | for comprehension (for 导出): filter/map | | ||
| `for ((x,y) <- xs zip ys) yield x*y` _等价于_ <br>`(xs zip ys) map { case (x,y) => x*y }` | for comprehension (for 导出): 解构绑定 | | ||
| `for (x <- xs; y <- ys) yield x*y` _等价于_ <br>`xs flatMap {x => ys map {y => x*y}}` | for comprehension (for 导出): 叉乘 | | ||
| `for (x <- xs; y <- ys) {`<br> `println("%d/%d = %.1f".format(x, y, x/y.toFloat))`<br>`}` | for comprehension (for 导出): 不可避免的格式<br>[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax) | | ||
| `for (i <- 1 to 5) {`<br> `println(i)`<br>`}` | for comprehension (for 导出): 包括上边界的遍历 | | ||
| `for (i <- 1 until 5) {`<br> `println(i)`<br>`}` | for comprehension (for 导出): 忽略上边界的遍历 | | ||
| `for (x <- xs if x%2 == 0) yield x*10` _等价于_ <br>`xs.filter(_%2 == 0).map(_*10)` | for 推导: filter/map | | ||
| `for ((x,y) <- xs zip ys) yield x*y` _等价于_ <br>`(xs zip ys) map { case (x,y) => x*y }` | for 推导: 解构绑定 | | ||
da-liii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `for (x <- xs; y <- ys) yield x*y` _等价于_ <br>`xs flatMap {x => ys map {y => x*y}}` | for 推导: 叉乘 | | ||
| `for (x <- xs; y <- ys) {`<br> `println("%d/%d = %.1f".format(x, y, x/y.toFloat))`<br>`}` | for 推导: 不可避免的格式<br>[sprintf-style](http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax) | | ||
| `for (i <- 1 to 5) {`<br> `println(i)`<br>`}` | for 推导: 包括上边界的遍历 | | ||
| `for (i <- 1 until 5) {`<br> `println(i)`<br>`}` | for 推导: 忽略上边界的遍历 | | ||
| <span id="pattern_matching" class="h2">模式匹配</span> | | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我们在《Scala实用指南》里面这样用的,这样比较简单。 |
||
| <span class="label success">Good</span> `(xs zip ys) map { case (x,y) => x*y }`<br> <span class="label important">Bad</span> `(xs zip ys) map( (x,y) => x*y )` | 在函数的参数中使用模式匹配的例子。 | | ||
| <span class="label important">Bad</span><br>`val v42 = 42`<br>`Some(3) match {`<br>` case Some(v42) => println("42")`<br>` case _ => println("Not 42")`<br>`}` | "v42" 被解释为可以匹配任何Int类型值的名称,打印输出"42"。 | | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.