Scalaz-core provides essential functional programming abstractions for Scala including type classes, data structures, and monad transformers.
npx @tessl/cli install tessl/maven-org-scalaz--scalaz-core-sjs1-2-13@7.3.0Scalaz-core is the foundational module of the Scalaz library, providing essential functional programming abstractions for Scala. It implements a comprehensive collection of type classes including Functor, Applicative, Monad, Foldable, Traversable, and many others that form the mathematical foundations of functional programming. The library offers data structures like Free monads, State transformers, Reader/Writer monads, and validation types that enable composable and pure functional programming patterns.
libraryDependencies += "org.scalaz" %%% "scalaz-core" % "7.3.8"import scalaz._
import Scalaz._ // Imports all instances and syntaxFor selective imports:
import scalaz.{Functor, Applicative, Monad, \/}
import scalaz.std.option._ // Option instances
import scalaz.std.list._ // List instances
import scalaz.syntax.monad._ // Monad syntaximport scalaz._
import Scalaz._
// Using type class instances
val doubled = List(1, 2, 3).map(_ * 2) // Uses Functor[List]
// Validation for error accumulation
val result = (
"John".successNel[String] |@|
25.successNel[String] |@|
"john@example.com".successNel[String]
) { (name, age, email) => User(name, age, email) }
// Disjunction (Either-like) with right bias
val computation: String \/ Int = \/-(42)
val mapped = computation.map(_ * 2) // Results in \/-(84)
// State monad for stateful computations
val statefulComp = for {
current <- State.get[Int]
_ <- State.put(current + 1)
final <- State.get[Int]
} yield final
val (finalState, result) = statefulComp.run(10) // (11, 11)Scalaz-core follows the mathematical structure of category theory and abstract algebra:
The library provides three main packages:
Essential type classes forming the mathematical foundations of functional programming, including functors, applicatives, monads, and their relationships.
trait Functor[F[_]] {
def map[A, B](fa: F[A])(f: A => B): F[B]
}
trait Apply[F[_]] extends Functor[F] {
def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B]
}
trait Applicative[F[_]] extends Apply[F] {
def point[A](a: => A): F[A]
}
trait Bind[F[_]] extends Apply[F] {
def bind[A, B](fa: F[A])(f: A => F[B]): F[B]
}
trait Monad[F[_]] extends Applicative[F] with Bind[F]
trait Foldable[F[_]] {
def foldMap[A, B](fa: F[A])(f: A => B)(implicit F: Monoid[B]): B
def foldRight[A, B](fa: F[A], z: => B)(f: (A, => B) => B): B
}
trait Traverse[F[_]] extends Functor[F] with Foldable[F] {
def traverseImpl[G[_], A, B](fa: F[A])(f: A => G[B])(implicit G: Applicative[G]): G[F[B]]
}Immutable functional data structures designed for composition, including collections, trees, validation types, and specialized containers.
sealed abstract class NonEmptyList[+A] {
def head: A
def tail: List[A]
def map[B](f: A => B): NonEmptyList[B]
def flatMap[B](f: A => NonEmptyList[B]): NonEmptyList[B]
}
sealed abstract class Tree[A] {
def rootLabel: A
def subForest: Stream[Tree[A]]
def map[B](f: A => B): Tree[B]
}
sealed abstract class \/[+A, +B] {
def map[C](f: B => C): A \/ C
def flatMap[C](f: B => A \/ C): A \/ C
def isLeft: Boolean
def isRight: Boolean
}
sealed abstract class Validation[+E, +A] {
def map[B](f: A => B): Validation[E, B]
def ap[EE >: E, B](f: => Validation[EE, A => B]): Validation[EE, B]
}Composable monad transformers for combining effects, including State, Reader, Writer, and their combinations.
case class StateT[S, F[_], A](run: S => F[(S, A)]) {
def map[B](f: A => B)(implicit F: Functor[F]): StateT[S, F, B]
def flatMap[B](f: A => StateT[S, F, B])(implicit F: Monad[F]): StateT[S, F, B]
}
type State[S, A] = StateT[S, Id, A]
case class ReaderT[E, F[_], A](run: E => F[A]) {
def map[B](f: A => B)(implicit F: Functor[F]): ReaderT[E, F, B]
def flatMap[B](f: A => ReaderT[E, F, B])(implicit F: Monad[F]): ReaderT[E, F, B]
}
case class WriterT[W, F[_], A](underlying: F[(W, A)]) {
def map[B](f: A => B)(implicit F: Functor[F]): WriterT[W, F, B]
def flatMap[B](f: A => WriterT[W, F, B])(implicit F: Monad[F], W: Semigroup[W]): WriterT[W, F, B]
}Type class instances for Scala and Java standard library types, enabling functional programming patterns with familiar types.
// Available instances (imported via scalaz.std.* or Scalaz._)
implicit val listMonad: Monad[List]
implicit val optionMonad: Monad[Option]
implicit val vectorMonad: Monad[Vector]
implicit val setMonoid: Monoid[Set[A]]
implicit val stringMonoid: Monoid[String]
implicit val intOrder: Order[Int]
implicit val booleanEqual: Equal[Boolean]Implicit conversions and operators providing a convenient DSL for functional programming operations.
// Extension methods available via scalaz.syntax.* imports
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 fproduct[B](f: A => B): F[(A, B)] = F.fproduct(fa)(f)
}
implicit class ApplicativeOps[F[_], A](fa: F[A])(implicit F: Applicative[F]) {
def |@|[B](fb: F[B]): ApplicativeBuilder[F, A, B]
}
implicit class MonadOps[F[_], A](fa: F[A])(implicit F: Monad[F]) {
def flatMap[B](f: A => F[B]): F[B] = F.bind(fa)(f)
def >>[B](fb: => F[B]): F[B] = F.bind(fa)(_ => fb)
}Free monads and cofree comonads for building composable, purely functional DSLs and interpreters.
sealed abstract class Free[S[_], A] {
def map[B](f: A => B): Free[S, B]
def flatMap[B](f: A => Free[S, B]): Free[S, B]
def foldMap[M[_]](f: S ~> M)(implicit M: Monad[M]): M[A]
}
object Free {
def liftF[S[_], A](sa: S[A]): Free[S, A]
def pure[S[_], A](a: A): Free[S, A]
}
sealed abstract class Cofree[S[_], A] {
def head: A
def tail: S[Cofree[S, A]]
def map[B](f: A => B): Cofree[S, B]
}