Solutions to Scala with Cats: Chapter 9
April 7, 2023These are my solutions to the exercises of chapter 9 of Scala with Cats.
Table of Contents
- Exercise 9.2: Implementing foldMap
- Exercise 9.3.3: Implementing parallelFoldMap
- Exercise 9.3.4: parallelFoldMap with more Cats
Exercise 9.2: Implementing foldMap
The signature of foldMap
can be as follows. We add Monoid
as a context bound
for B
:
To implement the body of foldMap
, we have moved the Monoid
from a context
bound to an implicit parameter list for easier access:
On the code above, we have done both steps separetely for clarity (the map
and
the foldLeft
), but we could have made the calls to func
directly in the
combine step of foldLeft
.
Exercise 9.3.3: Implementing parallelFoldMap
We can implement parallelFoldMap
as follows:
We determine the amount of batches in which to split our work based on the
number of CPUs we have available. We then determine the size of our groups by
dividing the length of our input by the number of batches we’re going to run,
rounding up. We spawn a Future
with foldMap
for each group and join them via
Future.sequence
, reducing the results of each batch with the Monoid
instance
we have in scope for B
.
Exercise 9.3.4: parallelFoldMap with more Cats
To implement parallelFoldMap
using Foldable
and Traverse
we import the
respective instances from cats.instances.vector
and the syntax extension
methods that allow us to call foldMap
and traverse
directly on Vector
instances: