Skip to content

Simplify Chinese translation of Scala Tour: tuples.md #1217

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 5 commits into from
Nov 29, 2018
Merged
Changes from 2 commits
Commits
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
83 changes: 80 additions & 3 deletions _zh-cn/tour/tuples.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: tour
title: Tuples
title: 元组

discourse: true

Expand All @@ -15,5 +15,82 @@ previous-page: traits
topics: tuples
---

(this section of the tour has not been translated yet. pull request
with translation welcome!)
在 Scala 中,元组是一个可以容纳不同类型元素的类。
元组是不可变的。

当我们必须从函数返回多个值时,元组会派上用场。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必须-> 需要,这样读起来更顺些

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot.


元组可以创建如下:

```tut
val ingredient = ("Sugar" , 25):Tuple2[String, Int]
```
这将创建一个包含一个 String 元素和一个 Int 元素的元组。

Scala 中的元组包含一系列类:Tuple2,Tuple3等,直到 Tuple22。
因此,当我们创建一个包含 n 个元素(n 位于 2 和 22 之间)的元组时,Scala 基本上就是从上述的一组类中实例化
一个相对应的类,使用组成元素的类型进行参数化。
上例中,`ingredient` 的类型为 `Tuple2[String, Int]`。

## 访问元素

使用下划线语法来访问元组中的元素。
'tuple._n' 取出了第 n 个元素(假设有足够多元素)。

```tut
println(ingredient._1) // Sugar

println(ingredient._2) // 25
```

## 解构元组数据

Scala 元组也支持解构。

```tut
val (name, quantity) = ingredient

println(name) // Sugar

println(quantity) // 25
```

元组解构也可用于模式匹配。

```tut
val planetDistanceFromSun = List(("Mercury", 57.9), ("Venus", 108.2), ("Earth", 149.6 ), ("Mars", 227.9), ("Jupiter", 778.3))

planetDistanceFromSun.foreach{ tuple => {

tuple match {

case ("Mercury", distance) => println(s"Mercury is $distance millions km far from Sun")

case p if(p._1 == "Venus") => println(s"Venus is ${p._2} millions km far from Sun")

case p if(p._1 == "Earth") => println(s"Blue planet is ${p._2} millions km far from Sun")

case _ => println("Too far....")

}

}

}
```

或者,在 'for' 推导中。

```tut
val numPairs = List((2, 5), (3, -7), (20, 56))

for ((a, b) <- numPairs) {

println(a * b)

}
```

类型 `Unit` 的值 `()` 在概念上与类型 `Tuple0` 的值 `()` 相同。 `Tuple0` 只能有一个值,因为它没有元素。

用户有时可能在元组和案例类之间难以选择。 通常,如果元素具有更多含义,则首选案例类。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

case class 一般翻译为 case 类 or 样本类。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里至少要和已有的翻译一致

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不确定已有的翻译是什么,能帮我指出来哪里有吗,谢谢

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

現在已經有很多 Scala 中文書籍,大家對 case class 有比較一致的翻譯,可參考《Scala 實用指南》。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

案例类肯定不对

使用 FastHub 从我的 Galaxy Note8 发送

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

谢谢 @satansk ,就翻成 case 类,简单明了