# Understanding Functor and Monad With a Bag of Peanuts

When I started coded in Scala I heard someone saying List, Option, Future etc. are all monads and functors. I wanted to understand what exactly is a monad or a functor.

A quick google search and I got articles explaining like this

Seriously?

Now I am gonna tell you a fictional story which might help in understanding what exactly is a monad or a functor.

Once upon a time, there was a supermarket named Scala. I went there to buy 1kg of peanuts.

I took 1KG of peanuts but I was looking for something to carry them as obviously I could not carry them loose.

“May I help you?” a gentlemen asked me.

“I want to wrap these peanuts in a bag.” I said.

“Sure. Just give me peanuts and I will wrap them in a bag.

“Give me anything and I will hand it over to you wrapped in a bag. That’s my job.” —Unit

“What’s your name?”

“People call me **Unit** here in Scala store. People form Haskell store call me **return**” He smiled.

I also asked him to bag 3 bags of sugar 1KG each (for my friends). Then I suddenly remembered that they had asked for half KG of sugar. I asked **Unit **to make it half.

“Sorry sir, I cannot do that. All I know is how to wrap things in bag. I cannot perform any operation on what is inside.”

I was saddened by the fact that I have to unwrap the bags make them half and then give them back to **Unit** for re-bagging. Too much of work just to make sugar half. I wished if someone could help me.

“May I help you?” Another gentlemen came smiling. He unpacked each bag, called a guy who is expert in making sugar half and then re-packed it in a bag handing it over to me

“Now, What’s your name?” I asked him thanking.

“**map**, people call me **map. **People in Haskell store add an extra f before my name. They call me **fmap**. Funny people.”

I can replace a bag of sugar with a peanuts bag. I can give a bag of roasted peanuts if you give me a bag of peanuts.

“Tell me to do anything on what is inside the bag and I will do it, first by unpacking the bag and then repacking it with new thing in it.” —map

### Functor

The Bag along with me**/map **is called as **Functor**. Basically the Bag is know as a Functor when map is around.

The process of map function on Functor is very well explained by the following illustration taken from: http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Oh my god, so this is called a Functor I have been trying to learn since ages. Tears of happiness rolled down my face.

I took out the laptop and started writing the Bag Functor. Here it is.

In our story:

- Bag — is a Functor
- map — is a map function on Functor
- half — is a function which makes sugar half
- sugar/peanut — is a generic type A or a type of the content inside Functor

case classBag[A](content: A) {

defmap[B](f: A => B): Bag[B] =Bag(f(content))

}case classSugar(weight: Double)

// the guy who is expert at making sugar halfhalf = (sugar: Sugar) =>

defSugar(sugar.weight / 2)

valsugarBag: Bag[Sugar] =Bag(Sugar(1))//Bag functor of type sugar

// map is the guy in our story who can perform operations

// by unwrapping the bag and then calling respective function

// and wraps the content back in a bag

val halfSugarBag: Bag[Sugar] = sugarBag.map(sugar => half(sugar))

Where is unit in above code. Unit in above code is an apply method provided by case class and hence is not visible. Apply method takes content and returns a bag of content.

defapply(content: A): Bag[A] =newBag(content)

I was so happy that I understood the concept of **Functor**.

“But sir, we are called Functor only if we follow certain laws.” Said **map.** I was little worried now and was listening carefully.

Before I tell you the laws, let me show you a funny guy. Do you see that funny guy. He is called ** identity. **He is so funny that if you hand over 1KG sugar he will give you back 1KG sugar. Essentially he will give back to you, whatever you give to him. Funny fellow. Not the laws.

If you give me 1KG sugar, I will give you 1KG sugar. You give me whatever, I will give it back to you —Identity

defidentity[A](x: A): A = x

**1. Identity law**

When map is called on a Functor with identity function, you get the Functor back.

Functor[X].map(x => identity(x)) == Functor[X]

or

Functor[X].map(identity) == Functor[X]

If you give me a Bag of 1 KG sugar and ask me to re-wrap it by calling identity to do his work on the sugar, then you should get a Bag of 1KG Sugar —map

This law makes sure that the map functions only applies the function which is passed to it on the contained value and does not perform any other operation of it’s own.

I started verifying the law:

valsugarBag =Bag(Sugar(1))sugarBag.map(identity) === sugarBag

#### 2. **associative law**

Let f and g be functions we want to apply on the value contained in functor. Then, calling map with f and then map with g is equivalent to calling map with g composed with f (g after f or g(f(x))).

If you give me a Bag of 1 KG peanuts and ask me to roast them I will give you a bag of roasted peanuts. If you ask me to salt the bag of roasted peanuts I will again unwrap the bag of roasted peanuts, will salt them and will give you back.

This operation is equivalent to I unwrapping the bag of peanuts roasting them, then salting them and then repacking back.

—map

Functor[X].map(f).map(g) == Functor[X].map(x => g(f(x))

This law allows us to chain map function calls instead of composing multiple functions and then applying them in a single map operation.

**Examples of functors in Scala**

- Option
- List
- Future
- Try
- Either

**What is the benefit of functors**

Let’s take an example of Option functor.

Option returns None or Some value. i.e. the container may or may not contain a value.

Let’s say we want to add an Int value to an Option of Int and then multiply it by another number. Without map method the implementation would be something like follow:

val x: Option[Int] = Some(1)

val y: Int = 2

val m: Int = 2

val z = if(x.isDefined) Some((x.get + y) * m) else None

The map method on functor simplifies this logic.

val x: Option[Int] = Some(1)

val y: Int = 2

val m: Int = 2

val z = x.map(a => (a+y) * m)

//or with the help of associative lawvalz = x

.map(_ + y)

.map(_ * m)

### Monad

I was happy with functors as they were really helpful whenever I visited the Scala supermarket. One day I visited the supermarket and bought a bag of 1KG Sugar. Then I realized I need 2KG of sugar. I went to **map** asking for help. map unwrapped the sugar and asked a guy to double the sugar. The guy who was expert is doubling the sugar was over smart. He not only doubled the sugar but also packed it in a bag and handed over the bag to **map**. map faithfully packed whatever was given by the expert guy and handed over me the bag.

Now I had a bag of 2KG sugar wrapped in to an another bag. I was a little irritated by it. If it was already packed in a bag why did map pack it again. Won’t it be good to pack the sugar only once. I didn't want to unpack two bags to use sugar.

“Why did you pack the bag into another one if the sugar was already packed” I asked map.

“Sorry sir, I have to follow the laws. I am doing my job. I cannot make any mistake in it. My job is to unwrap the bag give the stuff inside to someone and then repack whatever is given back by the someone. I don’t care if it already wrapped or not. I have to wrap it again.” — map

I asked him if is there someone who can flatten it for me? I just want one wrapping.

“**flatten **can help you.” He called another gentleman named **flatten**.

“give me the double wrapped bag and I will flatten it. I will give you sugar wrapped into only one bag” — **flatten**

“Give me stuff doubly wrapped into bag and I will return you the same stuff but with a single wrapping” —flatten

I was so happy to receive sugar in single wrapper as it was convenient for me.

Do you remember the **unit** guy. The bag with **unit, map and flatten** makes a **Monad**.

It was another shock for me. **Monad!, **so this is what is a Monad. It’s like a **Functor** who knows how to **flatten**.

“**unit, map , flatten** you guys are cool but wouldn’t it be convenient if there is someone who knows how to unwrap/wrap and flatten both. Someone who can do the job of** map and flatten**” I said.

“Are you taking about me?” I heard a voice.

“My name is **flatMap** and I do exactly that thing. I can do the job of map and flatten both.”

The bag with **unit and flatMap **makes a **Monad**.

It was time to take out laptop and code the very first monad. I had already created a Bag functor. I could simply extend the same functor to make it monad by adding **flatMap or flatten**.

case classBag[A](content: A) {

defmap[B](f: A => B): Bag[B] =Bag(f(content))

defflatMap[B](f: A => Bag[B]): Bag[B] = f(content)

defflatten = content

}

defdouble = (sugar: Sugar) =>Bag(Sugar(sugar.weight * 2))

valdoubleSugarBag = sugarBag.map(sugar => double(sugar)).flatten

orvaldoubleSugarBag = sugarBag.flatMap(sugar => double(sugar))

Yes we are like Functor but we also know how to flatten the double wrapping. “Do you have to follow certain laws like Functors?” I asked.

“Yes we have to obey following laws to call our self a Monad”

#### 1. **Left-identity law**

unit(x).flatMap(f) == f(x)

i.e.

Bag(Sugar(1)).flatMap(double) == double(Sugar(1))

//or to be more explicit the same statement could be written as

Bag.apply(Sugar(1)).flatMap(z => double(z)) == double(Sugar(1))

Here

**unit**is**apply**function on the monad.**f**is the**double**function which doubles the quantity**x**is 1KG Sugar i.e. Sugar(1)

So the Bag Monad fulfills the first law. Hurrah.

#### 2. Right-identity law

Monad[X].flatMap(unit) == Monad[X]

i.e.

Bag(Sugar(1)).flatMap(Bag.apply) ==Bag(Sugar(1))

Bag monad fulfills the second law as well.

#### 3. **Associativity law**

m.flatMap(f).flatMap(g) == m.flatMap(x ⇒ f(x).flatMap(g))

i.e.

Bag(Sugar(1)).flatMap(double).flatMap(tripple) ==Bag(Sugar(1)).flatMap(x ⇒ double(x).flatMap(tripple))

So the Bag monad fulfils all the laws.

**List of some important Monads in Scala**

- List
- Option
- Future
- Try

Hope you loved the story. Clap for it if you liked it. Don’t forget to follow me.