Skip to content

Commit

Permalink
Add example implementation of parallel_letter_frequency exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ccadden committed Feb 23, 2024
1 parent 0c90b0e commit 8afc89c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/example.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class ParallelLetterFrequency
def self.count(texts)
rs = (0...texts.length).map do |i|
Ractor.new(texts[i]) do |text|
res = Hash.new(0)
text.downcase.each_grapheme_cluster do |c|
res[c] += 1 if c.match?(/\p{Alpha}/)
end

res
end
end

tally = Hash.new(0)

until rs.empty?
r, v = Ractor.select(*rs)
rs.delete r
v.each do |k, v|
tally[k] += v
end
end

tally
end
end

0 comments on commit 8afc89c

Please sign in to comment.