Motivation
I’ve been using Scalaz for a while now and I remember not having any guides that provide a gentle introduction. It was a lot of scouring around, reading source code, and watching tech talks that gave me the understanding that I have now. This series of posts is intended at filling that gap by providing simple, step-by-step tutorials that will help you get productive quickly without compromising on the functional programming concepts.
What is Scalaz?
The documentation for Scalaz (pronounced Scala-zee or Scala-zed) states:
Scalaz is a Scala library for functional programming.
It provides purely functional data structures to complement those from the Scala standard library. It defines a set of foundational type classes (e.g. Functor, Monad) and corresponding instances for a large number of data structures.
In a nutshell, Scalaz aims to do three things:
I’ll provide a quick example of each of these without going into any details.
New Datatypes
1 | scala> import scalaz._ |
Here we are creating a NonEmptyList
which is a list that is guaranteed to have atleast one element i.e. it’s never empty.
New Operations on Existing Types
1 | scala> import scalaz._ |
The first way of extracting value from an Option
comes from Scalaz and is much more expressive compared to using fold
from Scala standard library.
General-Purpose Functions
1 | scala> import scalaz._ |
The |+|
operator from Scalaz conveniently concatenated the two List
s together.