From b29ea17ea94d1862114af2cf5ced0e09c197dc35 Mon Sep 17 00:00:00 2001 From: apogeeoak <59737221+apogeeoak@users.noreply.github.com> Date: Wed, 10 Feb 2021 18:03:29 -0500 Subject: [PATCH 1/2] feat: Added iterators5.rs exercise. --- .../standard_library_types/iterators5.rs | 113 ++++++++++++++++++ info.toml | 15 +++ 2 files changed, 128 insertions(+) create mode 100644 exercises/standard_library_types/iterators5.rs diff --git a/exercises/standard_library_types/iterators5.rs b/exercises/standard_library_types/iterators5.rs new file mode 100644 index 0000000..65c50e6 --- /dev/null +++ b/exercises/standard_library_types/iterators5.rs @@ -0,0 +1,113 @@ +// iterators5.rs + +// Rustling progress is modelled using a hash map. The name of the exercise is +// the key and the progress is the value. Two counting functions were created +// to count the number of exercises with a given progress. These counting +// functions use imperative style for loops. Recreate this counting +// functionality using iterators. +// Execute `rustlings hint iterators5` for hints. +// +// Make the code compile and the tests pass. + +// I AM NOT DONE + +use std::collections::HashMap; + +#[derive(PartialEq, Eq)] +enum Progress { + None, + Some, + Complete, +} + +fn count_for(map: &HashMap, value: Progress) -> usize { + let mut count = 0; + for val in map.values() { + if val == &value { + count += 1; + } + } + count +} + +fn count(map: &HashMap, value: Progress) -> usize { +} + +fn count_stack_for(stack: &[HashMap], value: Progress) -> usize { + let mut count = 0; + for map in stack { + for val in map.values() { + if val == &value { + count += 1; + } + } + } + count +} + +fn count_stack(stack: &[HashMap], value: Progress) -> usize { +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn count_complete() { + let map = get_map(); + assert_eq!(3, count(&map, Progress::Complete)); + } + + #[test] + fn count_equals_for() { + let map = get_map(); + assert_eq!( + count_for(&map, Progress::Complete), + count(&map, Progress::Complete) + ); + } + + #[test] + fn count_stack_complete() { + let stack = get_map_stack(); + assert_eq!(6, count_stack(&stack, Progress::Complete)); + } + + #[test] + fn count_stack_equals_for() { + let stack = get_map_stack(); + assert_eq!( + count_stack_for(&stack, Progress::Complete), + count_stack(&stack, Progress::Complete) + ); + } + + fn get_map() -> HashMap { + use Progress::*; + + let mut map = HashMap::new(); + map.insert(String::from("variables1"), Complete); + map.insert(String::from("functions1"), Complete); + map.insert(String::from("hashmap1"), Complete); + map.insert(String::from("arc1"), Some); + map.insert(String::from("as_ref_mut"), None); + map.insert(String::from("from_str"), None); + + map + } + + fn get_map_stack() -> Vec> { + use Progress::*; + + let map = get_map(); + + let mut other = HashMap::new(); + other.insert(String::from("variables2"), Complete); + other.insert(String::from("functions2"), Complete); + other.insert(String::from("if1"), Complete); + other.insert(String::from("from_into"), None); + other.insert(String::from("try_from_into"), None); + + vec![map, other] + } +} diff --git a/info.toml b/info.toml index f0fed93..646bb5a 100644 --- a/info.toml +++ b/info.toml @@ -741,6 +741,21 @@ a mutable variable. Or, you might write code utilizing recursion and a match clause. In Rust you can take another functional approach, computing the factorial elegantly with ranges and iterators.""" +[[exercises]] +name = "iterators5" +path = "exercises/standard_library_types/iterators5.rs" +mode = "test" +hint = """ +The documentation for the std::iter::Iterator trait contains numerous methods +that would be helpful here. + +Return 0 from count_stack to make the code compile in order to test count. + +The stack variable in count_stack is a slice of HashMaps. It needs to be +converted into an iterator in order to use the iterator methods. + +The fold method can be useful in the count_stack function.""" + # THREADS [[exercises]] From 9c88ea91266c84901fd46496902610241dab3baf Mon Sep 17 00:00:00 2001 From: apogeeoak <59737221+apogeeoak@users.noreply.github.com> Date: Tue, 20 Apr 2021 18:52:10 -0400 Subject: [PATCH 2/2] Improved iterators5.rs explanation. --- .../standard_library_types/iterators5.rs | 51 +++++++++++-------- info.toml | 11 ++-- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/exercises/standard_library_types/iterators5.rs b/exercises/standard_library_types/iterators5.rs index 65c50e6..2d97bd4 100644 --- a/exercises/standard_library_types/iterators5.rs +++ b/exercises/standard_library_types/iterators5.rs @@ -1,11 +1,14 @@ // iterators5.rs -// Rustling progress is modelled using a hash map. The name of the exercise is -// the key and the progress is the value. Two counting functions were created -// to count the number of exercises with a given progress. These counting -// functions use imperative style for loops. Recreate this counting -// functionality using iterators. -// Execute `rustlings hint iterators5` for hints. +// Let's define a simple model to track Rustlings exercise progress. Progress +// will be modelled using a hash map. The name of the exercise is the key and +// the progress is the value. Two counting functions were created to count the +// number of exercises with a given progress. These counting functions use +// imperative style for loops. Recreate this counting functionality using +// iterators. Only the two iterator methods (count_iterator and +// count_collection_iterator) need to be modified. +// Execute `rustlings hint +// iterators5` for hints. // // Make the code compile and the tests pass. @@ -30,12 +33,14 @@ fn count_for(map: &HashMap, value: Progress) -> usize { count } -fn count(map: &HashMap, value: Progress) -> usize { +fn count_iterator(map: &HashMap, value: Progress) -> usize { + // map is a hashmap with String keys and Progress values. + // map = { "variables1": Complete, "from_str": None, ... } } -fn count_stack_for(stack: &[HashMap], value: Progress) -> usize { +fn count_collection_for(collection: &[HashMap], value: Progress) -> usize { let mut count = 0; - for map in stack { + for map in collection { for val in map.values() { if val == &value { count += 1; @@ -45,7 +50,10 @@ fn count_stack_for(stack: &[HashMap], value: Progress) -> usiz count } -fn count_stack(stack: &[HashMap], value: Progress) -> usize { +fn count_collection_iterator(collection: &[HashMap], value: Progress) -> usize { + // collection is a slice of hashmaps. + // collection = [{ "variables1": Complete, "from_str": None, ... }, + // { "variables2": Complete, ... }, ... ] } #[cfg(test)] @@ -55,7 +63,7 @@ mod tests { #[test] fn count_complete() { let map = get_map(); - assert_eq!(3, count(&map, Progress::Complete)); + assert_eq!(3, count_iterator(&map, Progress::Complete)); } #[test] @@ -63,22 +71,25 @@ mod tests { let map = get_map(); assert_eq!( count_for(&map, Progress::Complete), - count(&map, Progress::Complete) + count_iterator(&map, Progress::Complete) ); } #[test] - fn count_stack_complete() { - let stack = get_map_stack(); - assert_eq!(6, count_stack(&stack, Progress::Complete)); + fn count_collection_complete() { + let collection = get_vec_map(); + assert_eq!( + 6, + count_collection_iterator(&collection, Progress::Complete) + ); } #[test] - fn count_stack_equals_for() { - let stack = get_map_stack(); + fn count_collection_equals_for() { + let collection = get_vec_map(); assert_eq!( - count_stack_for(&stack, Progress::Complete), - count_stack(&stack, Progress::Complete) + count_collection_for(&collection, Progress::Complete), + count_collection_iterator(&collection, Progress::Complete) ); } @@ -96,7 +107,7 @@ mod tests { map } - fn get_map_stack() -> Vec> { + fn get_vec_map() -> Vec> { use Progress::*; let map = get_map(); diff --git a/info.toml b/info.toml index 646bb5a..65f671a 100644 --- a/info.toml +++ b/info.toml @@ -694,7 +694,7 @@ Step 2 & step 2.1: Very similar to the lines above and below. You've got this! Step 3: An iterator goes through all elements in a collection, but what if we've run out of -elements? What should we expect here? If you're stuck, take a look at +elements? What should we expect here? If you're stuck, take a look at https://doc.rust-lang.org/std/iter/trait.Iterator.html for some ideas. """ @@ -749,12 +749,13 @@ hint = """ The documentation for the std::iter::Iterator trait contains numerous methods that would be helpful here. -Return 0 from count_stack to make the code compile in order to test count. +Return 0 from count_collection_iterator to make the code compile in order to +test count_iterator. -The stack variable in count_stack is a slice of HashMaps. It needs to be -converted into an iterator in order to use the iterator methods. +The collection variable in count_collection_iterator is a slice of HashMaps. It +needs to be converted into an iterator in order to use the iterator methods. -The fold method can be useful in the count_stack function.""" +The fold method can be useful in the count_collection_iterator function.""" # THREADS