Understanding IO Monad in Scala

Akash Srivastava
Walmart Global Tech Blog
6 min readJan 13, 2020

--

source: Google

Hey guys!

In my last post, we discussed how to set up a simple REST service in Scala, wherein we used cats-effect IO Monad to wrap our effect-ful (or you can say side-effect-ing) code to ensure it remains functionally pure (referentially transparent).

Now anyone new to the Functional Programming world will be swarming with questions such as:

  • What are side-effects and Referential Transparency?
  • How does it affect my code?
  • What is this IO Monad you are talking about? and How does it magically fixes my code?

At least, these were the question I had when I first encountered the IO monad. So, I thought I should put up a little something for anyone having the above questions.

Now, let’s go for it!

What is an effect/side-effect?

A side-effect is a function/expression which changes/mutates the state of something outside its local environment.

It can be something as simple as printing to console, making a DB call or a file read/write operation.

Before we go ahead, let me introduce you to Referential Transparency.

By definition, it is a property of purely functional languages that says an expression always gets evaluated to…

--

--