Explore Either in Cats Library

explore either in cats library
Reading Time: 4 minutes

Hey everyone, I’m back with another blog in which we are going to explore Either in Cats Library. For an introduction to Either Monad in Cats and comparison between Either in standard Scala Library and Cats Library, you can have a look at my previous blog i.e Either in Cats vs Either in Scala. Also, if you don’t know what is a Monad you can have a look at this blog.

In this blog, we will be exploring many extension methods that are provided in Cats which can be very helpful. So, let’s dive in!

Either Monad in Cats

Either is a Monad that represents a value of two possible types i.e. ether Right(correct value) or Left(error/failure).

In Cats we can create object of Either type in following ways:

  • By using Right.apply and Left.apply
either using Left.apply and Right.apply
  • By importing cats.syntax.either._, which will provide us with many helpful methods and one of those are “Smart Constructors”
Either using .asRight and .asLeft

For the detailed difference between the two you can visit my previous blog.

Moving forward, let’s discuss different extension methods provided to us after importing the syntax.

Extension Methods

catchOnly

It can be used to catch exception of a specific kind. If we catch the specific exception then it will wrap the exception in Left otherwise the value will be wrapped in Right.

It is good if you are concerned about a specific exception.

The problem with this is that the other exceptions will be propagated.

catchOnly example
cathOnly result

Similar to this we have caatchNonFatal too, which will only catch non-fatal exceptions.

getOrElse

It is for extracting value from Right or returning a default.

getOrElse Example
getOrElse solution

Similar to this we have orElse also.

ensure

It will allow us to check if the right value satisfies a predicate. If not then it will return value of Left type.

ensure example
ensure solution

recover

As the name suggests it is for recovering from the error. In recover block, we give a partial function and on basis of that, if the error occurs, the value will be recovered. In short, instead of error i.e. a left value it will return a Right value.

recover example
recover solution

Similar to this, we have recoverWith also.

The “map” method works on the right value only. To complement it we have the following:

leftMap

This will do the mapping on left value in Either.

leftMap example
leftMap solution

biMap

It will map both the values and for both of them, we can have different mappings.

biMap example
biMap solution

We also have conversion methods which convert others to Either and also converts Either to others. Let’s look at them as well.

toOption

It will convert an Either to Option. If we have a Right value then it will be wrapped in Some else None.

toOption example
toOption answer

fromOption

It will take Option value and convert it to Either.

fromOption example
fromOption answer

There are many more similar methods such as fromTry, toTry, toList etc.

That’s all for this blog. Here you can find all the code. I hope it was helpful 😀

If you like it, give it a thumbs up. See you in the next blogs.

Reference

Written by 

Muskan Gupta is a Software Consultant having almost 1-year experience. She has knowledge of languages like C, C++, Java and Scala. She is familiar with Object-Oriented Programming Paradigms and also has an interest in relational database technologies. Her hobbies include travelling, reading novels and listening to music.