From 7a8090bbd161a0ef531b65b8dbecec4070ecd74d Mon Sep 17 00:00:00 2001 From: Gustav Eek Date: Sun, 5 Mar 2023 19:08:13 +0100 Subject: [PATCH] Src. Refactor. Add debug printing of conf Also make vertical whitespace around functions consistent. Also remove duplicate comments. --- src/main.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5c0fe6b..2b51fed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use regex::Regex; const DEBUG: bool = false; fn winnow(mut list: Vec) -> (Conf, Vec ) { + list.retain(|x| x != ""); // only keep nonempty // Search patterns @@ -29,6 +30,10 @@ fn winnow(mut list: Vec) -> (Conf, Vec ) { }, }; + if DEBUG { + eprintln!("Conf: {:?}", conf); + } + // Remove patterns and trim for l in &mut *list { *l = bullet.replace_all(&l, "").to_string(); @@ -40,6 +45,7 @@ fn winnow(mut list: Vec) -> (Conf, Vec ) { } fn input() -> (Conf, Vec) { + let stdin = io::stdin(); let list: Vec = stdin.lock().lines() @@ -59,6 +65,7 @@ fn output(conf: &Conf, prio: &Vec, ranked: &Vec) { } fn normalize(mut arg: Vec) -> Vec { + let mut sum = 0.0; for v in &arg { sum += v; @@ -69,8 +76,8 @@ fn normalize(mut arg: Vec) -> Vec { arg } - fn delta(n: i32) -> Vec { + const NAME: &str = "Delta"; const MEAN: f64 = 1.0; // unessential thanks to normalization let mut prio: Vec = Vec::new(); @@ -87,6 +94,7 @@ fn delta(n: i32) -> Vec { } fn exp(n: i32) -> Vec { + const NAME: &str = "Exp"; const RATE: f64 = 1.0; // rate is unessential thanks to normalization let dist = Exp::new(RATE).unwrap(); @@ -105,8 +113,8 @@ fn exp(n: i32) -> Vec { normalize(prio) } - fn lognormal(n: i32, std: f64) -> Vec { + const NAME: &str = "Lognormal"; const MEAN: f64 = 0.0; // mean is unessential thanks to // normalization, and std of N @@ -126,6 +134,8 @@ fn lognormal(n: i32, std: f64) -> Vec { normalize(prio) } +#[derive(Debug)] + struct Conf { bullet: String, } @@ -147,6 +157,7 @@ struct Args { } fn main() { + let args = Args::parse(); // let mut skew = 1.0; @@ -186,7 +197,6 @@ fn main() { } output(&conf, &prio, &ranked); - } #[test] @@ -218,7 +228,6 @@ fn whitespace() { .split("\n").map(|x| x.to_owned()).collect(); let (_, res) = winnow(arg); assert_eq!(exp, res); - } #[test] @@ -226,13 +235,13 @@ fn whitespace() { fn bullets() { let arg: Vec = - "\n * Hej du\t\n - glade\n-\tta en\n\n" // Convert &str to String - .split("\n").map(|x| x.to_owned()) // with `.to_owned()` or - .collect(); // `.to_string()` + "\n * Hej du\t\n - glade\n-\tta en\n\n" + .split("\n").map(|x| x.to_owned()) + .collect(); let exp: Vec = "Hej du\nglade\nta en" .split("\n").map(|x| x.to_owned()).collect(); - let (conf, res ) = winnow(arg); + let (conf, res) = winnow(arg); assert_eq!(exp, res); assert_eq!(conf.bullet, " * ".to_owned()); @@ -241,6 +250,7 @@ fn bullets() { #[test] fn prioinput() { + let arg: Vec = " - 70 % Hej du\n - 30 %\tglade\nta en\n" .split("\n").map(|x| x.to_owned()) @@ -256,6 +266,7 @@ fn prioinput() { fn bullout() { let conf = Conf {bullet: " * ".to_owned()}; + let prio = vec![0.6, 0.3, 0.1]; let ranked: Vec = "Hej du\nglade\nta en" -- 2.39.2