What are Scala Type Classes?

What are Scala type classes, what kind of problem do they solve and how are they implemented?

In a nut shell, type classes provide polymorphism without using subtyping, but in a completely type safe way.

Type classes represent some common functionality that can be applied to values of many different types. Moreover, we don't have to change existing types in order to extend them with the new functionality.

In this post I will describe 5 simple steps for encoding a type class in Scala in an idiomatic way.

Continue reading →

7 Most Convenient Ways To Create A Future Either Stack

In Scala Future[A] and Either[A, B] are very useful and commonly used types. Very often we want to combine them, but a Future[Either[A, B]] is kind of awkward to handle not only because we don't want to have to call Await anywhere.

One way to deal with this is to stack the types into a combined data type EitherT defined in Cats that is much easier to handle.

Still it can be quite unwieldy to compose values of this new type with other values of different types.

To get nice composability (e.g. with for comprehensions) we have to wrap other values into the new type by lifting them up inside the monad stack.

Here are the most convenient ways that I found to do that.
Continue reading →

Up your game by stacking Applicatives in Scala

Monads are very useful in many different situations.

But they get a little unwieldy when we have different Monads nested inside each other.

In these cases Monad Transformers come to the rescue. They allow us to compose different Monads into one that shares the functionalities of all of them.

But sometimes we want to combine the behavior of Applicatives in the same way. This is especially useful when we have to combine independent tasks.

In this post we will see how to do this in Scala with the use of the Cats library.

But let's first look at an example of Monad Transformers.

Continue reading →

Hands on Monoids in Scala – Applying categories to birdwatching

What do Monoids in Scala have to do with birdwatching?

Before I come to that I would like to mention that since I'm coming from F# and recently started with Scala, being able to use type classes in my code is new to me.

I find this really exciting which I'd like to share.

So this post is maybe not a complete cohesive tutorial an Monoids but I hope that it is a fun little teaser for looking into things like functional programming, Scala, Cats, Haskell, type classes or maybe even category theory.

Continue reading →