Blog
Learning Functional Programming with Scala Cats
Like most blogs, mine is not too well structured so I thought it would be useful to tie the various posts together If you want to learn FP and Cats I suggest reading the posts in this order: Type classes for beginners - Cats makes extensive use of type classes. A good understanding of this concept is therefore a prerequisite. Don’t be scared! the concept is quite simple What is a Functor?
Writer Monad
Logging is something we all need to do. There are a many logging frameworks available with various layers of abstraction (logback, slf4j, scalalogging etc). These libraries are all side effecting. As good functional programmers we want defer side effects and push them to the boundaries of the system. You can find complete examples of the concepts discussed on my blog in my Github repo . In particular, check out the Writer monad example The problem with logging So what exactly is the problem with logging?
Stacking Monad Transformers
We can stack monads together, going beyond a simple Future, Either, Result combination. We do this by stacking monad transformers. In this post I will show you how
Validated & ValidatedNel
In a previous post I explained that Cartesians and Applicatives are used to perform N independent operations. One such example is validation. We typically want to validate multiple fields and accumulate errors. Cats includes a data type designed specifically for this. It’s called Validated You can find complete examples of the concepts discussed on my blog in my Github repo . In particular, check out the Validated example Why do we need Validated?
Applicatives vs Monads
You’ve probably heard of Monads or the phrase “monadic operations” (flatMap). You may also have heard of “Cartesians” or “Applicatives”. What are these patterns and why do you need them? An Applicative is actually a specialised form of a Cartesian and it’s full name is an “Applicative functor”. Applicatives are generally more useful than cartesians so I’ll focus on them here. At the end of this post I’ll explain cartesian. You can find complete examples of the concepts discussed on my blog in my Github repo .
MonadError - Handling failed futures functionally
Scala’s Either type allows us to deal with two paths of execution (Left or Right). Futures do the same (Success or Failure). Stacking Future and Either results in three execution paths (Success-Left, Success-Right, Failure) and it’s easy to forget about the third scenario. Cats’ MonadError allows us to reduce (Success-Left, Success-Right, Failure) to (Left or Right) by transforming a failed future into a Success-Left. You can find complete examples of the concepts discussed on my blog in my Github repo .
Monad transformers
In my previous article I talked about Monads: what they are and why we use them. I explained that whilst we can write generic code capable of mapping any combination of Futures, Options, Lists etc. we can’t necessarily do this when usingflatMap or flatten. To write generic code we need to use something called a monad transformer. You can find complete examples of the concepts discussed on my blog in my Github repo .
What is a Monad?
You’re probably already using Monads but you may not realise it. If you’ve called flatMap on an Option or Future you’re playing with Monads. But what exactly are Monads and why do we use them? In very simple terms a Monad has a map and flatMap method and we use them for sequencing effectful operations e.g. fetch a user based on her id; then fetch the orders associated with this user What is a Monad?
What is a Functor?
You’re probably familiar with the map method in the Scala standard library. Collections, Futures and Options all have a map method but unfortunately there’s no base class for mappable types, making it hard to write generic code Cats’ Functor type class allow us to write generic code that can be used for Futures, Options, Lists and more You can find complete examples of the concepts discussed on my blog in my Github repo .