Cats

Cats graphic

Overview

Cats is a library that provides abstractions for functional programming in Scala.

Scala supports both object-oriented and functional paradigms, and this is reflected in the hybrid approach of the standard library. Cats aims to provide foundational, binary-compatible, modular, accessible, and efficient abstractions for functional programming.

Cats' broader goal is to create a foundation for an ecosystem of pure, type-safe libraries, designed to support functional programming in Scala applications.

Key features of Cats:

Why "Cats"?

The name is a playful shortening of the English word category, which refers to the mathematical Theory of Categories.

However, to use Cats, you do not need to know anything about category theory. The name merely reflects the mathematical roots of the library, and its API is designed to solve practical everyday development tasks.

Getting Started

Cats is available not only on the standard JVM (Java Virtual Machine), but also for compilation to JavaScript via Scala.js and to native code via Scala Native. This allows Cats to be used in backend services, frontend applications (e.g., on React), and even in high-performance native code systems.

Adding the dependency

To start using Cats, add the following line to your build.sbt file:

libraryDependencies += "org.typelevel" %% "cats-core" % "@VERSION@"
Note: replace @VERSION@ with the current version of the library.

Cats modules

The line above includes cats-core — the main module. However, you can include other modules selectively to reduce the final application size. Here are the main modules (specify the name of the needed module instead of "cats-core"):

Type Classes

Type classes are a powerful functional programming tool that provides ad-hoc polymorphism (also known as overloading).

While many object-oriented languages use inheritance for polymorphism, functional programming tends toward a combination of:

  1. Parametric polymorphism (as in Java generics): one function works with any type, knowing nothing about it.
  2. Ad-hoc polymorphism (type classes): the same function may behave differently with different types, but this behavior is predefined and independent of inheritance hierarchies.

In simple terms: a type class defines an interface (a set of operations), and for specific types instances — implementations of that interface — are provided. This allows "adding" new behavior to existing types without modifying their source code.

Type classes in Cats (infographic)

Below is an infographic that visualizes the hierarchy of Cats' main type classes from the most general (Functor) to the most specialized (Monad, Comonad, etc.).

infographic

Author: @tpolecat. The graph shows how type classes extend each other (arrows mean "extends" or "depends on").

Brief explanation of the hierarchy (top to bottom):

Partial type class instances table in Cats

Important note: Cats provides standard instances (implementations) for many types. However, not every type supports all operations (e.g., you cannot make flatMap for Validated, since its semantics are designed for error accumulation, not for sequential action).

The following table (originally provided by @hobwekiva) shows which type classes are implemented for which built-in types.

Legend:

Data Type (container) Functor Apply Applicative Monad MonoidK ApplicativeError MonadError CoflatMap Comonad Bimonad
Id[A] (identity) ✔ ✔ ✔ ✔ ✗ ✗ ✗ ✔ ✔ ✔
Eval[A] (lazy evaluation) ✔ ✔ ✔ ✔ ✗ ✗ ✗ ✔ ✔ ✔
Option[A] (maybe absent) ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✗ ✗
Const[K, A] (constant) ✔ ✔ (if K:Monoid) ✔ (if K:Monoid) ✗ ✗ ✗ ✗ ✗ ✗ ✗
Either[E, A] (error or value) ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✗ ✗ ✗
List[A] (list) ✔ ✔ ✔ ✔ ✔ ✗ ✗ ✔ ✗ ✗
NonEmptyList[A] (non-empty list) ✔ ✔ ✔ ✔ ✗ ✗ ✗ ✔ ✔ ✔
Stream[A] (lazy list) ✔ ✔ ✔ ✔ ✔ ✗ ✗ ✔ ✗ ✗
Map[K, A] (map) ✔ ✔ ✗ ✗ ✔ ✗ ✗ ✗ ✗ ✗
Validated[E, A] (error-accumulating) ✔ ✔ (if E:Semigroup) ✔ (if E:Semigroup) ✗ ✗ ✔ (if E:Semigroup) ✗ ✗ ✗ ✗
Reader[E, A] (environment reader) ✔ ✔ ✔ ✔ ✗ ✗ ✗ ✗ ✗ ✗
Writer[E, A] (logging writer) ✔ ✔ (if E:Monoid) ✔ (if E:Monoid) ✔ (if E:Monoid) ✗ ✗ ✗ ✔ ✗ ✗

Practical meaning of the table

Looking at the table, you can quickly understand a type's capabilities:

This table is a cheat sheet that helps you choose the right abstraction for your task.

Cats Ecosystem

Besides the main modules, there are several important projects released separately:


References: