Skip to content

add code tab in _zh-cn/overviews/scala3-book/fp-pure-functions.md #2749

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 all 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
19 changes: 18 additions & 1 deletion _zh-cn/overviews/scala3-book/fp-pure-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Scala 提供的另一个帮助您编写函数式代码的特性是编写纯函

这些 `String` 方法也是纯函数:

-`isEmpty`
- `isEmpty`
- `length`
- `substring`

Expand Down Expand Up @@ -85,17 +85,34 @@ Scala 集合类上的大多数方法也可以作为纯函数工作,包括 `dro
要在 Scala 中编写纯函数,只需使用 Scala 的方法语法编写它们(尽管您也可以使用 Scala 的函数语法)。
例如,这是一个将给定输入值加倍的纯函数:

{% tabs fp-pure-function %}
{% tab 'Scala 2 and 3' %}
```scala
def double(i: Int): Int = i * 2
```
{% endtab %}
{% endtabs %}

如果您对递归感到满意,这是一个计算整数列表之和的纯函数:

{% tabs fp-pure-recursive-function class=tabs-scala-version %}
{% tab 'Scala 2' %}
```scala
def sum(xs: List[Int]): Int = xs match {
case Nil => 0
case head :: tail => head + sum(tail)
}
```
{% endtab %}

{% tab 'Scala 3' %}
```scala
def sum(xs: List[Int]): Int = xs match
case Nil => 0
case head :: tail => head + sum(tail)
```
{% endtab %}
{% endtabs %}

如果您理解该代码,您会发现它符合纯函数定义。

Expand Down