Skip to content

Commit

Permalink
Merge pull request #3126 from gkepka/scala-book-control-structures
Browse files Browse the repository at this point in the history
Scala 2 Book migration - Control Structures
  • Loading branch information
gkepka authored Dec 10, 2024
2 parents c35f5df + e803479 commit 70efd3e
Show file tree
Hide file tree
Showing 43 changed files with 188 additions and 49 deletions.
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-context-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Context Bounds
type: section
description: This page demonstrates Context Bounds in Scala.
languages: [ru, zh-cn]
num: 62
num: 63
previous-page: ca-context-parameters
next-page: ca-given-imports
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-context-parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Context Parameters
type: section
description: This page demonstrates how to declare context parameters, and how the compiler infers them at call-site.
languages: [ru, zh-cn]
num: 61
num: 62
previous-page: ca-extension-methods
next-page: ca-context-bounds
redirect_from: /scala3/book/ca-given-using-clauses.html
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-contextual-abstractions-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Contextual Abstractions
type: chapter
description: This chapter provides an introduction to the Scala 3 concept of Contextual Abstractions.
languages: [ru, zh-cn]
num: 59
num: 60
previous-page: types-others
next-page: ca-extension-methods
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-extension-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Extension Methods
type: section
description: This page demonstrates how Extension Methods work in Scala 3.
languages: [ru, zh-cn]
num: 60
num: 61
previous-page: ca-contextual-abstractions-intro
next-page: ca-context-parameters
scala3: true
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-given-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Given Imports
type: section
description: This page demonstrates how 'given' import statements work in Scala 3.
languages: [ru, zh-cn]
num: 63
num: 64
previous-page: ca-context-bounds
next-page: ca-type-classes
scala3: true
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-implicit-conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Implicit Conversions
type: section
description: This page demonstrates how to implement Implicit Conversions in Scala.
languages: [ru, zh-cn]
num: 66
num: 67
previous-page: ca-multiversal-equality
next-page: ca-summary
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-multiversal-equality.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Multiversal Equality
type: section
description: This page demonstrates how to implement Multiversal Equality in Scala 3.
languages: [ru, zh-cn]
num: 65
num: 66
previous-page: ca-type-classes
next-page: ca-implicit-conversions
scala3: true
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Summary
type: section
description: This page provides a summary of the Contextual Abstractions lessons.
languages: [ru, zh-cn]
num: 67
num: 68
previous-page: ca-implicit-conversions
next-page: concurrency
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/ca-type-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Type Classes
type: section
description: This page demonstrates how to create and use type classes.
languages: [ru, zh-cn]
num: 64
num: 65
previous-page: ca-given-imports
next-page: ca-multiversal-equality
redirect_from: /scala3/book/types-type-classes.html
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/collections-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Collections Types
type: section
description: This page introduces the common Scala 3 collections types and some of their methods.
languages: [ru, zh-cn]
num: 38
num: 39
previous-page: collections-intro
next-page: collections-methods
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/collections-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Scala Collections
type: chapter
description: This page provides and introduction to the common collections classes and their methods in Scala 3.
languages: [ru, zh-cn]
num: 37
num: 38
previous-page: packaging-imports
next-page: collections-classes
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/collections-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Collections Methods
type: section
description: This page demonstrates the common methods on the Scala 3 collections classes.
languages: [ru, zh-cn]
num: 39
num: 40
previous-page: collections-classes
next-page: collections-summary
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/collections-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Summary
type: section
description: This page provides a summary of the Collections chapter.
languages: [ru, zh-cn]
num: 40
num: 41
previous-page: collections-methods
next-page: fp-intro
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/concurrency.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Concurrency
type: chapter
description: This page discusses how Scala concurrency works, with an emphasis on Scala Futures.
languages: [ru, zh-cn]
num: 68
num: 69
previous-page: ca-summary
next-page: scala-tools
---
Expand Down
74 changes: 66 additions & 8 deletions _overviews/scala3-book/control-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ speak(Person("Bam Bam")) // "Bam Bam says, Bam bam!"

#### Binding matched patterns to variables

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

{% tabs pattern-binding class=tabs-scala-version %}
{% tab 'Scala 2' for=pattern-binding %}
Expand Down Expand Up @@ -992,10 +992,68 @@ def pattern(x: Matchable): String = x match
{% endtab %}
{% endtabs %}

{% comment %}
TODO: Add in the new Scala 3 syntax shown on this page:
http://dotty.epfl.ch/docs/reference/changed-features/match-syntax.html
{% endcomment %}
You can also write the code on the right side of the `=>` on multiple lines if you think it is easier to read. Here is one example:

{% tabs control-structures-31 class=tabs-scala-version %}
{% tab 'Scala 2' for=control-structures-31 %}
```scala
count match {
case 1 =>
println("one, a lonely number")
case x if x == 2 || x == 3 =>
println("two's company, three's a crowd")
case x if x > 3 =>
println("4+, that's a party")
case _ =>
println("i'm guessing your number is zero or less")
}
```
{% endtab %}
{% tab 'Scala 3' for=control-structures-31 %}
```scala
count match
case 1 =>
println("one, a lonely number")
case x if x == 2 || x == 3 =>
println("two's company, three's a crowd")
case x if x > 3 =>
println("4+, that's a party")
case _ =>
println("i'm guessing your number is zero or less")
```
{% endtab %}
{% endtabs %}

In Scala 3, `match` expressions can be chained:

{% tabs 'control-structures-32' %}
{% tab 'Scala 3 Only' %}
```scala
i match
case odd: Int if odd % 2 == 1 => "odd"
case even: Int if even % 2 == 0 => "even"
case _ => "not an integer"
match
case "even" => true
case _ => false
```
{% endtab %}
{% endtabs %}

The `match` expression can also follow a period, which simplifies matching on results returned by chained method calls:

{% tabs 'control-structures-33' %}
{% tab 'Scala 3 Only' %}
```scala
List(1, 2, 3)
.map(_ * 2)
.headOption
.match
case Some(value) => println(s"The head is: $value")
case None => println("The list is empty")
```
{% endtab %}
{% endtabs %}

## try/catch/finally

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

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`:

{% tabs control-structures-31 class=tabs-scala-version %}
{% tab 'Scala 2' for=control-structures-31 %}
{% tabs control-structures-34 class=tabs-scala-version %}
{% tab 'Scala 2' for=control-structures-34 %}
```scala
var text = ""
try {
Expand All @@ -1019,7 +1077,7 @@ try {
}
```
{% endtab %}
{% tab 'Scala 3' for=control-structures-31 %}
{% tab 'Scala 3' for=control-structures-34 %}
```scala
var text = ""
try
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/fun-eta-expansion.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Eta-Expansion
type: section
description: This page discusses Eta-Expansion, the Scala technology that automatically and transparently converts methods into functions.
languages: [ru, zh-cn]
num: 31
num: 32
previous-page: fun-function-variables
next-page: fun-hofs
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/fun-function-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: This page shows how to use function variables in Scala.
languages: [ru, zh-cn]
num: 30
previous-page: fun-anonymous-functions
next-page: fun-eta-expansion
next-page: fun-partial-functions
---


Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/fun-hofs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Higher-Order Functions
type: section
description: This page demonstrates how to create and use higher-order functions in Scala.
languages: [ru, zh-cn]
num: 32
num: 33
previous-page: fun-eta-expansion
next-page: fun-write-map-function
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/fun-intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ next-page: fun-anonymous-functions


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

81 changes: 81 additions & 0 deletions _overviews/scala3-book/fun-partial-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: Partial Functions
type: section
description: This page shows how to use partial functions in Scala.
num: 31
previous-page: fun-function-variables
next-page: fun-eta-expansion
---

A partial function is a function that may not be defined for all values of its argument type. In Scala, partial functions
are unary functions implementing the `PartialFunction[A, B]` trait, where `A` is the argument type and `B` the result type.

To define a partial function, use a `case` identical to those used in `match` expressions:

{% tabs fun-partial-1 %}
{% tab 'Scala 2 and 3' %}
```scala
val doubledOdds: PartialFunction[Int, Int] = {
case i if i % 2 == 1 => i * 2
}
```
{% endtab %}
{% endtabs %}

To check if a partial function is defined for an argument, use the `isDefinedAt` method:

{% tabs fun-partial-2 %}
{% tab 'Scala 2 and 3' %}
```scala
doubledOdds.isDefinedAt(3) // true
doubledOdds.isDefinedAt(4) // false
```
{% endtab %}
{% endtabs %}

Trying to apply a partial function to an argument not belonging to its domain results in `MatchError`:

{% tabs fun-partial-3 %}
{% tab 'Scala 2 and 3' %}
```scala
doubledOdds(4) // Exception in thread "main" scala.MatchError: 4
```
{% endtab %}
{% endtabs %}

### Using partial functions

A partial function can be passed as an argument to a method:

{% tabs fun-partial-4 %}
{% tab 'Scala 2 and 3' %}
```scala
val res = List(1, 2, 3).collect({ case i if i % 2 == 1 => i * 2 }) // List(2, 6)
```
{% endtab %}
{% endtabs %}

You can define a default value for arguments not in domain with `applyOrElse`:

{% tabs fun-partial-5 %}
{% tab 'Scala 2 and 3' %}
```scala
doubledOdds.applyOrElse(4, _ + 1) // 5
```
{% endtab %}
{% endtabs %}

Two partial function can be composed with `orElse`, the second function will be applied for arguments where the first
one is not defined:

{% tabs fun-partial-6 %}
{% tab 'Scala 2 and 3' %}
```scala
val incrementedEvens: PartialFunction[Int, Int] = {
case i if i % 2 == 0 => i + 1
}

val res2 = List(1, 2, 3).collect(doubledOdds.orElse(incrementedEvens)) // List(2, 3, 6)
```
{% endtab %}
{% endtabs %}
2 changes: 1 addition & 1 deletion _overviews/scala3-book/fun-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Summary
type: section
description: This page provides a summary of the previous 'Functions' sections.
languages: [ru, zh-cn]
num: 35
num: 36
previous-page: fun-write-method-returns-function
next-page: packaging-imports
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/fun-write-map-function.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Write Your Own map Method
type: section
description: This page demonstrates how to create and use higher-order functions in Scala.
languages: [ru, zh-cn]
num: 33
num: 34
previous-page: fun-hofs
next-page: fun-write-method-returns-function
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Creating a Method That Returns a Function
type: section
description: This page demonstrates how to create and use higher-order functions in Scala.
languages: [ru, zh-cn]
num: 34
num: 35
previous-page: fun-write-map-function
next-page: fun-summary
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/interacting-with-java.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Interacting with Java
type: chapter
description: This page demonstrates how Scala code can interact with Java, and how Java code can interact with Scala code.
languages: [ru, zh-cn]
num: 72
num: 73
previous-page: tools-worksheets
next-page: scala-for-java-devs
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/packaging-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Packaging and Imports
type: chapter
description: A discussion of using packages and imports to organize your code, build related modules of code, control scope, and help prevent namespace collisions.
languages: [ru, zh-cn]
num: 36
num: 37
previous-page: fun-summary
next-page: collections-intro
---
Expand Down
2 changes: 1 addition & 1 deletion _overviews/scala3-book/scala-for-java-devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Scala for Java Developers
type: chapter
description: This page is for Java developers who are interested in learning about Scala 3.
languages: [zh-cn]
num: 73
num: 74
previous-page: interacting-with-java
next-page: scala-for-javascript-devs
---
Expand Down
Loading

0 comments on commit 70efd3e

Please sign in to comment.