Tracks
/
Scala
Scala
/
Exercises
/
Parallel Letter Frequency
Parallel Letter Frequency

Parallel Letter Frequency

Medium

Instructions

Count the frequency of letters in texts using parallel computation.

Parallelism is about doing things in parallel that can also be done sequentially. A common example is counting the frequency of letters. Create a function that returns the total frequency of each letter in a list of texts and that employs parallelism.

According to this terminology you should write a parallel and deterministic program and (by all means!) let Scala deal with the concurrency aspect. Or else your code could quickly get messy and error-prone with all kinds of nasty concurrency bugs. In particular your program could become indeterministic which spells in practice: very (in fact, VERY) hard to debug, test and reason about.

Having said that it might be a good idea to first write a sequential solution (and use the test suite to verify it). Only then should you try to parallelize it while keeping the sequential and parallel portions of your code as separate as possible.

A first iteration could be using Scala's parallel collections. You might find that this is almost too simple (especially if you have followed our advice and already have a sequential solution).

For the second iteration we recommend you try a solution with scala.concurrent.Future. You can consult this tutorial and its sequel for some help. Make sure that you

  • have only one single blocking call to wait for the result
  • that it is at the very end of your program, and
  • that it has a timeout.

scala.concurrent.Future is used in many libraries and the doctor's advice for parallel and asynchronous programming in Scala. So it is essential for mastering the language and it should become part of your Scala armory.

Edit via GitHub The link opens in a new window or tab
Scala Exercism

Ready to start Parallel Letter Frequency?

Sign up to Exercism to learn and master Scala with 94 exercises, and real human mentoring, all for free.

Deep Dive into Parallel Letter Frequency!

We explore the differences between concurrency and parallelism, looking at different approaches taken by languages such as JavaScript, Go, Elixir and Rust.