-
-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example implementation of parallel_letter_frequency exercise
Co-authored-by: KOTP <[email protected]>
- Loading branch information
Showing
2 changed files
with
27 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class ParallelLetterFrequency | ||
def self.count(texts) | ||
ractors = (0...texts.length).map do |i| | ||
Ractor.new(texts[i]) do |text| | ||
text.downcase.each_grapheme_cluster.select do |cluster| | ||
cluster.match?(/\p{Alpha}/) | ||
end.tally | ||
end | ||
end | ||
|
||
tally = Hash.new(0) | ||
|
||
until ractors.empty? | ||
ractor, result = Ractor.select(*ractors) | ||
ractors.delete ractor | ||
result.each do |key, value| | ||
tally[key] += value | ||
end | ||
end | ||
|
||
tally | ||
end | ||
end |
6 changes: 4 additions & 2 deletions
6
exercises/practice/parallel-letter-frequency/parallel_letter_frequency.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
=begin | ||
Write your code for the 'Parallel Letter Frequency' exercise in this file. Make the tests in | ||
`parallel_letter_frequency_test.rb` pass. | ||
Write your code for the 'Parallel Letter Frequency' exercise in this file. Make | ||
the tests in `parallel_letter_frequency_test.rb` pass. | ||
To get started with TDD, see the `README.md` file in your | ||
`ruby/parallel_letter_frequency` directory. | ||
=end | ||
|