Core functional programming abstractions for Scala providing fundamental type classes like Functor, Applicative, and Monad with their instances and syntax extensions.
npx @tessl/cli install tessl/maven-org-typelevel--cats-core-sjs0-6-2-11@2.0.0Cats-core provides essential functional programming abstractions for Scala, serving as the foundation of the Cats ecosystem. It offers a comprehensive collection of type classes, data types, syntax extensions, and instances that enable developers to write pure, composable, and type-safe functional code.
libraryDependencies += "org.typelevel" %%% "cats-core" % "2.0.0"The most common import brings all syntax and instances into scope:
import cats._
import cats.implicits._Selective imports for specific functionality:
import cats.{Functor, Applicative, Monad}
import cats.data.{Validated, NonEmptyList, Chain}
import cats.syntax.functor._
import cats.syntax.applicative._import cats._
import cats.implicits._
import cats.data._
// Using type classes with syntax
val numbers = List(1, 2, 3)
val doubled = numbers.map(_ * 2) // Using Functor syntax
// Working with validated data
val valid1: Validated[String, Int] = Validated.valid(42)
val valid2: Validated[String, Int] = Validated.valid(10)
val combined = (valid1, valid2).mapN(_ + _) // Using Applicative syntax
// Non-empty collections
val nel = NonEmptyList.of(1, 2, 3, 4)
val sum = nel.reduce // Safe reduction without empty case
// Monad transformers for composing effects
val result: OptionT[List, Int] = for {
x <- OptionT.liftF(List(1, 2, 3))
y <- OptionT.pure[List](10)
} yield x + yCats-core is built around several key architectural concepts:
Core abstractions that form the foundation of functional programming patterns.
// Functor - map operation
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
// Applicative - pure and apply operations
trait Applicative[F[_]] extends Functor[F] {
def pure[A](x: A): F[A]
def ap[A, B](ff: F[A => B])(fa: F[A]): F[B]
}
// Monad - flatMap operation
trait Monad[F[_]] extends Applicative[F] {
def flatMap[A, B](fa: F[A])(f: A => F[B]): F[B]
def tailRecM[A, B](a: A)(f: A => F[Either[A, B]]): F[B]
}Functional data types that provide safety guarantees and composable operations.
// Validated - accumulating validation
sealed abstract class Validated[+E, +A]
case class Valid[A](a: A) extends Validated[Nothing, A]
case class Invalid[E](e: E) extends Validated[E, Nothing]
// NonEmptyList - guaranteed non-empty list
final case class NonEmptyList[+A](head: A, tail: List[A])
// Chain - efficient sequence with O(1) append
sealed abstract class Chain[+A]Composable monadic effects that allow stacking of different effect types.
// EitherT - Either over any monad
final case class EitherT[F[_], A, B](value: F[Either[A, B]])
// OptionT - Option over any monad
final case class OptionT[F[_], A](value: F[Option[A]])
// Kleisli - function composition in monadic context
final case class Kleisli[F[_], A, B](run: A => F[B])Implicit syntax that enhances existing types with functional programming operations.
// Enhanced operations available through implicits
implicit class FunctorOps[F[_], A](fa: F[A])(implicit F: Functor[F]) {
def map[B](f: A => B): F[B] = F.map(fa)(f)
def void: F[Unit] = F.void(fa)
def as[B](b: B): F[B] = F.as(fa, b)
}
implicit class ApplicativeOps[A](a: A) {
def pure[F[_]](implicit F: Applicative[F]): F[A] = F.pure(a)
}Pre-defined implementations of type classes for standard library types.
// Available instances (imported via cats.implicits._)
implicit val optionMonad: Monad[Option]
implicit val listMonad: Monad[List]
implicit val eitherMonad[E]: Monad[Either[E, *]]
implicit val function1Monad[A]: Monad[Function1[A, *]]Core type aliases and re-exports available from the cats package:
// Identity and function types
type Id[A] = A
type Endo[A] = A => A
type ~>[F[_], G[_]] = arrow.FunctionK[F, G]
// Kernel re-exports
type Eq[A] = cats.kernel.Eq[A]
type Order[A] = cats.kernel.Order[A]
type Semigroup[A] = cats.kernel.Semigroup[A]
type Monoid[A] = cats.kernel.Monoid[A]