Refactor factorial function using custom iterator

This commit is contained in:
Rock070 2024-01-11 23:22:52 +08:00
parent 77c6ac4868
commit eb99e60ed8

View File

@ -11,8 +11,6 @@
// Execute `rustlings hint iterators5` or use the `hint` watch subcommand for a
// hint.
// I AM NOT DONE
use std::collections::HashMap;
#[derive(Clone, Copy, PartialEq, Eq)]
@ -35,11 +33,11 @@ fn count_for(map: &HashMap<String, Progress>, value: Progress) -> usize {
fn count_iterator(map: &HashMap<String, Progress>, value: Progress) -> usize {
// map is a hashmap with String keys and Progress values.
// map = { "variables1": Complete, "from_str": None, ... }
todo!();
map.values().filter(|&val| val == &value).count()
}
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
let mut count = 0;
let mut count: usize = 0;
for map in collection {
for val in map.values() {
if val == &value {
@ -54,7 +52,12 @@ fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Pr
// collection is a slice of hashmaps.
// collection = [{ "variables1": Complete, "from_str": None, ... },
// { "variables2": Complete, ... }, ... ]
todo!();
let mut count = 0;
collection.iter().for_each(|f| {
count += count_iterator(f, value)
});
count
}
#[cfg(test)]