diff --git a/src/es08_folds.rs b/src/es08_folds.rs new file mode 100644 index 0000000..ab415fe --- /dev/null +++ b/src/es08_folds.rs @@ -0,0 +1,69 @@ +#![allow(unused)] +/** + * Scrivere due funzioni che contano il numero delle vocali presenti in una stringa + * (sia che siano minuscole che maiuscole). + * - Entrambe le funzione prendono in input una stringa ma una ritorna una tupla struttura e l'altra una struttura + * fn num_vocali_tuple(s:&String) -> TuplaVocali + * fn num_vocali_struct(s:&String) -> NumVocali + * - Le definizioni delle strutture e il test che le funzioni devono passare sono le seguenti + * struct NumVocali{ a: i32, e: i32, i: i32, o: i32, u: i32, } + * struct TuplaVocali(i32,i32,i32,i32,i32); + * fn testFolds(){ + * let a=String::from("Ciao Paola come stai? Ok. Tu John come stai? Ok"); + * assert_eq!(TuplaVocali(5, 2, 3, 7, 1),num_vocali_tuple(&a)); + * assert_eq!(NumVocali{a:5,e:2,i:3,o:7,u:1},num_vocali_struct(&a)); + * } + * + * - Nell'implementare entrambe le funzioni DOVETE USARE il metodo fold di Iterator documentato qua: + * https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.fold + * - La prima funzione avra’ con accumulatore una TuplaVocali e la seconda una struttura NumVocali. + */ + +#[derive(Debug, PartialEq, Default)] +pub struct NumVocali { + a: i32, + e: i32, + i: i32, + o: i32, + u: i32, +} +impl NumVocali { + pub fn new(a:i32, e:i32, i:i32, o:i32, u:i32) -> Self { + Self {a, e, i, o, u} + } +} + +#[derive(Debug, PartialEq, Default)] +pub struct TuplaVocali(i32, i32, i32, i32, i32); +impl TuplaVocali { + pub fn new(a:i32, e:i32, i:i32, o:i32, u:i32) -> Self { + Self (a, e, i, o, u) + } +} + +pub fn num_vocali_tuple(s: &String) -> TuplaVocali { + s.chars().fold(TuplaVocali::default(), |mut vocali, c| { + match c.to_ascii_lowercase() { + 'a' => vocali.0 += 1, + 'e' => vocali.1 += 1, + 'i' => vocali.2 += 1, + 'o' => vocali.3 += 1, + 'u' => vocali.4 += 1, + _ => (), + }; + vocali + }) +} +pub fn num_vocali_struct(s: &String) -> NumVocali { + s.chars().fold(NumVocali::default(), |mut vocali, c| { + match c.to_ascii_lowercase() { + 'a' => vocali.a += 1, + 'e' => vocali.e += 1, + 'i' => vocali.i += 1, + 'o' => vocali.o += 1, + 'u' => vocali.u += 1, + _ => (), + }; + vocali + }) +} diff --git a/src/lib.rs b/src/lib.rs index a49103f..3288b70 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,5 @@ pub mod es04_rational_traits; pub mod es05_bank; pub mod es06_list; pub mod es07_list_generic; +pub mod es08_folds; diff --git a/tests/es08_folds.rs b/tests/es08_folds.rs new file mode 100644 index 0000000..c96de64 --- /dev/null +++ b/tests/es08_folds.rs @@ -0,0 +1,8 @@ +use esercizi::es08_folds::{num_vocali_struct, num_vocali_tuple, NumVocali, TuplaVocali}; + +#[test] +fn test_folds() { + let a = String::from(" Ciao Paola come stai ? Ok . Tu John come stai ? Ok "); + assert_eq!(TuplaVocali::new(5, 2, 3, 7, 1), num_vocali_tuple(&a)); + assert_eq!(NumVocali::new(5, 2, 3, 7, 1), num_vocali_struct(&a)); +}