Skip to content

Commit e6286d0

Browse files
committed
Replace for loop with map
It seems cleaner to have two `map`s rather than a `for` loop iterating over a `map`. The two `map`s *could* be combined into one, but I think it's more readable as two steps.
1 parent af2b28d commit e6286d0

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

ci/date-check/src/main.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,28 @@ fn make_date_regex() -> Regex {
4949
}
5050

5151
fn collect_dates_from_file(date_regex: &Regex, text: &str) -> Vec<(usize, Date)> {
52-
let mut dates = vec![];
5352
let mut line = 1;
5453
let mut end_of_last_cap = 0;
55-
for (byte_range, date) in date_regex.captures_iter(&text).map(|cap| {
56-
(
57-
cap.get(0).unwrap().range(),
58-
Date {
59-
year: cap["y"].parse().unwrap(),
60-
month: cap["m"].parse().unwrap(),
61-
},
62-
)
63-
}) {
64-
line += text[end_of_last_cap..byte_range.end]
65-
.chars()
66-
.filter(|c| *c == '\n')
67-
.count();
68-
dates.push((line, date));
69-
end_of_last_cap = byte_range.end;
70-
}
71-
dates
54+
date_regex
55+
.captures_iter(&text)
56+
.map(|cap| {
57+
(
58+
cap.get(0).unwrap().range(),
59+
Date {
60+
year: cap["y"].parse().unwrap(),
61+
month: cap["m"].parse().unwrap(),
62+
},
63+
)
64+
})
65+
.map(|(byte_range, date)| {
66+
line += text[end_of_last_cap..byte_range.end]
67+
.chars()
68+
.filter(|c| *c == '\n')
69+
.count();
70+
end_of_last_cap = byte_range.end;
71+
(line, date)
72+
})
73+
.collect()
7274
}
7375

7476
fn collect_dates(paths: impl Iterator<Item = PathBuf>) -> BTreeMap<PathBuf, Vec<(usize, Date)>> {

0 commit comments

Comments
 (0)