0PricingLogin
Scala for Backend Engineering & Functional Programming · Lesson

Cats Type Classes

Functor, Monad, etc.

What Are Type Classes?

A type class is a pattern for ad-hoc polymorphism. It lets you add behavior to a type without modifying it. Cats is a Scala library that provides a rich hierarchy of type classes for functional programming.

  • Functor — things you can map over
  • Applicative — combine independent effects
  • Monad — sequence dependent effects

Functor: map

A Functor[F] provides map. It transforms the value inside a context without changing the context's structure.

import cats.Functor
import cats.instances.option._

val f = Functor[Option].map(Some(2))(_ + 1)
println(f) // Some(3)

All lessons in this course

  1. Cats Type Classes
  2. The IO Monad
  3. Composing IO
  4. Error Handling in IO
← Back to Scala for Backend Engineering & Functional Programming