From e2d219b331a878bbb3c9dcef9ea4e218b2e3ee06 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 11 Dec 2022 21:59:21 -0500 Subject: refactor --- src/parse.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) (limited to 'src/parse.rs') diff --git a/src/parse.rs b/src/parse.rs index 3222bf0..7e6cab7 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,14 +1,26 @@ use crate::prelude::*; -pub fn data(year: u16, day: u16) -> Result { +pub fn data(year: u16, day: u8) -> Result { File::open(format!("data/{}/{}.txt", year, day)).map_err(|e| anyhow!(e)) } -pub fn lines(fh: R) -> impl Iterator { +pub fn raw_lines(fh: R) -> impl Iterator +where + R: std::io::Read, +{ let fh = std::io::BufReader::new(fh); fh.lines().map(|res| res.unwrap()) } +pub fn lines(fh: R) -> impl Iterator +where + R: std::io::Read, + T: std::str::FromStr, + ::Err: std::fmt::Debug, +{ + raw_lines(fh).map(|s| s.trim().parse().unwrap()) +} + pub struct Chunk<'a, I: Iterator> { it: &'a mut I, } @@ -35,17 +47,16 @@ where Chunk { it } } -pub fn split( - fh: R, - sep: u8, -) -> impl Iterator { +pub fn split(fh: R, sep: u8) -> impl Iterator +where + R: std::io::Read, + T: std::str::FromStr, + ::Err: std::fmt::Debug, +{ let fh = std::io::BufReader::new(fh); fh.split(sep) .map(|res| String::from_utf8(res.unwrap()).unwrap()) -} - -pub fn ints(iter: impl Iterator) -> impl Iterator { - iter.map(|s| s.trim().parse().unwrap()) + .map(|s| s.trim().parse().unwrap()) } pub fn bytes(fh: R) -> impl Iterator { -- cgit v1.2.3-54-g00ecf