MONAD
FUNCTIONAL DESIGN PATTERN
What is Monad ?
 A Monad is not a class or a trait; it is a concept.
 A Monad is an object that wraps another object in Scala, like wrapping a present.
 In Monads, the output of a calculation at any step is the input to other calculations, which run as a
parent to the current step.
 We wrap gifts with shiny paper because they look pretty. We wrap objects with monads because
monads provide us with the following two operations:
 identity (unit in Scala)
 bind (flatMap in Scala)
Identity - Unit ()
 “Give me anything and I will hand it over to you wrapped in a bag. That’s my job.”—Unit
 In case of our monad trait, if we give it a value of type A, it will return a value of type M[A].
 In Scala, Unit is usually the apply method, a special method that doesn’t need to be named. A.apply()
is the same as A() which is the same as A{}.
Bind – flatMap()
 “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
 “Give me stuff doubly wrapped into bag and I will
return you the same stuff but with a single
wrapping”—flatten
 The bag with unit, map and flatten makes
a Monad.
 Monad!. It’s like a Functor who knows how
to flatten.
Monad Laws
 Left-identity law unit(x).flatMap(f) == f(x)
 Right-identity law Monad[X].flatMap(unit) == Monad[X]
 Associativity law m.flatMap(f).flatMap(g) == m.flatMap(x ⇒ f(x).flatMap(g))
Monads
 List
 Option
 Future
 Try
class mPackaging{
def flatMapOp(firstList : List[String], secondList : List[String] ):
List[String]= {
firstList.flatMap { x => secondList
.map { y => x + y }
}
}
}
object mPackaging{
def main(args: Array[String]): Unit = {
val monad = List("unit","bind")
val gift = List("packaging","opaeration")
println(apply.flatMapOp(monad, gift))
}
def apply: mPackaging = new mPackaging()
}
References
 https://medium.com/beingprofessional/understanding-functor-and-
monad-with-a-bag-of-peanuts-8fa702b3f69e
 https://medium.freecodecamp.org/demystifying-the-monad-in-scala-
cc716bb6f534

Monad

  • 1.
  • 2.
    What is Monad?  A Monad is not a class or a trait; it is a concept.  A Monad is an object that wraps another object in Scala, like wrapping a present.  In Monads, the output of a calculation at any step is the input to other calculations, which run as a parent to the current step.  We wrap gifts with shiny paper because they look pretty. We wrap objects with monads because monads provide us with the following two operations:  identity (unit in Scala)  bind (flatMap in Scala)
  • 3.
    Identity - Unit()  “Give me anything and I will hand it over to you wrapped in a bag. That’s my job.”—Unit  In case of our monad trait, if we give it a value of type A, it will return a value of type M[A].  In Scala, Unit is usually the apply method, a special method that doesn’t need to be named. A.apply() is the same as A() which is the same as A{}.
  • 4.
    Bind – flatMap() “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  “Give me stuff doubly wrapped into bag and I will return you the same stuff but with a single wrapping”—flatten  The bag with unit, map and flatten makes a Monad.  Monad!. It’s like a Functor who knows how to flatten.
  • 5.
    Monad Laws  Left-identitylaw unit(x).flatMap(f) == f(x)  Right-identity law Monad[X].flatMap(unit) == Monad[X]  Associativity law m.flatMap(f).flatMap(g) == m.flatMap(x ⇒ f(x).flatMap(g))
  • 6.
    Monads  List  Option Future  Try class mPackaging{ def flatMapOp(firstList : List[String], secondList : List[String] ): List[String]= { firstList.flatMap { x => secondList .map { y => x + y } } } } object mPackaging{ def main(args: Array[String]): Unit = { val monad = List("unit","bind") val gift = List("packaging","opaeration") println(apply.flatMapOp(monad, gift)) } def apply: mPackaging = new mPackaging() }
  • 7.