Skip to content

Commit fb57fc6

Browse files
committed
Add {min,max}_set(_by{_key)?)? functions (6) inline bulk into match arm
Slightly simplifies control flow as it avoids mutable variables that may not even be used in case of early return.
1 parent 6f96e01 commit fb57fc6

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

src/extrema_set.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,30 @@ pub fn min_set_impl<I, K, F, Compare>(mut it: I,
88
F: FnMut(&I::Item) -> K,
99
Compare: FnMut(&I::Item, &I::Item, &K, &K) -> Ordering,
1010
{
11-
let (mut result, mut current_key) = match it.next() {
12-
None => return Vec::new(),
11+
match it.next() {
12+
None => Vec::new(),
1313
Some(element) => {
14-
let key = key_for(&element);
15-
(vec![element], key)
14+
let mut current_key = key_for(&element);
15+
let mut result = vec![element];
16+
it.for_each(|element| {
17+
let key = key_for(&element);
18+
match compare(&element, &result[0], &key, &current_key) {
19+
Ordering::Less => {
20+
result.clear();
21+
result.push(element);
22+
current_key = key;
23+
},
24+
Ordering::Equal => {
25+
result.push(element);
26+
},
27+
Ordering::Greater => {
28+
},
29+
}
30+
});
31+
result
1632
}
17-
};
33+
}
1834

19-
it.for_each(|element| {
20-
let key = key_for(&element);
21-
match compare(&element, &result[0], &key, &current_key) {
22-
Ordering::Less => {
23-
result.clear();
24-
result.push(element);
25-
current_key = key;
26-
},
27-
Ordering::Equal => {
28-
result.push(element);
29-
},
30-
Ordering::Greater => {
31-
},
32-
}
33-
});
34-
35-
result
3635
}
3736

3837
/// Implementation guts for `ax_set`, `max_set_by`, and `max_set_by_key`.

0 commit comments

Comments
 (0)