-
Notifications
You must be signed in to change notification settings - Fork 1k
Scala 2 Book migration - Control Structures #3126
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 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3d80ecd
Scala 2 Book migration - Control Structures
gkepka 38ac7a3
Add match following a period example and remove languages from Partia…
gkepka f05349a
Apply suggestions from review
gkepka e803479
Update _overviews/scala3-book/control-structures.md
gkepka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: Partial Functions | ||
type: section | ||
description: This page shows how to use function variables in Scala. | ||
gkepka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
languages: [ru, zh-cn] | ||
gkepka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 for an argument, use the `isDefinedAt` method: | ||
gkepka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
{% 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 %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.