-
Notifications
You must be signed in to change notification settings - Fork 25
Implement WordCounter and WordAggregator #3
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
Implement WordCounter and WordAggregator #3
Conversation
JavierCane
left a comment
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.
Some suggestions added 🙂
@joan-miralles tell me if you want to apply these suggestions in this very same PR. If so, I would wait for them before merging it.
|
|
||
| counter.countWords(nonEmptyText) shouldBe wordsNumber | ||
| } | ||
| "return the number of words given a non empty text with spaces" in { |
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.
Good one!
Suggestion: Add other corner cases such as empty strings (0), punctuation, and so on 😬
| def countWords(text: String): Int = { | ||
| 0 | ||
| if (text.isEmpty) | ||
| return 0 |
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.
return statement not needed. In fact, it's not Scala idiomatic to specify such clauses since we will always return the last statement in a code block. In order to remove it, we should add the else part of the if 🙂
| def aggregateWords(text: String): Map[String, Int] = { | ||
| Map.empty | ||
| if (text.isEmpty) | ||
| return Map.empty |
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.
Same as with the WordCounter case. You don't need to explicitly declare the return statements 🙂
| Map.empty | ||
| if (text.isEmpty) | ||
| return Map.empty | ||
| var occurrences = Map[String, Int]() |
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.
Try to think about an alternative implementation without requiring mutability (var) 🙂
Hint: Instead of iterating over the words: Array[String] with a foreach, there're other methods which allow to accumulate a result between each array item iteration. Take a look at the scala.Array documentation 😬
…ples into collections_counter
|
Fixed! I've moved solution to exercise_solutions folder. (better if you want merge PR to public repository) |
|
Thanks for moving your solution proposal to its own package. It definitely help us out! Merging! |
Exercise: https://pro.codely.tv/library/introduccion-a-scala/63278/path/step/33533069/