Skip to content

nucleotide-count: Error on invalid nucleotide#238

Merged
ijanos merged 3 commits intoexercism:masterfrom
petertseng:nucleotide-invalid
Jan 5, 2017
Merged

nucleotide-count: Error on invalid nucleotide#238
ijanos merged 3 commits intoexercism:masterfrom
petertseng:nucleotide-invalid

Conversation

@petertseng
Copy link
Copy Markdown
Member

count and nucleotide_counts both return a Result now.

Closes #149


Too many other language implementations to look through them all and see what they did. There is no canonical-data.json for this problem yet and I do not have the time to make one.

Comment thread exercises/nucleotide-count/example.rs Outdated
let mut map: HashMap<char, usize> = VALID_NUCLEOTIDES.chars().map(|c| (c, 0)).collect();
for nucleotide in input.chars() {
*map.entry(nucleotide).or_insert(0) += 1;
match map.entry(nucleotide) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you know, maybe instead of all this I could just check valid(nucleotide) (I would make it a top-level function), so that I wouldn't have to deal with Entry:: cases

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that you are not supposed to deal with entry cases directly. I suggest using if let here:

    if let Some(n) = map.get_mut(nucleotide) {
      *n += 1;
    } else {
       return Err(nucleotide);
    }

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic. Did exactly that (though &nucleotide needed)

#[test]
fn test_count_empty() {
assert_eq!(dna::count('A', ""), 0);
assert_eq!(dna::count('A', "").unwrap(), 0);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do anything specific here to make abundandly clear that it's a result? see https://github.com/exercism/xrust/blob/master/exercises/largest-series-product/tests/largest-series-product.rs#L6

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using is_err is enough.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. For implementations that expect Result or Option, I usually start with some is_err and is_ok tests just to make it clear.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. One for each function.

`count` and `nucleotide_counts` both return a Result now.

Closes #149
Comment thread problems.md
scrabble-score | chaining higher-order functions, HashMap (optional)
pangram | filter, ascii (optional)
nucleotide-count | filter, entry api, mutablity, match
nucleotide-count | Result, filter, entry api, mutablity, match
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duped, but to be solved by #236

if valid(nucleotide) && input.chars().all(valid) {
Ok(input.chars().filter(|&c| c == nucleotide).count())
} else {
Err(nucleotide)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for count('A', "AX") this returns A instead of X, but we don't check it in the tests so I'm not motivated.

@petertseng
Copy link
Copy Markdown
Member Author

Squashing is recommended. I'll do it soon, just giving y'all a last chance to look.

Copy link
Copy Markdown
Contributor

@IanWhitney IanWhitney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than adding the is_err test, which is not a blocker, I think this is fine.

@ijanos ijanos merged commit aea470a into exercism:master Jan 5, 2017
@petertseng petertseng deleted the nucleotide-invalid branch January 5, 2017 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants