Skip to content

Instantly share code, notes, and snippets.

@rwz
Created January 18, 2016 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwz/84db00dd3989a595d4c8 to your computer and use it in GitHub Desktop.
Save rwz/84db00dd3989a595d4c8 to your computer and use it in GitHub Desktop.
my_string.scan(/[a-z\d]+/).each_with_object(Hash.new(0)){ |e, a| a[e] += 1 }
use std::collections::HashMap;
fn split_text(c: char) -> bool {
!c.is_alphanumeric()
}
fn fold_words(mut acc: HashMap<String, u32>, word: &str) -> HashMap<String, u32> {
if word.len() > 0 {
let count = acc.entry(word.to_lowercase()).or_insert(0);
*count += 1;
}
acc
}
pub fn word_count(text: &str) -> HashMap<String, u32> {
text.split(split_text).fold(HashMap::new(), fold_words)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment