feat: prepare basic parsers

This commit is contained in:
Matej Janezic 2023-11-30 21:46:12 +01:00
parent 000000003b
commit 00000010c8
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
3 changed files with 25 additions and 0 deletions

View File

@ -2,6 +2,8 @@
# Advent-of-Code 2023 # Advent-of-Code 2023
*This is a dumbed down version of [fspoettel/advent-of-code-rust](https://github.com/fspoettel/advent-of-code-rust) with some extra features* *This is a dumbed down version of [fspoettel/advent-of-code-rust](https://github.com/fspoettel/advent-of-code-rust) with some extra features*
This project targets nightly build of rust, since some preview features are enabled
## Project overview ## Project overview
### Project structure ### Project structure

View File

@ -1 +1,4 @@
#![feature(pattern)]
pub mod template; pub mod template;
pub mod parsers;

20
src/parsers.rs Normal file
View File

@ -0,0 +1,20 @@
use std::str::{pattern::Pattern, FromStr};
pub fn to_vec<'a, T, P>(s: &'a str, pat: P) -> Vec<T>
where
T: FromStr,
P: Pattern<'a>,
{
s.split(pat).filter_map(|x| x.parse().ok()).collect()
}
pub fn to_vec_map<'a, T, U, P>(s: &'a str, pat: P, func: impl FnMut(T) -> U) -> Vec<U>
where
T: FromStr,
P: Pattern<'a>,
{
s.split(pat)
.filter_map(|x| x.parse().ok())
.map(func)
.collect()
}