New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ep 9: Some ideas #45
Merged
Merged
Ep 9: Some ideas #45
Conversation
This file contains 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
This is a CLI app. It deserves proper argument parsing and stuff. This has the side effect of collecting the files into a Vec but if you pass so many files that this matters you are doing some weird stuff
Super tiny thing but why not support people's non UTF8 file names?
This is very unhelpful: error[E0387]: cannot borrow data mutably in a captured outer variable in an `Fn` closure --> src/main.rs:30:57 | 30 | .for_each(|filename| tally_words(filename, &mut words).unwrap()); | ^^^^^ | help: consider changing this closure to take self by mutable reference --> src/main.rs:30:19 | 30 | .for_each(|filename| tally_words(filename, &mut words).unwrap()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This creates a new HashMap in each `tally_words` call. We can thus omit using a Mutex. Instead of doing `for_each` with side effects, we use `reduce` (Rayon's parallel version of it) to build the final map of words. The reason I used the im crate is because it has a very nice `union_with` method on its HashMap; you can also replace `im::HashMap` with `im::OrdMap` if you want to get sorted output directly. I have no idea if this is faster, but it does show a different way of using concurrency.
74b45b9
to
152163e
Compare
As always, top notch work by @killercup. |
Thanks! Added. |
|
Very interesting approach, well done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Hey, after watching episode 9 I wanted to play around with another way of doing this: Instead of using a Mutex, we can also create a new hash map each time and combine them later on in a reduce. And Rayon has a pretty nice way of doing that.
I have not done any benchmarks whatsoever. See commit messages for more details.