Skip to content

Commit 3d80ecd

Browse files
committed
Scala 2 Book migration - Control Structures
1 parent c35f5df commit 3d80ecd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+174
-49
lines changed

Diff for: _overviews/scala3-book/ca-context-bounds.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Context Bounds
33
type: section
44
description: This page demonstrates Context Bounds in Scala.
55
languages: [ru, zh-cn]
6-
num: 62
6+
num: 63
77
previous-page: ca-context-parameters
88
next-page: ca-given-imports
99
---

Diff for: _overviews/scala3-book/ca-context-parameters.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Context Parameters
33
type: section
44
description: This page demonstrates how to declare context parameters, and how the compiler infers them at call-site.
55
languages: [ru, zh-cn]
6-
num: 61
6+
num: 62
77
previous-page: ca-extension-methods
88
next-page: ca-context-bounds
99
redirect_from: /scala3/book/ca-given-using-clauses.html

Diff for: _overviews/scala3-book/ca-contextual-abstractions-intro.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Contextual Abstractions
33
type: chapter
44
description: This chapter provides an introduction to the Scala 3 concept of Contextual Abstractions.
55
languages: [ru, zh-cn]
6-
num: 59
6+
num: 60
77
previous-page: types-others
88
next-page: ca-extension-methods
99
---

Diff for: _overviews/scala3-book/ca-extension-methods.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Extension Methods
33
type: section
44
description: This page demonstrates how Extension Methods work in Scala 3.
55
languages: [ru, zh-cn]
6-
num: 60
6+
num: 61
77
previous-page: ca-contextual-abstractions-intro
88
next-page: ca-context-parameters
99
scala3: true

Diff for: _overviews/scala3-book/ca-given-imports.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Given Imports
33
type: section
44
description: This page demonstrates how 'given' import statements work in Scala 3.
55
languages: [ru, zh-cn]
6-
num: 63
6+
num: 64
77
previous-page: ca-context-bounds
88
next-page: ca-type-classes
99
scala3: true

Diff for: _overviews/scala3-book/ca-implicit-conversions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Implicit Conversions
33
type: section
44
description: This page demonstrates how to implement Implicit Conversions in Scala.
55
languages: [ru, zh-cn]
6-
num: 66
6+
num: 67
77
previous-page: ca-multiversal-equality
88
next-page: ca-summary
99
---

Diff for: _overviews/scala3-book/ca-multiversal-equality.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Multiversal Equality
33
type: section
44
description: This page demonstrates how to implement Multiversal Equality in Scala 3.
55
languages: [ru, zh-cn]
6-
num: 65
6+
num: 66
77
previous-page: ca-type-classes
88
next-page: ca-implicit-conversions
99
scala3: true

Diff for: _overviews/scala3-book/ca-summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Summary
33
type: section
44
description: This page provides a summary of the Contextual Abstractions lessons.
55
languages: [ru, zh-cn]
6-
num: 67
6+
num: 68
77
previous-page: ca-implicit-conversions
88
next-page: concurrency
99
---

Diff for: _overviews/scala3-book/ca-type-classes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Type Classes
33
type: section
44
description: This page demonstrates how to create and use type classes.
55
languages: [ru, zh-cn]
6-
num: 64
6+
num: 65
77
previous-page: ca-given-imports
88
next-page: ca-multiversal-equality
99
redirect_from: /scala3/book/types-type-classes.html

Diff for: _overviews/scala3-book/collections-classes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Collections Types
33
type: section
44
description: This page introduces the common Scala 3 collections types and some of their methods.
55
languages: [ru, zh-cn]
6-
num: 38
6+
num: 39
77
previous-page: collections-intro
88
next-page: collections-methods
99
---

Diff for: _overviews/scala3-book/collections-intro.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Scala Collections
33
type: chapter
44
description: This page provides and introduction to the common collections classes and their methods in Scala 3.
55
languages: [ru, zh-cn]
6-
num: 37
6+
num: 38
77
previous-page: packaging-imports
88
next-page: collections-classes
99
---

Diff for: _overviews/scala3-book/collections-methods.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Collections Methods
33
type: section
44
description: This page demonstrates the common methods on the Scala 3 collections classes.
55
languages: [ru, zh-cn]
6-
num: 39
6+
num: 40
77
previous-page: collections-classes
88
next-page: collections-summary
99
---

Diff for: _overviews/scala3-book/collections-summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Summary
33
type: section
44
description: This page provides a summary of the Collections chapter.
55
languages: [ru, zh-cn]
6-
num: 40
6+
num: 41
77
previous-page: collections-methods
88
next-page: fp-intro
99
---

Diff for: _overviews/scala3-book/concurrency.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Concurrency
33
type: chapter
44
description: This page discusses how Scala concurrency works, with an emphasis on Scala Futures.
55
languages: [ru, zh-cn]
6-
num: 68
6+
num: 69
77
previous-page: ca-summary
88
next-page: scala-tools
99
---

Diff for: _overviews/scala3-book/control-structures.md

+51-8
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ speak(Person("Bam Bam")) // "Bam Bam says, Bam bam!"
812812

813813
#### Binding matched patterns to variables
814814

815-
You can bind the matched pattern to a variable to use type-specific behavior.
815+
You can bind the matched pattern to a variable to use type-specific behavior:
816816

817817
{% tabs pattern-binding class=tabs-scala-version %}
818818
{% tab 'Scala 2' for=pattern-binding %}
@@ -992,10 +992,53 @@ def pattern(x: Matchable): String = x match
992992
{% endtab %}
993993
{% endtabs %}
994994

995-
{% comment %}
996-
TODO: Add in the new Scala 3 syntax shown on this page:
997-
http://dotty.epfl.ch/docs/reference/changed-features/match-syntax.html
998-
{% endcomment %}
995+
You can also write the code on the right side of the `=>` on multiple lines if you think is easier to read. Here’s one example:
996+
997+
{% tabs control-structures-31 class=tabs-scala-version %}
998+
{% tab 'Scala 2' for=control-structures-31 %}
999+
```scala
1000+
count match {
1001+
case 1 =>
1002+
println("one, a lonely number")
1003+
case x if x == 2 || x == 3 =>
1004+
println("two's company, three's a crowd")
1005+
case x if x > 3 =>
1006+
println("4+, that's a party")
1007+
case _ =>
1008+
println("i'm guessing your number is zero or less")
1009+
}
1010+
```
1011+
{% endtab %}
1012+
{% tab 'Scala 3' for=control-structures-31 %}
1013+
```scala
1014+
count match
1015+
case 1 =>
1016+
println("one, a lonely number")
1017+
case x if x == 2 || x == 3 =>
1018+
println("two's company, three's a crowd")
1019+
case x if x > 3 =>
1020+
println("4+, that's a party")
1021+
case _ =>
1022+
println("i'm guessing your number is zero or less")
1023+
```
1024+
{% endtab %}
1025+
{% endtabs %}
1026+
1027+
In Scala 3, `match` expressions can be chained:
1028+
1029+
{% tabs 'control-structures-32' %}
1030+
{% tab 'Scala 3 Only' %}
1031+
```scala
1032+
i match
1033+
case odd: Int if odd % 2 == 1 => "odd"
1034+
case even: Int if even % 2 == 0 => "even"
1035+
case _ => "not an integer"
1036+
match
1037+
case "even" => true
1038+
case _ => false
1039+
```
1040+
{% endtab %}
1041+
{% endtabs %}
9991042

10001043
## try/catch/finally
10011044

@@ -1004,8 +1047,8 @@ For consistency, Scala uses the same syntax that `match` expressions use and sup
10041047

10051048
In the following example, `openAndReadAFile` is a method that does what its name implies: it opens a file and reads the text in it, assigning the result to the mutable variable `text`:
10061049

1007-
{% tabs control-structures-31 class=tabs-scala-version %}
1008-
{% tab 'Scala 2' for=control-structures-31 %}
1050+
{% tabs control-structures-33 class=tabs-scala-version %}
1051+
{% tab 'Scala 2' for=control-structures-33 %}
10091052
```scala
10101053
var text = ""
10111054
try {
@@ -1019,7 +1062,7 @@ try {
10191062
}
10201063
```
10211064
{% endtab %}
1022-
{% tab 'Scala 3' for=control-structures-31 %}
1065+
{% tab 'Scala 3' for=control-structures-33 %}
10231066
```scala
10241067
var text = ""
10251068
try

Diff for: _overviews/scala3-book/fun-eta-expansion.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Eta-Expansion
33
type: section
44
description: This page discusses Eta-Expansion, the Scala technology that automatically and transparently converts methods into functions.
55
languages: [ru, zh-cn]
6-
num: 31
6+
num: 32
77
previous-page: fun-function-variables
88
next-page: fun-hofs
99
---

Diff for: _overviews/scala3-book/fun-function-variables.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: This page shows how to use function variables in Scala.
55
languages: [ru, zh-cn]
66
num: 30
77
previous-page: fun-anonymous-functions
8-
next-page: fun-eta-expansion
8+
next-page: fun-partial-functions
99
---
1010

1111

Diff for: _overviews/scala3-book/fun-hofs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Higher-Order Functions
33
type: section
44
description: This page demonstrates how to create and use higher-order functions in Scala.
55
languages: [ru, zh-cn]
6-
num: 32
6+
num: 33
77
previous-page: fun-eta-expansion
88
next-page: fun-write-map-function
99
---

Diff for: _overviews/scala3-book/fun-intro.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ next-page: fun-anonymous-functions
1010

1111

1212
Where the previous chapter introduced Scala *methods*, this chapter digs into *functions*.
13-
The topics that are covered include anonymous functions, function variables, and higher-order functions (HOFs), including how to create your own HOFs.
13+
The topics that are covered include anonymous functions, partial functions, function variables, and higher-order functions (HOFs), including how to create your own HOFs.
1414

Diff for: _overviews/scala3-book/fun-partial-functions.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: Partial Functions
3+
type: section
4+
description: This page shows how to use function variables in Scala.
5+
languages: [ru, zh-cn]
6+
num: 31
7+
previous-page: fun-function-variables
8+
next-page: fun-eta-expansion
9+
---
10+
11+
A partial function is a function that may not be defined for all values of its argument type. In Scala, partial functions
12+
are unary functions implementing the `PartialFunction[A, B]` trait, where `A` is the argument type and `B` the result type.
13+
14+
To define a partial function, use a `case` identical to those used in `match` expressions:
15+
16+
{% tabs fun-partial-1 %}
17+
{% tab 'Scala 2 and 3' %}
18+
```scala
19+
val doubledOdds: PartialFunction[Int, Int] = {
20+
case i if i % 2 == 1 => i * 2
21+
}
22+
```
23+
{% endtab %}
24+
{% endtabs %}
25+
26+
To check if a partial function is for an argument, use the `isDefinedAt` method:
27+
28+
{% tabs fun-partial-2 %}
29+
{% tab 'Scala 2 and 3' %}
30+
```scala
31+
doubledOdds.isDefinedAt(3) // true
32+
doubledOdds.isDefinedAt(4) // false
33+
```
34+
{% endtab %}
35+
{% endtabs %}
36+
37+
Trying to apply a partial function to an argument not belonging to its domain results in `MatchError`:
38+
39+
{% tabs fun-partial-3 %}
40+
{% tab 'Scala 2 and 3' %}
41+
```scala
42+
doubledOdds(4) // Exception in thread "main" scala.MatchError: 4
43+
```
44+
{% endtab %}
45+
{% endtabs %}
46+
47+
### Using partial functions
48+
49+
A partial function can be passed as an argument to a method:
50+
51+
{% tabs fun-partial-4 %}
52+
{% tab 'Scala 2 and 3' %}
53+
```scala
54+
val res = List(1, 2, 3).collect({ case i if i % 2 == 1 => i * 2 }) // List(2, 6)
55+
```
56+
{% endtab %}
57+
{% endtabs %}
58+
59+
You can define a default value for arguments not in domain with `applyOrElse`:
60+
61+
{% tabs fun-partial-5 %}
62+
{% tab 'Scala 2 and 3' %}
63+
```scala
64+
doubledOdds.applyOrElse(4, _ + 1) // 5
65+
```
66+
{% endtab %}
67+
{% endtabs %}
68+
69+
Two partial function can be composed with `orElse`, the second function will be applied for arguments where the first
70+
one is not defined:
71+
72+
{% tabs fun-partial-6 %}
73+
{% tab 'Scala 2 and 3' %}
74+
```scala
75+
val incrementedEvens: PartialFunction[Int, Int] = {
76+
case i if i % 2 == 0 => i + 1
77+
}
78+
79+
val res2 = List(1, 2, 3).collect(doubledOdds.orElse(incrementedEvens)) // List(2, 3, 6)
80+
```
81+
{% endtab %}
82+
{% endtabs %}

Diff for: _overviews/scala3-book/fun-summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Summary
33
type: section
44
description: This page provides a summary of the previous 'Functions' sections.
55
languages: [ru, zh-cn]
6-
num: 35
6+
num: 36
77
previous-page: fun-write-method-returns-function
88
next-page: packaging-imports
99
---

Diff for: _overviews/scala3-book/fun-write-map-function.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Write Your Own map Method
33
type: section
44
description: This page demonstrates how to create and use higher-order functions in Scala.
55
languages: [ru, zh-cn]
6-
num: 33
6+
num: 34
77
previous-page: fun-hofs
88
next-page: fun-write-method-returns-function
99
---

Diff for: _overviews/scala3-book/fun-write-method-returns-function.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Creating a Method That Returns a Function
33
type: section
44
description: This page demonstrates how to create and use higher-order functions in Scala.
55
languages: [ru, zh-cn]
6-
num: 34
6+
num: 35
77
previous-page: fun-write-map-function
88
next-page: fun-summary
99
---

Diff for: _overviews/scala3-book/interacting-with-java.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Interacting with Java
33
type: chapter
44
description: This page demonstrates how Scala code can interact with Java, and how Java code can interact with Scala code.
55
languages: [ru, zh-cn]
6-
num: 72
6+
num: 73
77
previous-page: tools-worksheets
88
next-page: scala-for-java-devs
99
---

Diff for: _overviews/scala3-book/packaging-imports.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Packaging and Imports
33
type: chapter
44
description: A discussion of using packages and imports to organize your code, build related modules of code, control scope, and help prevent namespace collisions.
55
languages: [ru, zh-cn]
6-
num: 36
6+
num: 37
77
previous-page: fun-summary
88
next-page: collections-intro
99
---

Diff for: _overviews/scala3-book/scala-for-java-devs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Scala for Java Developers
33
type: chapter
44
description: This page is for Java developers who are interested in learning about Scala 3.
55
languages: [zh-cn]
6-
num: 73
6+
num: 74
77
previous-page: interacting-with-java
88
next-page: scala-for-javascript-devs
99
---

Diff for: _overviews/scala3-book/scala-for-javascript-devs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Scala for JavaScript Developers
33
type: chapter
44
description: This chapter provides an introduction to Scala 3 for JavaScript developers
55
languages: [zh-cn]
6-
num: 74
6+
num: 75
77
previous-page: scala-for-java-devs
88
next-page: scala-for-python-devs
99
---

0 commit comments

Comments
 (0)