# Understanding Functor and Monad With a Bag of Peanuts

Sujit

24 Apr 2018

â€˘

When I started coding 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 it like this

Seriously?

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

I bought 1KG of peanuts but I was looking for something to carry them as obviously I could not take them home without a bag.

â€śI want to wrap these peanuts in a bag.â€ť I said.

â€śSure. Just give me the 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

â€śPeople call me Unit here in the Scala store. People from the 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 a KG of sugar. I proceeded to ask Unit to make it half.

â€śSorry sir, I cannot do that. All I know is how to wrap things in a bag. I cannot perform any operation on what is inside.â€ť

I was saddened by the fact that I had to unwrap the bags to half what was inside, then give them back to Unit for re-bagging. Too much work just for what should be a simple task. I wished that someone could help me.

â€śMay I help you?â€ť Another gentleman came smiling. He unpacked each bag, called over a guy who was an expert in halving bags of sugar, and then re-packed it in a bag and handed it over to me

â€śmap, people call me map. People in the Haskell store add an extra f before my name. They call me fmap. Funny people.â€ť

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

â€śTell me to do anything with what is inside the bag and I will do it. First by unpacking the bag and then repacking it with the new replacement.â€ťâ€Šâ€”â€Šmap

## Functor

The Bag along with me/map is known as Functor. Basically, the Bag is known as a Functor whenever 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 this for 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 class Bag[A](content: A) {
def map[B](f: A => B): Bag[B] = Bag(f(content))
}

case class Sugar(weight: Double)
// the guy who is expert at making sugar half
def half = (sugar: Sugar) => Sugar(sugar.weight / 2)
val sugarBag: 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 the above code? Unit in the 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.

``````def apply(content: A): Bag[A] = new Bag(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

``````def identity[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 1KG Bag of sugar and asked me to re-wrap it by calling identity to do his work on the sugar, then you should get a 1KG Bag of 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:

``````val sugarBag = 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 the equivalent to calling map with g composed with f (g after f or g(f(x))).

"If you give me a 1KG Bag of peanuts and ask me to roast them then 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 it back to you. This operation is equivalent to me 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.

• 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 the following:

``````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 law
val z = x
.map(_ + y)
.map(_ * m)
``````

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 1KG Bag of Sugar. Then I realized I needed 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 in doubling the sugar was so 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 me the bag.

Now I had a bag of 2KG sugar wrapped in another bag. I was a little irritated. If it was already packed in a bag why did map pack it again. Wouldn't it be better to pack the sugar only once?I didn't want to have to unpack two bags to use the 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 mistakes. My job is to unwrap the bag, give the stuff inside to someone and then repack whatever is given back to me by that person. I donâ€™t care if it is 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.

â€śGive me the double wrapped bag and I will flatten it. I will give you sugar wrapped only into one bagâ€ťâ€Šâ€”â€Šflatten

â€śGive me stuff doubly wrapped into a 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 more 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 a Monad is. 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 was someone who knew 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. I can do both the job of map and flatten.â€ť

The bag with unit and flatMap makes a Monad.

It was time to take out the 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 class Bag[A](content: A) {
def map[B](f: A => B): Bag[B] = Bag(f(content))

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

def flatten = content

}
def double = (sugar: Sugar) => Bag(Sugar(sugar.weight * 2))
val doubleSugarBag = sugarBag.map(sugar => double(sugar)).flatten
or
val doubleSugarBag = 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.

Sujit

See other articles by Sujit

Related jobs

See all

### WorksHub

CareersCompaniesSitemapFunctional WorksBlockchain WorksJavaScript WorksAI WorksGolang WorksJava WorksPython WorksRemote Works

### For companies

hello@works-hub.com

Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ

108 E 16th Street, New York, NY 10003