Skip to content

modify/correct _zh-cn/index.md #2394

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 87 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
f38f5ff
modify/correct _zh-cn/index.md
benluo May 10, 2022
30add30
modify _zh-cn/scala3/new-in-scala3.md
benluo May 11, 2022
1c937b8
add _zh-cn/scala3/guides/tasty-overview.md
benluo May 11, 2022
3de24bc
Merge branch 'scala:main' into main
benluo May 12, 2022
770e5a7
Change _zh-cn/scala3/guides/tasty-overview.md
benluo May 13, 2022
3edca3b
add few chapters in _zh-cn/overviews/scala3-book
benluo May 16, 2022
adcf494
Merge branch 'scala:main' into main
benluo May 16, 2022
e732e9e
translated all tastes files. in scala3-book.
benluo May 17, 2022
f87d417
Merge branch 'scala:main' into main
benluo May 17, 2022
74405c5
modified according to review.
benluo May 17, 2022
4422ef0
Merge branch 'scala:main' into main
benluo May 18, 2022
313fdff
correct translation according review.
benluo May 18, 2022
300d5cd
Missed one translation. Finish all corrects.
benluo May 18, 2022
13c00a6
Merge branch 'scala:main' into main
benluo May 21, 2022
3f33fc4
add _zh-cn/overviews/scala3-book/first-look-at-types.md
benluo May 21, 2022
e10cf0d
add _zh-cn/overviews/scala3-book/control-structures.md
benluo May 21, 2022
3573d19
add two files as below
benluo May 22, 2022
d8cbad3
add _zh-cn/overviews/scala3-book/domain-modeling-oop.md
benluo May 24, 2022
76f49ec
Merge branch 'scala:main' into main
benluo May 24, 2022
799b1f2
add and modified as below
benluo May 24, 2022
3dd2ec3
add all methods*
benluo May 24, 2022
66a752e
Merge branch 'scala:main' into main
benluo May 25, 2022
8390a95
add functions
benluo May 26, 2022
b48eb7c
add _zh-cn/overviews/scala3-book/packaging-imports.md
benluo May 28, 2022
6504fbf
Merge branch 'scala:main' into main
benluo May 28, 2022
c11754c
add all collections
benluo May 28, 2022
f751a72
add all fp files
benluo May 29, 2022
3098a0f
Merge branch 'scala:main' into main
benluo May 31, 2022
d811aba
add all types files
benluo Jun 5, 2022
b9c6ac0
add all types files
benluo Jun 5, 2022
d8ba669
Merge branch 'scala:main' into main
benluo Jun 5, 2022
a98a687
add all ca-* files
benluo Jun 8, 2022
aadcb0e
add concurrency.md
benluo Jun 10, 2022
b845934
Merge branch 'scala:main' into main
benluo Jun 10, 2022
bb47995
add all tools files
benluo Jun 11, 2022
4bb25d3
Merge branch 'scala:main' into main
benluo Jun 11, 2022
c59f3ba
add interacting-with-java.md
benluo Jun 11, 2022
76e08bf
add all scala-for-* files. Finish all files in Scala3-book.
benluo Jun 16, 2022
d3637fb
corrected broken link in concurrency.md
benluo Jun 16, 2022
4f3b6da
Merge branch 'scala:main' into main
benluo Jun 21, 2022
00e7ca6
Merge branch 'scala:main' into main
benluo Jun 25, 2022
8fa8ac4
Merge branch 'scala:main' into main
benluo Jul 1, 2022
585c7ac
Merge branch 'scala:main' into main
benluo Jul 4, 2022
47aab08
correct ca.* files
benluo Jul 4, 2022
de48d7e
Merge branch 'scala:main' into main
benluo Jul 15, 2022
00edb7d
Merge branch 'scala:main' into main
benluo Jul 17, 2022
ddcf921
modified translation according to review.
benluo Jul 17, 2022
951c842
Merge branch 'scala:main' into main
benluo Jul 19, 2022
7e54512
correct collections-classes.md
benluo Jul 19, 2022
86fdf8b
change translation according to review on Jul 19
Jul 21, 2022
fca798c
Merge branch 'scala:main' into main
benluo Jul 22, 2022
9cdf9fe
Merge branch 'scala:main' into main
benluo Jul 25, 2022
e387055
Merge branch 'scala:main' into main
benluo Jul 27, 2022
6a507ea
modified according to review on Jul 25, 2022.
benluo Jul 27, 2022
cbad07c
Merge branch 'scala:main' into main
benluo Jul 29, 2022
f524cce
Update _zh-cn/overviews/scala3-book/control-structures.md
benluo Jul 29, 2022
774fa7d
Update _zh-cn/overviews/scala3-book/domain-modeling-tools.md
benluo Jul 29, 2022
d64054f
Update _zh-cn/overviews/scala3-book/domain-modeling-tools.md
benluo Jul 29, 2022
e216375
changed according to review on Jul 28, 2022.
benluo Jul 29, 2022
3008f7c
Merge branch 'scala:main' into main
benluo Aug 3, 2022
6e7daca
Merge branch 'scala:main' into main
benluo Aug 10, 2022
2f5c1db
add scala4x.css in fold. Add next in scala-features.md
benluo Aug 10, 2022
65a6a01
Merge branch 'scala:main' into main
benluo Aug 14, 2022
4eb239a
Merge branch 'scala:main' into main
benluo Aug 17, 2022
6549731
Merge branch 'scala:main' into main
benluo Aug 19, 2022
2decccc
Update _zh-cn/overviews/scala3-book/domain-modeling-fp.md
benluo Aug 21, 2022
0051e8b
Update _zh-cn/overviews/scala3-book/domain-modeling-oop.md
benluo Aug 21, 2022
6c267bf
Update _zh-cn/overviews/scala3-book/fun-write-map-function.md
benluo Aug 21, 2022
a523765
Update _zh-cn/overviews/scala3-book/fun-write-map-function.md
benluo Aug 21, 2022
4073ced
Update _zh-cn/overviews/scala3-book/methods-most.md
benluo Aug 21, 2022
5a1aa41
Update _zh-cn/overviews/scala3-book/scala-for-java-devs.md
benluo Aug 21, 2022
07192cf
Update _zh-cn/overviews/scala3-book/taste-objects.md
benluo Aug 21, 2022
40f3e2c
changed all review.
benluo Aug 21, 2022
5ff7904
Merge branch 'scala:main' into main
benluo Aug 24, 2022
c080bac
Merge branch 'scala:main' into main
benluo Aug 25, 2022
ce11fd7
Merge branch 'scala:main' into main
benluo Aug 28, 2022
e908683
Update _zh-cn/overviews/scala3-book/domain-modeling-oop.md
benluo Aug 28, 2022
bf18649
Update _zh-cn/overviews/scala3-book/domain-modeling-oop.md
benluo Aug 28, 2022
3db8200
Update _zh-cn/overviews/scala3-book/methods-most.md
benluo Aug 28, 2022
349b19d
Update _zh-cn/overviews/scala3-book/methods-most.md
benluo Aug 28, 2022
0ef10ad
Update _zh-cn/overviews/scala3-book/methods-most.md
benluo Aug 28, 2022
850bee6
Update _zh-cn/overviews/scala3-book/domain-modeling-oop.md
benluo Aug 28, 2022
1cf9b12
all have been changed according to review.
benluo Aug 28, 2022
67699a2
Merge branch 'scala:main' into main
benluo Sep 1, 2022
4e32489
changed term and delete "next-page" in scala-features.md
benluo Sep 2, 2022
60e8684
Merge branch 'scala:main' into main
benluo Sep 2, 2022
ba6db33
Merge branch 'scala:main' into main
benluo Sep 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions _zh-cn/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,37 @@ scala3-sections:
- title: "第一步"
links:
- title: "Scala 3 中的新东西"
description: "Scala 3 现存新特性概览"
description: "Scala 3 中令人兴奋的新特性概览"
icon: "fa fa-star"
link: /scala3/new-in-scala3.html
- title: "快速开始"
description: "安装 Scala 3 并开始写些 Scala 代码"
description: "在你的电脑中安装 Scala 3,然后开始写些 Scala 代码"
icon: "fa fa-rocket"
link: /scala3/getting-started.html
- title: "Scala 3 书籍"
description: "一部介绍主要语言特性的线上书"
- title: "Scala 3 册子"
description: "通过一系列小课程来学习 Scala。"
icon: "fa fa-book"
link: /scala3/book/introduction.html
- title: "更多细节"
links:
- title: "迁移指引"
description: "一份帮你从 Scala 2 迁移到 Scala 3 的指引"
description: "一份帮你从 Scala 2 迁移到 Scala 3 的指引"
icon: "fa fa-suitcase"
link: /scala3/guides/migration/compatibility-intro.html
- title: "导览"
description: "关于语言特别之处的详细导览"
description: "关于语言特别之处的详细导览"
icon: "fa fa-map"
link: /zh-cn/scala3/guides.html
- title: "Scala 库 API"
description: "Scala 3 标准库API文档(多个小版本)"
description: "Scala 3 每一个版本的标准库API文档。"
icon: "fa fa-file-alt"
link: https://scala-lang.org/api/3.x/
- title: "语言参考手册"
description: "Scala 3 语言参考手册"
description: "Scala 3 语言参考手册"
icon: "fa fa-book"
link: https://docs.scala-lang.org/scala3/reference
- title: "贡献指南"
description: "Scala 3 编译器指南及如何贡献代码"
description: "Scala 3 编译器指南及如何修复问题"
icon: "fa fa-cogs"
link: /scala3/guides/contribution/contribution-intro.html
- title: "Scala 3 全新的 Scaladoc"
Expand Down Expand Up @@ -109,4 +109,8 @@ scala2-sections:
description: "Scala改进过程(Scala Improvement Process),语言及编译器进展"
icon: "fa fa-cogs"
link: /sips/index.html
- title: "为 Scala 作贡献"
description: "为 Scala 项目作贡献的完整指南"
icon: "fa fa-cogs"
link: /contribute/
---
270 changes: 270 additions & 0 deletions _zh-cn/overviews/scala3-book/taste-control-structures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
---
title: 控制结构
type: section
description: This section demonstrates Scala 3 control structures.
num: 8
previous-page: taste-vars-data-types
next-page: taste-modeling
---


Scala 具有您在其他编程语言中可以找到的控制结构,并且还具有强大的 `for` 表达式和 `match` 表达式:

- `if`/`else`
- `for` 循环和表达式
- `match` 表达式
- `while` 循环
- `try`/`catch`

这些结构在以下示例中进行了说明。

## `if`/`else`

Scala 的 `if`/`else` 控制结构看起来与其他语言相似:

```scala
if x < 0 then
println("negative")
else if x == 0 then
println("zero")
else
println("positive")
```

请注意,这确实是一个 _表达式_ ---不是一个 _语句_。
这意味着它返回一个值,因此您可以将结果赋值给一个变量:

```scala
val x = if a < b then a else b
```

正如您将在本书中看到的那样,_所有_ Scala 控制结构都可以用作表达式。

> 表达式返回结果,而语句不返回。
> 语句通常用于它们的副作用,例如使用 `println` 打印到控制台。

在 Scala 2 中 `if`/`else` 控制结构的构造不同,需要括号和大括号,而不是关键字 `then`。

```scala
// Scala 2 syntax
if (test1) {
doX()
} else if (test2) {
doY()
} else {
doZ()
}
```

## `for` 循环和表达式

`for` 关键字用于创建 `for` 循环。
这个例子展示了如何打印 `List` 中的每个元素:

```scala
val ints = List(1, 2, 3, 4, 5)

for i <- ints do println(i)
```

代码 `i <- ints` 被称为 _生成器_,紧随 `do` 关键字的代码是 _循环体_。

Scala 2 中这种控制结构的旧语法是:

```scala
// Scala 2 语法
for (i <- ints) println(i)
```

再次注意括号的使用和 Scala 3 中新的 `for`-`do`。

### 守卫

您还可以在 `for` 循环中使用一个或多个 `if` 表达式。
这些被称为 _守卫_。
此示例打印 `ints` 中大于 `2` 的所有数字:

```scala
for
i <- ints
if i > 2
do
println(i)
```

您可以使用多个生成器和守卫。
此循环遍历数字 `1` 到 `3`,并且对于每个数字,它还遍历字符 `a` 到 `c`。
然而,它也有两个守卫,所以唯一一次调用 print 语句是当 `i` 的值为 `2` 并且 `j` 是字符 `b` 时:

```scala
for
i <- 1 to 3
j <- 'a' to 'c'
if i == 2
if j == 'b'
do
println(s"i = $i, j = $j") // prints: "i = 2, j = b"
```

### `for` 表达式

`for` 关键字更强大:当您使用 `yield` 关键字代替 `do` 时,您会创建 `for` _表达式_ 用于计算和产生结果。

几个例子演示了这一点。
使用与上一个示例相同的 `ints` 列表,此代码创建一个新列表,其中新列表中每个元素的值是原始列表中元素值的两倍:

````
scala> val doubles = for i <- ints yield i * 2
val doubles: List[Int] = List(2, 4, 6, 8, 10)
````

Scala 的控制结构语法很灵活,`for` 表达式可以用其他几种方式编写,具体取决于您的偏好:

```scala
val doubles = for i <- ints yield i * 2 // 如上所示的风格
val doubles = for (i <- ints) yield i * 2
val doubles = for (i <- ints) yield (i * 2)
val doubles = for { i <- ints } yield (i * 2)
```

此示例显示如何将列表中每个字符串的第一个字符大写:

```scala
val names = List("chris", "ed", "maurice")
val capNames = for name <- names yield name.capitalize
```

最后,这个 `for` 表达式遍历一个字符串列表,并返回每个字符串的长度,但前提是该长度大于 `4`:

```scala
val fruits = List("apple", "banana", "lime", "orange")

val fruitLengths = for
f <- fruits
if f.length > 4
yield
// 在这里你可以
// 使用多行代码
f.length

//fruitLengths: List[Int] = List(5, 6, 6)
```

`for` 循环和表达式更多细节在本书的 [控制结构部分][control] 中,和 [参考文档]({{ site.scala3ref }}/other-new-features/control-syntax.html) 中。

## `match` 表达式

Scala 有一个 `match` 表达式,它最基本的用途类似于 Java `switch` 语句:

```scala
val i = 1

// later in the code ...
i match
case 1 => println("one")
case 2 => println("two")
case _ => println("other")
```

但是,`match` 确实是一个表达式,这意味着它会根据模式匹配返回一个结果,您可以将其绑定到一个变量:

```scala
val result = i match
case 1 => "one"
case 2 => "two"
case _ => "other"
```

`match` 不仅限于使用整数值,它可以用于任何数据类型:

```scala
val p = Person("Fred")

// later in the code
p match
case Person(name) if name == "Fred" =>
println(s"$name says, Yubba dubba doo")

case Person(name) if name == "Bam Bam" =>
println(s"$name says, Bam bam!")

case _ => println("Watch the Flintstones!")
```

事实上,`match` 表达式可以用许多模式的不同类型来测试变量。
此示例显示 (a) 如何使用 `match` 表达式作为方法的主体,以及 (b) 如何匹配显示的所有不同类型:

```scala
// getClassAsString 是一个接受任何类型的单个参数的方法。
def getClassAsString(x: Matchable): String = x match
case s: String => s"'$s' is a String"
case i: Int => "Int"
case d: Double => "Double"
case l: List[_] => "List"
case _ => "Unknown"

// examples
getClassAsString(1) // Int
getClassAsString("hello") // 'hello' is a String
getClassAsString(List(1, 2, 3)) // List
```

`getClassAsString` 方法将 [Matchable]({{ site.scala3ref }}/other-new-features/matchable.html) 类型的值作为参数,它可以是
任何支持模式匹配的类型(某些类型不支持模式匹配,因为这可能
打破封装)。

Scala 中的模式匹配还有 _更多_ 内容。
模式可以嵌套,模式的结果可以绑定,模式匹配甚至可以是用户自定义的。
有关详细信息,请参阅 [控制结构章节][control] 中的模式匹配示例。

## `try`/`catch`/`finally`

Scala 的 `try`/`catch`/`finally` 控制结构让你捕获异常。
它类似于 Java,但其语法与 `match` 表达式一致:

```scala
try
writeTextToFile(text)
catch
case ioe: IOException => println("Got an IOException.")
case nfe: NumberFormatException => println("Got a NumberFormatException.")
finally
println("Clean up your resources here.")
```

## `while` 循环

Scala 还有一个 `while` 循环结构。
它的单行语法如下所示:

```scala
while x >= 0 do x = f(x)
```

在 Scala 2 中,语法有点不同。条件用括号括起来,并且
没有 `do` 关键字:

```scala
while (x >= 0) { x = f(x) }
```

为了兼容性,Scala 3 仍然支持 Scala 2 语法。

`while` 循环多行语法如下所示:

```scala
var x = 1

while
x < 3
do
println(x)
x += 1
```

## 自定义控制结构

由于 by-name 参数、中缀表示法、流畅接口、可选括号、扩展方法和高阶函数等功能,您还可以创建自己的代码,就像控制结构一样工作。
您将在 [控制结构][control] 部分了解更多信息。

[control]: {% link _overviews/scala3-book/control-structures.md %}
62 changes: 62 additions & 0 deletions _zh-cn/overviews/scala3-book/taste-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: First-Class Functions
type: section
description: This page provides an introduction to functions in Scala 3.
num: 11
previous-page: taste-methods
next-page: taste-objects
---


Scala具有函数式编程语言中您期望的大多数功能,包括:

- Lambdas(匿名函数)
- 高阶函数 (HOFs)
- 标准库中的不可变集合

Lambdas(也称为 _匿名函数_)是保持代码简洁但可读性的重要组成部分。

`List` 类的 `map` 方法是高阶函数的典型示例---一个将函数作为参数的函数。

这两个示例是等效的,并演示如何通过将 lambda 传递到 `map` 方法中,将列表中的每个数字乘以 `2`:

```scala
val a = List(1, 2, 3).map(i => i * 2) // List(2,4,6)
val b = List(1, 2, 3).map(_ * 2) // List(2,4,6)
```

这些示例也等效于以下代码,该代码使用 `double` 方法而不是lambda:

```scala
def double(i: Int): Int = i * 2

val a = List(1, 2, 3).map(i => double(i)) // List(2,4,6)
val b = List(1, 2, 3).map(double) // List(2,4,6)
```

> 如果您以前从未见过 `map` 方法,它会将给定的函数应用于列表中的每个元素,从而生成一个包含结果值的新列表。

将 lambda 传递给集合类上的高阶函数(如 `List`)是 Scala 体验的一部分,您每天都会这样做。

## 不可变集合

当您使用不可变集合(如 `List`,`Vector`)以及不可变的 `Map` 和 `Set` 类时,重要的是要知道这些函数不会改变它们被调用的集合;相反,它们返回包含更新数据的新集合。
因此,以“流式”的风格将它们链接在一起以解决问题也很常见。

例如,此示例演示如何对一个集合进行两次筛选,然后将其余集合中的每个元素乘某个数:

```scala
// a sample list
val nums = (1 to 10).toList // List(1,2,3,4,5,6,7,8,9,10)

// methods can be chained together as needed
val x = nums.filter(_ > 3)
.filter(_ < 7)
.map(_ * 10)

// result: x == List(40, 50, 60)
```

除了在整个标准库中使用的高阶函数外,您还可以[创建自己的][higher-order] 高阶函数。

[higher-order]: {% link _overviews/scala3-book/fun-hofs.md %}
Loading