What are Monad Transformers in Scala for?

My understanding is this:

Suppose that M1 and M2 are monads, that is, they provide unit and flatMap functions that comply with monadic laws. Unfortunately, we cannot create unit and flatMap for M1[M2] . It seems that unit and flatMap do not always exist for any M1 and M2 . I do not know if we need to prove it.

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

Do I understand this correctly?

+8
scala functional-programming monads monad-transformers
source share
2 answers

You're right. They are usually used in constructions to understand where you need to combine the properties of monads.

Monad transformers "step by step" here .

+3
source share

Eric Bakker talks about the monadic transformers β€œOptions in futures, how they suck” (available on Youtube). I found this so useful that I made a slide deck based on it: https://www.slideshare.net/pjschwarz/monad-transformers-part-1

enter image description here

+1
source share

All Articles