-
-
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
Conversation
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 am not quite sure if the entry in the config.json file needs to be done manually (as I did) or if this entry is somehow generated automatically. (I used a randomly generated uuid for the entry.)
Both are fine. You could generate it via:
bin/fetch-configlet
bin/configlet create --approach when-expression --exercise bob
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 snippet can be at most 8 lines. You should try to pick the lines that best represent the approach (so that would be the when
expression, with the function definition I think).
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 length of the snippet including the function definition works out to exactly eight lines. 👍🏼
private fun String.isYelling(): Boolean { | ||
val letters = this.filter { it.isLetter() } | ||
return if (letters.isEmpty()) | ||
false | ||
else | ||
letters.all { it.isUpperCase() } | ||
} | ||
} |
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.
What about?
private fun String.isYelling(): Boolean { | |
val letters = this.filter { it.isLetter() } | |
return if (letters.isEmpty()) | |
false | |
else | |
letters.all { it.isUpperCase() } | |
} | |
} | |
private fun String.isYelling() = any(Char::isLetter) && toUpperCase() == this |
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.
Hey,
looks good. Tried a solution using higher order functions but did not find an approach I was happy with.
I will update the solution using this one-liner, thanks.
private fun String.isSilence(): Boolean = this.isBlank() | ||
private fun String.isQuestion(): Boolean = this.trim().endsWith('?') |
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)
private fun String.isSilence(): Boolean = this.isBlank() | |
private fun String.isQuestion(): Boolean = this.trim().endsWith('?') | |
private fun String.isSilence() = this.isBlank() | |
private fun String.isQuestion() = this.trim().endsWith('?') |
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 :)
I updated the code using your suggestions. Sorry for the rather long delay. |
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.
Thanks! Looks good.
Hey there,
as proposed in the forum here I added a new approach to the Bob exercise on the Kotlin track using a
when
-expression.I hope I provided all the files and changes needed for a correctly working version.
I am not quite sure if the entry in the config.json file needs to be done manually (as I did) or if this entry is somehow generated automatically. (I used a randomly generated uuid for the entry.)
Please have a look at the proposed changes and give feedback on the writing and configuration.
Have a nice day! 👋🏼