Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need Monad Transformers in Scala?

My understanding is the following:

Suppose M1 and M2 are monads, i.e. they provide functions unit and flatMap that comply to the monadic laws. Unfortunately we cannot create unit and flatMap for M1[M2] . It looks like the unit and flatMap don't always exist for any M1 and M2. I do not know if we need to prove it.

So the solution is to create a wrapper W for M1[M2], so that W[M1, M2] is a monad, and use it instead. This wrapper W is called Monad Transformer.

Do I understand it correctly ?

like image 446
Michael Avatar asked Mar 12 '14 12:03

Michael


1 Answers

You are right. Typically they are used in for-comprehension constructions where you need to combine monads properties.

Monad transformers "step-by-step" here.

like image 191
Yuriy Avatar answered Nov 15 '22 04:11

Yuriy