Use enumerate combinator

This commit is contained in:
2022-12-06 18:19:42 +01:00
parent e887a8ad0d
commit 391bba24c5
2 changed files with 16 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
//! Common helper utilities to all days
use anyhow::Result;
use nom::combinator::map;
use nom::error::ErrorKind;
use nom::error::ParseError;
use nom::Finish;
@@ -93,6 +94,17 @@ where
}
}
/// Add an index to repeated successful invocations of the embedded parser.
pub fn enumerate<I, O, E>(f: impl Parser<I, O, E>) -> impl FnMut(I) -> IResult<I, (usize, O), E> {
let mut index = 0usize;
map(f, move |v| {
let res = (index, v);
index += 1;
res
})
}
/// Return the minimum and maximum of two unordered variables
pub fn minmax<T>(a: T, b: T) -> (T, T)
where