-
-
Notifications
You must be signed in to change notification settings - Fork 194
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
Kotlin / Bob / DigDeeper: Add a pattern matching approach using when-expression #629
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
daae9f0
Add approach to introduction
micha-b 02ff7d9
Add snippet.txt and content.md
micha-b b590b6c
Add entry in config.json
micha-b c607a14
Fix typo in config.json
micha-b 5ddccb1
Reduce snippet length to 8 lines
micha-b 9d3d113
Use proposed one-liner in function isYelling
micha-b 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 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 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
54 changes: 54 additions & 0 deletions
54
exercises/practice/bob/.approaches/when-expression/content.md
This file contains 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,54 @@ | ||
# `when` expressions | ||
|
||
```kotlin | ||
object Bob { | ||
fun hey(statement: String): String = | ||
when { | ||
statement.isQuestion() && statement.isYelling() -> "Calm down, I know what I'm doing!" | ||
statement.isQuestion() -> "Sure." | ||
statement.isYelling() -> "Whoa, chill out!" | ||
statement.isSilence() -> "Fine. Be that way!" | ||
else -> "Whatever." | ||
} | ||
|
||
private fun String.isSilence(): Boolean = this.isBlank() | ||
private fun String.isQuestion(): Boolean = this.trim().endsWith('?') | ||
private fun String.isYelling(): Boolean = any(Char::isLetter) && toUpperCase() == this | ||
} | ||
``` | ||
|
||
In this approach you have a `when` expression containing different so-called branches that can be matched using the corresponding patterns. | ||
As soon as one of these patterns on the left side of the arrow (`->`) is matched ... | ||
|
||
1. the value on the right side | ||
2. the block on the right side is executed and the value retuned from the block | ||
|
||
... is returned from the `when` expression. | ||
|
||
Only one branch is matched in one execution of the `when` expression. The branches are matched from top to bottom and the first branch in which the condition evaluates to `true` is selected. | ||
If none of the given conditions matches the `else` branch is selected and returned. | ||
|
||
~~~~exercism/caution | ||
Depending on what patterns are on the left side of your branches the order of branches is important to the correct execution of the `when` expression since the first matching branch is selected. | ||
~~~~ | ||
|
||
An [object declaration][object] is used to define `Bob` as essentially a [singleton][singleton] object instantiation of the class. | ||
This is sufficient, since there is no object state that needs to change with each call of the `hey` method. | ||
|
||
Inside this object there are some `private` [extension methods as members][extension-members] of the `object`. This adds these methods to the `String` data type for `private` usage of these methods in this `object`. This allows calling the methods directly on the `String` instead of passing the `String` to the method. | ||
(More about extension methods in general [here][extension-general]) | ||
|
||
These extension methods check for: | ||
|
||
1. `isSilence()`: a blank string | ||
2. `isQuestion()`: a string with the last non-whitespace-character being a question mark | ||
3. `isYelling()`: a string with all letters in uppercase | ||
|
||
When combining these methods as in the `when` expression above you can map all the cases required by the exercise. | ||
|
||
|
||
[when]: https://kotlinlang.org/docs/control-flow.html#when-expression | ||
[object]: https://kotlinlang.org/docs/object-declarations.html#object-declarations-overview | ||
[singleton]: https://en.wikipedia.org/wiki/Singleton_pattern | ||
[extension-members]: https://kotlinlang.org/docs/extensions.html#declaring-extensions-as-members | ||
[extension-general]: https://kotlinlang.org/docs/extensions.html |
8 changes: 8 additions & 0 deletions
8
exercises/practice/bob/.approaches/when-expression/snippet.txt
This file contains 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,8 @@ | ||
fun hey(statement: String): String = | ||
when { | ||
statement.isQuestion() && statement.isYelling() -> "Calm down, I know what I'm doing!" | ||
statement.isQuestion() -> "Sure." | ||
statement.isYelling() -> "Whoa, chill out!" | ||
statement.isSilence() -> "Fine. Be that way!" | ||
else -> "Whatever." | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return types could be omitted (feel free to ignore this suggestion)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually really like explicit return types because I think it makes the intent and outcome of a function easier to read.
But I know that you are able to omit them in Kotlin if you want to.
But I usually don't omit them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, that's perfectly fine :)