Scala with Cats is one of several of underscore‘s books focused on the Scala ecosystem. I had an opportunity to pick up a (free, kudos to them for that) physical copy of the book — already updated for cats 1.0 — at underscore’s booth during last year’s Scala eXchange.
One thing to be aware of — Scala with Cats assumes an introductory level in functional programming concepts on the part of the reader.
With that in mind, let’s see how the book is structured.
Beyond the preliminary stuff of getting
cats (weirdly enough squirrelled away in the preface), a large portion of the first chapter isn’t about the library at all. Rather, it focuses on elemental knowledge — type classes and how implicits are resolved and used in Scala.
These are explained without any examples from
cats — at first — which I find to be an excellent move — it puts little cognitive load on those readers who are actually yet unfamiliar with the concepts.
This modus operandi continues up until the first exercise — creating a type-safe “printout” typeclass. Yes, there are exercises, very well interwoven with the narrative at that.
Only about halfway into the chapter we get to use actual typeclasses from
cats. They’re the usual suspects —
Show for typesafe printing, and
Eq for typesafe equality testing. Their introduction is intertwined with further explanation of how implicits work with typeclasses, and how the “implicit part” of the
cats API is organized — this serves to reinforce the reader’s intuition.
Chapter 2 covers the first category-theoretical concepts,
Semigroup(“something that can be combined”) and
Semigroups that have a neutral element such that
i+x = x+i = x”). Compelling usage scenarios are listed out, including a currently-trendy reference to data science.
Chapter 3 focuses on
Functor (“things that can
map”) and associated typeclasses. We also learn about variance in the context of typeclasses, and why we sometimes need the
We only arrive at the (in)famous Monads at Chapter 4. After both a generalized and a
cats-specific introduction, we get a description of how well-known types can exhibit monadic behavior (e.g.
Either). Following is a selection of “standard” monads, i.e.
Eval (abstracts eager/lazy/memoized execution),
Writer (for attaching “metainformation” to computations),
Reader (abstracts execution dependent on parameters) and
State (state machine), with an aside about
MonadError and writing “custom” monads.
After that, we’re introduced to monad transformers in Chapter 5, typeclasses that allow us to transform nested monads (e.g.
Chapter 6 introduces
Applicative, and explains how the typeclass hierarchy in
cats is organized in the context of expressive power. Last, but not least, it introduces the very useful error-accumulating typeclass,
The final chapter of the “Theory” portion deals with
Traverse, the latter being a useful abstraction for converting e.g.
List[Option] to an
This, however, is not the end of the book. What follows are three “case studies”, which in actuality serve as sets of exercises with an overarching theme. They cover, reinforce, and expand upon concepts learned in the “Theory” section.
Is it any good?
In general, yes, it’s a very well-written and -structured book.
Evidently, it was created with an assumption that the reader might not know anything about category theory, and how it translates to Scala. This has two important implications:
- everything is explained, including concepts that are auxiliary to the subject matter, but nevertheless important in practice;
- the structure of the book follows a natural progression from basic to more complex abstractions.
The flipside of the latter is that some “gateway drugs” to
cats are described relatively deep in the book — mostly monad transformers and Validated, both having direct utility in virtually all non-trivial Scala projects. I’d argue that a “teaser section” beforehand would serve to improve motivation on the reader’s part.
Moreover, great care is taken to maximize accessibility — no “hieroglyphic” method names are used — they’re mentioned only in passing — and even standard-library
Set operators are invoked by name (e.g.
There’s also certainly a lot of self-awareness regarding the hype for functional programming being displayed. Jibes about FP crop up here and there, evidently intended to lighten up the mood. Arguably, there’s a bit too much of that, however not to cringe-inducing levels. In general, the style is very approachable and sensibly informal.
The structure of the book as described above also lends to its modularity — so it’s also a good book to fill in the blanks in one’s knowledge of FP in Scala/
It is very difficult to overstate the value of exercises in Scala with Cats. Virtually every piece of theory is immediately supported by at least one opportunity to use it in practice. The exercises themselves are simple, concise, and to-the-point. Furthermore, the aforementioned use cases provide even more opportunity for reinforcement. The fact that the “Solution to Exercises” section spans almost a third of the book is telling.
Really, my biggest complaint is that there are not as many cute illustrations of cats as I would’ve liked.
If you’re one of the two following groups:
people who already dipped their toes in Scala and are looking to extend their developer toolbox with functional programming abstractions,
Scala developers who are already familiar with some functional programming concepts and wish to organize and/or reinforce their knowledge;
then this book is definitely for you.
I would go far as to say that I would recommend Scala with Cats over the “Red Book”, at least for readers who don’t like creating “toy” abstractions for exercise’s sake — which is what you often do in the latter’s case. In contrast, here you’ll be writing code that will translate 1:1 to actual projects you may later develop.
Scala with Cats is available as a free download here.