use orx_parallel::*;
const LOG: &[&str] = &[
"INFO service started",
"DEBUG loading config",
"ERROR failed to connect to database",
"INFO retrying connection",
"ERROR timeout after 30s",
"INFO connection restored",
"WARN high memory usage",
"ERROR disk quota exceeded",
];
const ERROR_LINE_NUMBERS: &[usize] = &[2, 4, 7];
fn enumerate_then_filter() {
let error_positions: Vec<usize> = LOG
.par()
.enumerate()
.filter(|(_, line)| line.starts_with("ERROR"))
.map(|(pos, _)| pos)
.collect();
assert_eq!(error_positions, ERROR_LINE_NUMBERS);
}
fn map_then_enumerate() {
let error_positions: Vec<usize> = LOG
.par()
.map(|line| line.split_whitespace().next().unwrap_or(""))
.enumerate()
.filter(|(_, level)| *level == "ERROR")
.map(|(pos, _)| pos)
.collect();
assert_eq!(error_positions, ERROR_LINE_NUMBERS);
}
fn main() {
enumerate_then_filter();
map_then_enumerate();
}