Solutions to Scala with Cats: Chapter 5
April 5, 2023These are my solutions to the exercises of chapter 5 of Scala with Cats.
Table of Contents
Exercise 5.4: Transform and Roll Out
We can rewrite Response
using a monad transformer as follows:
We can implement getPowerLevel
as follows. Note that we need an implicit
ExecutionContext
in scope so that we can have an instance of Functor
for
Future
, even if we just create our Future
s with Future.successful
(which
doesn’t need one). We are using the global ExecutionContext
for convenience.
To implement canSpecialMove
we can request the power levels of each autobot
and check if their sum is greater than 15. We can use flatMap
on EitherT
which makes sure that errors being raised on calls to getPowerLevel
stop the
sequencing and have canSpecialMove
return a Response
with the appropriate
error message.
To implement tacticalReport
, we need to produce a String
from a Future
, so
we must use Await
.