ZIO Prelude is a lightweight, distinctly Scala take on functional abstractions, with tight ZIO integration.
ZIO Prelude is a small library that brings common, useful algebraic abstractions and data types to scala developers. It is an alternative to libraries like Scalaz and Cats based on radical ideas that embrace modularity and subtyping in Scala and offer new levels of power and ergonomics. It throws out the classic functor hierarchy in favor of a modular algebraic approach that is smaller, easier to understand and teach, and more expressive.
ZIO Prelude has three key areas of focus:
Future
, Option
, ZIO Task
). These patterns are named after the algebraic laws they satisfy (associativity, commutativity, and identity) and the structure they produce, whether a tuple or an either.Design principles behind ZIO Prelude:
ZIO Prelude gives us:
Validation
and NonEmptyList
to enable more accurate domain modeling and handle common problems like data validation. For example:
NonEmptyList
, NonEmptySet
ZSet
, ZNonEmptySet
Validation
, ZValidation
The library has a small research-stage package (zio.prelude.fx
) that provides abstraction over expressive effect types like ZIO and ZPure
.
ZIO Prelude is a library focused on providing a core set of functional data types and abstractions that can help you solve a variety of day to day problems. The tools provided by ZIO Prelude fall into the following main categories:
In order to use this library, we need to add the following line in our build.sbt
file:
libraryDependencies += "dev.zio" %% "zio-prelude" % "1.0.0-RC35"
In this example, we are going to create a simple voting application. We will use two features of ZIO Prelude:
Topic
and Votes
data types.Associative
type class for Votes
data type which helps us to combine Votes
values.import zio.prelude._
object VotingExample extends scala.App {
object Votes extends Subtype[Int] {
implicit val associativeVotes: Associative[Votes] =
new Associative[Votes] {
override def combine(l: => Votes, r: => Votes): Votes =
Votes(l + r)
}
}
type Votes = Votes.Type
object Topic extends Subtype[String]
type Topic = Topic.Type
final case class VoteState(map: Map[Topic, Votes]) { self =>
def combine(that: VoteState): VoteState =
VoteState(self.map combine that.map)
}
val zioHttp = Topic("zio-http")
val uziHttp = Topic("uzi-http")
val zioTlsHttp = Topic("zio-tls-http")
val leftVotes = VoteState(Map(zioHttp -> Votes(4), uziHttp -> Votes(2)))
val rightVotes = VoteState(Map(zioHttp -> Votes(2), zioTlsHttp -> Votes(2)))
println(leftVotes combine rightVotes)
// Output: VoteState(Map(zio-http -> 6, uzi-http -> 2, zio-tls-http -> 2))
}
Learn more on the ZIO Prelude homepage!
For the general guidelines, see ZIO contributor's guide.
See the Code of Conduct