Below you will find pages that utilize the taxonomy term “fp”

Posts

# 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?

Posts

# 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

Posts

# The Tagless Final Pattern

Tagless Final is something you may have heard about. It’s a functional programming pattern but what exactly is it?
In short, tagless final is similar in concept to the traditional dependency injection pattern used by frameworks such as Spring, Guice or Macwire. However we can abstract over the “Effect” and write generic code
The code here is not complete, it just illustrates the concepts. For a working example please see my associated github repo

Posts

# 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 ValidatedNel example Why do we need Validated?

Posts

# 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.

Posts

# 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.

Posts

# 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.

Posts

# 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?

Posts

# 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 In particular, check out the Functors example

Posts

# What is a Semigroup?

We often need to aggregate data. Hadoop/Spark map-reduce is a classic example of this (the reduce part). There are other more trivial examples - e.g. summing the line items in a shopping basket to calculate the total basket price
Semigroups allow us to parallelize operations on large and small data sets, then combine the results together. In essence Semigroup encapsulates the reduce part of map reduce.
You can find complete examples of the concepts discussed on my blog in my Github repo.

Posts

# What is a Monoid?

We often need to combine data types that may be “empty” e.g. empty Lists or Options. How do we combine a List[Int] to produce an Int if the List is empty? What is the result? How about List[String] => String. Logically we would expect zero for the Int example and "” for the String but how do we represent this?
The simple Monoid extends Semigroup and adds a default or fallback value for the given type.

Posts

# Type classes for beginners

Type classes are everywhere in the Scala ecosystem. If you want to learn advanced libraries like Scalaz, Cats or Shapeless you need to know about them. Even if you don’t plan to use these libraries you can (and probably should) use type classes in your own applications. Type classes are borrowed from Haskell and are sometimes called “ad-hoc polymorphism”. If you want to know what that means read on
Type classes are like the adapter pattern on steroids.