Created
January 18, 2016 02:37
-
-
Save rwz/84db00dd3989a595d4c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_string.scan(/[a-z\d]+/).each_with_object(Hash.new(0)){ |e, a| a[e] += 1 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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