Skip to content

Instantly share code, notes, and snippets.

@leshow
Created August 7, 2018 13:36
Show Gist options
  • Save leshow/7310462bcbf92e71ec0d08082fd2f53b to your computer and use it in GitHub Desktop.
Save leshow/7310462bcbf92e71ec0d08082fd2f53b to your computer and use it in GitHub Desktop.
Remove the need for locking memory w/ rayon
extern crate rayon;
use rayon::prelude::*;
use std::{collections::HashSet, env, error::Error, fs};
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
let final_diff: HashSet<String> = args[1..]
.par_iter()
.map(|file| match get_set(&file[..]) {
Ok(s) => Some(s),
Err(e) => {
eprintln!("error encountered collecting files: {} \n {}", file, e);
return None;
}
}).filter_map(|x| x)
.reduce(|| HashSet::new(), |acc, set| &acc ^ &set);
println!("{:?}", final_diff);
Ok(())
}
fn get_set<S>(file: S) -> Result<HashSet<String>, Box<dyn Error>>
where
S: AsRef<str>,
{
let mut set = HashSet::new();
let contents = fs::read_dir(file.as_ref())?;
for entry in contents {
let dir = entry?;
if let Some(name) = dir.path().file_name() {
set.insert(name.to_str().unwrap().to_string());
}
}
Ok(set)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment