Learn functional programming with Scalaz

About this book

This book is an introduction to functional programming using Scalaz. To read this book, you need to be familiar with basic Scala syntax including functions, classes, singleton objects, traits, and pattern matching. You also need some non-basic Scala concepts(like implicits) but you'll learn them through the book. Many concepts are taught through examples and exercises. So, It's highly recommended that you work through the exercises.

A word about functional programming

In recent years, functional programming (or FP) regained it's fame, thanks to the rise of multi-core CPUs. But concurrency is not the only aspect that FP shines at. Using even the basics of FP (immutability and purity) makes it easier to read and reason about code, but there's more. FP enables us to use the patterns brought by Category theory to compose programs into bigger programs.

It's hard to provide a precise definition for functional programming, but we can begin with that FP is about trying to make our functions (in our programs) look like mathematical functions and avoid side effects and mutable data structure. When we say a language is suitable for FP, it means that the language provides syntax, tools, and structures that ease using such functions. Using immutable data-structures cut both ways; sometimes it's hard to write elegant code in using just immutable data-structures, especially if the language doesn't offer any help. FP languages usually support these criteria:

  • Language should treat Functions as first-class citizens: Language must provide syntax and tools for easily :

    • Defining functions

    • Pass them around

    • Create new function by composing existing ones (instead of defining them)

  • Language must provide an army of immutable data-structures as defacto structures so that libraries expose their API with immutable data-structures too

  • Strong typing dramatically increases the reliability of functional code

  • Supporting advanced type definitions (like higher-kinded types) makes writing functional code a lot smoother

Still, you can use FP in a language with weak support of the above features, for example, the amazing book Mostly adequate guide to functional programming uses JavaScript, which doesn't have strong typing and is terrible at supporting immutable data-structures.

A word about Category Theory

One of the things that makes FP amazing is the concepts that are borrowed from Category Theory (e.g Monads). These concepts are usually, powerful abstractions that make programs more composable and thus make the code more concise and elegant. Many brutally hard problems in real-world, day to day development domains have beautiful FP solutions.

Although Category theory is not easy to learn (even for mathematicians!), The good news is that you don't have to know or learn category theory to be able to use these kinds of techniques! Chances are that you already used lots of them without even hearing about Category Theory, For example, if you've ever used map and flatMap on a List or an Option, you used the mighty Monad without ever knowing about it.

Why I wrote this book

I've always admired Scalaz, I wondered whether it's possible to use it in the real world, in real problems. But when I tried using Scalaz in my small projects, I found that it's somehow assumed that if you want to use Scalaz, you know functional programming very well (and a little Category theory ofcourse), but if you're not used to functional programming and it's terminology, you're stuck. Learning resources are limited to a few video presentations and a few blogs (Eugene Yokota's blog is awesome). And it's how I got the idea of writing a small handbook to help others that want to use Scalaz.

More learning resources

A Quick look

This is a work in progress, chapters 0, 1 and 2 are ready at this time.

List of chapters

  • Since Scala implicits are widely used throughout the book, chapter zero covers this concept (feel free to skip it if you feel confident in using implicits)

  • Chapter one introduces Typeclasses and Scalaz

  • Chapter two introduces Semigroup

About

This book is sponsored by Sanjagh

Last updated

Was this helpful?