Dictionary is an Iterator
thought it's probably bad to iterate over it
This commit is contained in:
parent
15ff07c4b3
commit
6ea12cfc46
1 changed files with 25 additions and 3 deletions
28
src/main.rs
28
src/main.rs
|
@ -1,4 +1,5 @@
|
|||
use std::env;
|
||||
use std::iter::Iterator;
|
||||
use std::io::Error;
|
||||
use std::io::BufReader;
|
||||
use std::io::BufRead;
|
||||
|
@ -7,8 +8,29 @@ use std::fs::File;
|
|||
use unidecode::unidecode;
|
||||
use regex::Regex;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Dictionary {
|
||||
words: Vec<String>,
|
||||
iter_position: usize,
|
||||
}
|
||||
|
||||
/*impl Dictionary {
|
||||
fn default() -> Dictionary {
|
||||
Dictionary{words: vec![], iter_position: 0}
|
||||
}
|
||||
}*/
|
||||
|
||||
impl Iterator for Dictionary {
|
||||
type Item = String;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.words.len() >= self.iter_position {
|
||||
None
|
||||
} else {
|
||||
let v = self.words[self.iter_position].clone();
|
||||
self.iter_position += 1;
|
||||
Some(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn dictionary_from_iterable(lines: impl Iterator<Item = Result<String, Error>>) -> Dictionary {
|
||||
|
@ -18,7 +40,7 @@ fn dictionary_from_iterable(lines: impl Iterator<Item = Result<String, Error>>)
|
|||
// TODO: normalizza: lascia solo a-z, converti gli accenti, ecc.
|
||||
w.push(line_to_word(line))
|
||||
}
|
||||
return Dictionary{words: w};
|
||||
return Dictionary{words: w, ..Dictionary::default()};
|
||||
}
|
||||
|
||||
fn line_to_word(l: String) -> String {
|
||||
|
@ -37,8 +59,8 @@ fn main() {
|
|||
let d = dictionary_from_iterable(buf.lines());
|
||||
|
||||
let re = Regex::new(regexp).unwrap();
|
||||
for w in &d.words {
|
||||
if re.is_match(w) {
|
||||
for w in d {
|
||||
if re.is_match(w.as_str()) {
|
||||
println!("{}", w)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue