fix lintting

This commit is contained in:
Jean-Marie Mineau 2023-02-26 12:43:14 +01:00 committed by Jean-Marie Mineau
parent 323e77b4e4
commit c4442e2741
2 changed files with 36 additions and 30 deletions

View file

@ -20,7 +20,7 @@ impl Dict {
let words: std::io::Result<Vec<_>> = file
.lines()
.enumerate()
.filter_map(|(i, e)| (i != 0).then(|| e)) // Skip the first line ( = to size of the dict)
.filter_map(|(i, e)| (i != 0).then_some(e)) // Skip the first line ( = to size of the dict)
.into_iter()
.collect();
let words = words?;
@ -76,8 +76,8 @@ impl From<&Dict> for AnagramDict {
fn from(dict: &Dict) -> Self {
let mut map = Self {
map: HashMap::<FrequencyHash, Vec<String>>::new(),
nb_wild_cards: dict.nb_wild_cards.clone(),
wild_card_char: dict.wild_card_char.clone(),
nb_wild_cards: dict.nb_wild_cards,
wild_card_char: dict.wild_card_char,
letters: HashSet::<char>::new(),
};
@ -87,15 +87,16 @@ impl From<&Dict> for AnagramDict {
let len = dict.words.len();
for word in dict.words.iter() {
let freq = FrequencyHash::compute(word);
for char_ in word.chars() {
word.chars().for_each(|char_| {
if char_ != map.wild_card_char {
map.letters.insert(char_);
}
}
});
map.add_word_with_wild_card(freq, word, dict.nb_wild_cards, dict.wild_card_char);
#[cfg(not(target_arch = "wasm32"))]
{
// Show avancement when running with a stding available
i += 1;
print!("{}%\r", (100 * i) / len);
io::stdout().flush().unwrap();
@ -116,15 +117,15 @@ impl AnagramDict {
if freq.get_freq(self.wild_card_char) > self.nb_wild_cards {
let mut result = vec![];
freq.remove_one_char(self.wild_card_char);
for char_ in self.letters.iter() {
self.letters.iter().for_each(|char_| {
let mut freq = freq.clone();
freq.add_one_char(char_.clone());
freq.add_one_char(*char_);
if let Some(anagrams) = self.find_freq(freq) {
for anagram in anagrams {
result.push(anagram);
}
anagrams
.iter()
.for_each(|anagram| result.push(anagram.clone()));
}
}
});
if result.is_empty() {
None
} else {
@ -145,11 +146,11 @@ impl AnagramDict {
self.map
.entry(freq.clone())
.and_modify(|anagrams| anagrams.push(word.to_string()))
.or_insert(vec![word.to_string()]);
.or_insert_with(|| vec![word.to_string()]);
if nb_wild_card != 0 {
for char_ in freq.0.clone().keys() {
for char_ in freq.0.keys() {
let mut freq = freq.clone();
freq.remove_one_char(char_.clone());
freq.remove_one_char(*char_);
freq.add_one_char(wild_card_symbole);
self.add_word_with_wild_card(freq, word, nb_wild_card - 1, wild_card_symbole);
}
@ -164,18 +165,18 @@ impl AnagramDict {
/// We use the [`char`] type for simplicity. The initial problem was in ascii
/// anyway, but keep in mind that [`char`]s are utf-8 scalar values, and in
/// some alphabet those do not match characters.
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, Eq, Clone)]
struct FrequencyHash(HashMap<char, u8>);
impl FrequencyHash {
/// Compute the [`FrequencyHash`] of a slice.
fn compute(string: &str) -> Self {
let mut map = HashMap::<char, u8>::new();
for char_ in string.chars() {
string.chars().for_each(|char_| {
map.entry(char_)
.and_modify(|counter| *counter += 1)
.or_insert(1);
}
});
Self(map)
}
@ -209,7 +210,7 @@ impl FrequencyHash {
/// Return the number of occurence of a char in the word
fn get_freq(&self, char_: char) -> u8 {
if let Some(freq) = self.0.get(&char_) {
freq.clone()
*freq
} else {
0
}
@ -221,3 +222,9 @@ impl Hash for FrequencyHash {
self.get_unique_vec().hash(state);
}
}
impl PartialEq for FrequencyHash {
fn eq(&self, other: &Self) -> bool {
self.get_unique_vec() == other.get_unique_vec()
}
}

View file

@ -71,19 +71,18 @@ fn update_letters() {
while let Some(child) = result.first_element_child() {
child.remove();
}
DICTIONNARY
if let Some(anagrms) = DICTIONNARY
.lock()
.expect("Failed to access DICTIONNARY")
.as_ref()
.map(|dict| dict.find(&val))
.flatten()
.map(|anagrms| {
for anagram in anagrms {
let val = document().create_element("li").unwrap();
val.set_text_content(Some(&anagram));
result.append_child(&val).unwrap();
}
});
.and_then(|dict| dict.find(&val))
{
for anagram in anagrms {
let val = document().create_element("li").unwrap();
val.set_text_content(Some(&anagram));
result.append_child(&val).unwrap();
}
};
}
fn load_dict(text: JsValue) {
@ -113,8 +112,8 @@ fn load_dict(text: JsValue) {
// Called when the wasm module is instantiated
#[wasm_bindgen(start)]
fn main() -> Result<(), JsValue> {
let load_dict_c1 = Closure::<dyn FnMut(_)>::new(move |text: JsValue| load_dict(text));
let load_dict_c2 = Closure::<dyn FnMut(_)>::new(move |text: JsValue| load_dict(text));
let load_dict_c1 = Closure::<dyn FnMut(_)>::new(load_dict);
let load_dict_c2 = Closure::<dyn FnMut(_)>::new(load_dict);
let update_dict = Closure::<dyn FnMut(_)>::new(move |event: Event| {
display_spinner();