Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions solutions/18_iterators/iterators5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ fn count_collection_iterator_flat(
.count()
}

// Equivalent to `count_collection_iterator`, `count_collection_iterator_flat` and `count_iterator`
// iterating over the collection with an accumulator in order to avoid the sum step after.
fn count_collection_iterator_fold(
collection: &[HashMap<String, Progress>],
value: Progress,
) -> usize {
collection
.iter()
.fold(0, |acc, map| acc + count_iterator(map, value))
}

fn main() {
// You can optionally experiment here.
}
Expand Down Expand Up @@ -133,20 +144,23 @@ mod tests {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Complete), 6);
assert_eq!(count_collection_iterator_flat(&collection, Complete), 6);
assert_eq!(count_collection_iterator_fold(&collection, Complete), 6);
}

#[test]
fn count_collection_some() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, Some), 1);
assert_eq!(count_collection_iterator_flat(&collection, Some), 1);
assert_eq!(count_collection_iterator_fold(&collection, Some), 1);
}

#[test]
fn count_collection_none() {
let collection = get_vec_map();
assert_eq!(count_collection_iterator(&collection, None), 4);
assert_eq!(count_collection_iterator_flat(&collection, None), 4);
assert_eq!(count_collection_iterator_fold(&collection, None), 4);
}

#[test]
Expand Down
Loading