Last active
May 26, 2016 23:53
-
-
Save jmoyers/e5524bf396975c65896a4998084e0554 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
// From https://thesquareplanet.com/blog/the-path-to-rust/ | |
// Example 1: functional | |
let idx = args | |
// iterate over our arguments | |
.iter() | |
// open each file | |
.map(|fname| (fname.as_str(), fs::File::open(fname.as_str()))) | |
// check for errors | |
.map(|(fname, f)| { f.and_then(|f| Ok((fname, f))) | |
.expect(&format!("input file {} could not be opened", fname)) | |
}) | |
// make a buffered reader | |
.map(|(fname, f)| (fname, io::BufReader::new(f))) | |
// for each file | |
.flat_map(|(f, file)| { | |
file | |
// read the lines | |
.lines() | |
// split into words | |
.flat_map(|line| { | |
line.unwrap().split_whitespace() | |
.map(|w| w.to_string()).collect::<Vec<_>>().into_iter() | |
// NOTE: the collect+into_iter here is icky | |
// have a look at the flat_map entry | |
// in the Appendix for why it's here | |
}) | |
// prune duplicates | |
.collect::<HashSet<_>>() | |
.into_iter() | |
// and emit inverted index entry | |
.map(move |word| (word, f)) | |
}) | |
.fold(HashMap::new(), |mut idx, (word, f)| { | |
// absorb all entries into a vector of file names per word | |
idx.entry(word) | |
.or_insert(Vec::new()) | |
.push(f); | |
idx | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment