From 18cab4c9209996aeaa29a88f8ac98e91dcfdede9 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 1 Dec 2022 04:21:57 -0500 Subject: simplify --- src/parse.rs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src/parse.rs') diff --git a/src/parse.rs b/src/parse.rs index 3674a25..3222bf0 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -4,12 +4,41 @@ pub fn data(year: u16, day: u16) -> Result { File::open(format!("data/{}/{}.txt", year, day)).map_err(|e| anyhow!(e)) } -pub fn lines(fh: File) -> impl Iterator { +pub fn lines(fh: R) -> impl Iterator { let fh = std::io::BufReader::new(fh); fh.lines().map(|res| res.unwrap()) } -pub fn split(fh: File, sep: u8) -> impl Iterator { +pub struct Chunk<'a, I: Iterator> { + it: &'a mut I, +} + +impl<'a, I: Iterator> Iterator for Chunk<'a, I> { + type Item = String; + + fn next(&mut self) -> Option { + if let Some(line) = self.it.next() { + if line.is_empty() { + return None; + } else { + return Some(line); + } + } + None + } +} + +pub fn chunk(it: &mut I) -> Chunk<'_, I> +where + I: Iterator, +{ + Chunk { it } +} + +pub fn split( + fh: R, + sep: u8, +) -> impl Iterator { let fh = std::io::BufReader::new(fh); fh.split(sep) .map(|res| String::from_utf8(res.unwrap()).unwrap()) @@ -19,11 +48,11 @@ pub fn ints(iter: impl Iterator) -> impl Iterator { iter.map(|s| s.trim().parse().unwrap()) } -pub fn bytes(fh: File) -> impl Iterator { +pub fn bytes(fh: R) -> impl Iterator { fh.bytes().map(|res| res.unwrap()) } -pub fn string(fh: File) -> String { +pub fn string(fh: R) -> String { let bytes: Vec<_> = bytes(fh).collect(); String::from_utf8(bytes).unwrap() } -- cgit v1.2.3-54-g00ecf