@@ -64,6 +64,17 @@ fn count_collection_iterator_flat(
64
64
. count ( )
65
65
}
66
66
67
+ // Equivalent to `count_collection_iterator`, `count_collection_iterator_flat` and `count_iterator`
68
+ // iterating over the collection with an accumulator in order to avoid the sum step after.
69
+ fn count_collection_iterator_fold (
70
+ collection : & [ HashMap < String , Progress > ] ,
71
+ value : Progress ,
72
+ ) -> usize {
73
+ collection
74
+ . iter ( )
75
+ . fold ( 0 , |acc, map| acc + count_iterator ( map, value) )
76
+ }
77
+
67
78
fn main ( ) {
68
79
// You can optionally experiment here.
69
80
}
@@ -133,20 +144,23 @@ mod tests {
133
144
let collection = get_vec_map ( ) ;
134
145
assert_eq ! ( count_collection_iterator( & collection, Complete ) , 6 ) ;
135
146
assert_eq ! ( count_collection_iterator_flat( & collection, Complete ) , 6 ) ;
147
+ assert_eq ! ( count_collection_iterator_fold( & collection, Complete ) , 6 ) ;
136
148
}
137
149
138
150
#[ test]
139
151
fn count_collection_some ( ) {
140
152
let collection = get_vec_map ( ) ;
141
153
assert_eq ! ( count_collection_iterator( & collection, Some ) , 1 ) ;
142
154
assert_eq ! ( count_collection_iterator_flat( & collection, Some ) , 1 ) ;
155
+ assert_eq ! ( count_collection_iterator_fold( & collection, Some ) , 1 ) ;
143
156
}
144
157
145
158
#[ test]
146
159
fn count_collection_none ( ) {
147
160
let collection = get_vec_map ( ) ;
148
161
assert_eq ! ( count_collection_iterator( & collection, None ) , 4 ) ;
149
162
assert_eq ! ( count_collection_iterator_flat( & collection, None ) , 4 ) ;
163
+ assert_eq ! ( count_collection_iterator_fold( & collection, None ) , 4 ) ;
150
164
}
151
165
152
166
#[ test]
0 commit comments