SlideShare a Scribd company logo
1 of 53
Monads

              Part 2
functional programming with scala
Read this
Download it…
Scala is..
Choose your…

•   DSL
•   Actors
•   OOP
•   Procedures
•   Functional
public class VersionFormatter {
  private final String comp;

    public VersionFormatter(String comp) {
      this.comp = comp;
    }

    public String generate(String app) {
      return ”{" + comp + " - " + app + “}";
    }
}

VersionFormatter myCompFormat = new VersionFormatter("Co");
System.out.println(myCompFormat.generate("App"));
case class VersionFormatter(val comp: String) {
  def generateFor(app: String): String = ”{" + comp + " - " + app + “}"
}

val myCompFormat = VersionFormatter("Co")
println(myCompFormat generateFor "App")
def generate(comp: String, app: String) = ”{%s - %s}".format(comp, app)

val myCompFormat = generate ("Co", _: String)
println(myCompFormat("App"))
Examples
return output filterById map toJson

ls | grep '^d'
val service = {
 path("orders") {
  authenticate(httpBasic(realm = "admin area")) { user =>
    get {
     cacheResults(LruCache(maxCapacity = 1000, timeToIdle =
Some(30.minutes))) {
       encodeResponse(Deflate) {
         completeWith {
           // marshal custom object with in-scope         //   marshaller
           getOrdersFromDB
         }
       }
     }
    }~
var avg: Option[Float] =
  from(grades)(g =>
    where(g.subjectId === mathId)
    compute(avg(
      g.scoreInPercentage))
)
“For” but not for…

val ns = List(10, 20)
val qs = for (n <- ns) yield n * 2
assert (qs == List(20, 40))
Map like for
val ns = List(10, 20)



val qs = ns map {n => n * 2}
For like map

for (x <- expr) yield resultExpr
                   =
expr map {x => resultExpr}
                   =
expr flatMap {x => unit(resultExpr)}
for [each] n [in] ns [and each] o [in] os
               yield n * o
val in1 = List(1, 2)
val in2 = List (3, 4)
val out = for (n <- in1; o <- in2) yield n + o
assert (out == List (1+3, 1+4, 2+3, 2+4))
                           =
val out2 = in1 flatMap {n =>
 in2 map {o => n + o }}
assert(out == out2)
Step by step
val out = ns flatMap {n => os map {o => n + o }}

val qs = ns flatMap {n => List(n + 3, n + 4)}

val qs = List(1 + 3, 1 + 4, 2 + 3, 2 + 4)
More of For…
val out = for (n <- in1; o <- in2; p <- in3)
    yield n + o + p

val qs = ns flatMap {n =>
 os flatMap {o =>
    {ps map {p => n + o + p}}}}
rule is recursive
For (x1 <- expr1;...x <- expr)
 yield resultExpr

                          =
expr1 flatMap {x1 =>
  for(...;x <- expr) yield resultExpr
}
Imperative "For"
Just loosing yield:

val ns = List(1, 2)
val os = List (3, 4)
for (n <- ns; o <- os) println(n + o)
                          =
ns foreach {n => os foreach {o => println(n + o) }}
Foreach is imperative form of "for"
class M[A] {
  def map[B](f: A=> B) : M[B] = ...
  def flatMap[B](f: A => M[B]) : M[B] = ...
  def foreach[B](f: A=> B) : Unit = {
    map(f)
    return ()
  }
}
Filtering
val names = List ( “Mike”, “Raph”, “Don”, “Leo” )
val eNames = for (eName <- names;
          if eName.contains ( ‘e’ )
) yield eName + “ is a name contains e”


assert(eNames == List(
 "Mike is a name contains e",
 "Leo is a name contains e"))
Filtering with map
val bNames =
  (names filter { eName => eName.contains('e') })
   .map { eName => eName + " is a name
contains e"
}
Basis 2
Haskell                        Scala
do var1<- expn1                for {var1 <- expn1;
 var2 <- expn2                   var2 <- expn2;
 expn3                           result <- expn3
                               } yield result
do var1 <- expn1               for {var1 <- expn1;
 var2 <- expn2                   var2 <- expn2;
 return expn3                  } yield expn3
do var1 <- expn1 >> expn2      for {_ <- expn1;
 return expn3                    var1 <- expn2
                               } yield expn3
Setting the Law

     f(x) ≡ g(x)

But no eq, ==, hashCode, ref
All the laws I present implicitly assume that
there are no side effects.
Breaking the law
Mathematic example:
    a*1≡a
    a*b≡b*a
    (a * b) * c ≡ a * (b * c)
WTF - What The Functor?
In Scala a functor is a class with a map method
and a few simple properties.
   class M[A] {
       def map[B](f: A => B):M[B] = ...
   }
First Functor Law: Identity
  def identity[A](x:A) = x
  identity(x) ≡ x

So here's our first functor law: for any functor m
   – F1. m map identity ≡ m // or equivalently *
   – F1b. m map {x => identity(x)} ≡ m // or
     equivalently
   – F1c. m map {x => x} ≡ m
Always must be true

– F1d. for (x <- m) yield x ≡ m
Second Functor Law: Composition

 F2. m map g map f ≡ m map {x => f(g(x))}
Always must work
val result1 = m map (f compose g)
val temp = m map g
val result2 = temp map f
assert result1 == result2
For analogue

F2b. for (y<- (for (x <-m) yield g(x)) yield f(y) ≡
       for (x <- m) yield f(g(x))
Functors and Monads
All Monads are Functors

class M[A] {
   def map[B](f: A => B):M[B] = ...
   def flatMap[B](f: A=> M[B]): M[B] = ...
   def unit[A](x:A):M[A] = …
}
Scala way for unit Object apply().
The Functor/Monad Connection Law
FM1. m map f ≡ m flatMap {x => unit(f(x))}

    Three concepts: unit, map, and flatMap.

FM1a. for (x <- m) yield f(x) ≡ for (x <- m; y <-
unit(f(x))) yield y
Flatten in details
FL1. m flatMap f ≡ flatten(m map f)
                         =
1. flatten(m map identity) ≡ m flatMap identity
   // substitute identity for f
2. FL1a. flatten(m) ≡ m flatMap identity // by F1
The First Monad Law: Identity
1. M1. m flatMap unit ≡ m // or equivalently
2. M1a. m flatMap {x => unit(x)} ≡ m

Where the connector law connected 3 concepts,
this law focuses on the relationship between 2
of them
Identity
• m flatMap {x => unit(x)} ≡ m // M1a
• m flatMap {x => unit(identity(x))}≡ m //
  identity
• F1b. m map {x => identity(x)} ≡ m // by FM1
• M1c. for (x <- m; y <- unit(x)) yield y ≡ m
The Second Monad Law: Unit
• M2. unit(x) flatMap f ≡ f(x) // or equivalently
• M2a. unit(x) flatMap {y => f(y)} ≡ f(x)
• M2b. for (y <- unit(x); result <- f(y)) yield result
  ≡ f(x)

It's in precisely this sense that it's safe to say
that any monad is a type of container (but that
doesn't mean a monad is a collection!).
Rephrasing
• unit(x) map f ≡ unit(x) map f // no, really, it
  does!
• unit(x) map f ≡ unit(x) flatMap {y => unit(f(y))}
  // by FM1
• M2c. unit(x) map f ≡ unit(f(x)) // by M2a
• M2d. for (y <- unit(x)) yield f(y) ≡ unit(f(x))
The Third Monad Law: Composition
• M3. m flatMap g flatMap f ≡
      m flatMap {x => g(x) flatMap f} // or
equivalently
• M3a. m flatMap {x => g(x)} flatMap {y => f(y)} ≡
       m flatMap {x => g(x) flatMap {y => f(y) }}
Mind breaker
• M3b.
for (a <- m; b <- g(a); result <- f(b) ) yield result ≡
for (a <- m; result <-
          for(b < g(a); temp <- f(b) ) yield temp )
yield result
?
1. m map g map f ≡ m map g map f // is it?
2. m map g map f ≡ m flatMap {x => unit(g(x))} flatMap
   {y => unit(f(y))} // by FM1, twice
3. m map g map f ≡ m flatMap {x => unit(g(x)) flatMap {y
   => unit(f(y))}} // by M3a
4. m map g map f ≡ m flatMap {x => unit(g(x)) map {y =>
   f(y)}} // by FM1a
5. m map g map f ≡ m flatMap {x => unit(f(g(x))} // by
   M2c
6. F2. m map g map f ≡ m map {x => f(g(x))} // by FM1a
Monadic zeros
Nill for List
None for Option
The First Zero Law: Identity
MZ1. mzero flatMap f ≡ mzero

1. mzero map f ≡ mzero map f // identity
2. mzero map f ≡ mzero flatMap {x => unit(f(x))
   // by FM1
3. MZ1b. mzero map f ≡ mzero // by MZ1
Not enough zero
unit(null) map {x => "Nope, not empty enough
to be a zero"} ≡
unit("Nope, not empty enough to be a zero")
The Second Zero Law: M to Zero in
            Nothing Flat
MZ2. m flatMap {x => mzero} ≡ mzero

Anything to nothing is nothing
What is +
Type         Method
Int          +
List         :::
Option       orElse
The Third and Fourth Zero Laws: Plus
class M[A] {
  ...
  def plus(other:M[B >: A]): M[B] = ...
}

• MZ3. mzero plus m ≡ m
• MZ4. m plus mzero ≡ m
Filtering Again
class M[A] {
  def map[B](f: A => B):M[B] = ...
  def flatMap[B](f: A=> M[B]): M[B] = ...
  def filter(p: A=> Boolean): M[A] = ...
}
Filter law
FIL1. m filter p ≡
   m flatMap {x => if(p(x)) unit(x) else mzero}
Filter results 1
• m filter {x => true} ≡
   m filter {x => true} // identity
• m filter {x => true} ≡
   m flatMap {x => if (true) unit(x) else mzero}
   // by FIL1
• m filter {x => true} ≡
   m flatMap {x => unit(x)} // by definition of if
• FIL1a. m filter {x => true} ≡ m // by M1
Filter results 2
• m filter {x => false} ≡ m filter {x => false}
   // identity
• m filter {x => false} ≡ m flatMap {x => if (false)
  unit(x) else mzero} // by FIL1
• m filter {x => false} ≡ m flatMap {x => mzero}
  // by definition of if
• FIL1b. m filter {x => false} ≡ mzero // by MZ1
Side Effects
m map g map f ≡ m map {x => (f(g(x)) }
Basis 3
       Scala                            Haskell
FM1    m map f ≡ m flatMap {x =>        fmap f m ≡ m >>= x -> return (f x)
       unit(f(x))}
M1     m flatMap unit ≡ m               m >>= return ≡ m
M2     unit(x) flatMap f ≡ f(x)         (return x) >>= f ≡ f x
M3     m flatMap g flatMap f ≡ m        (m >>= f) >>= g ≡ m >>= (x -> f x >>= g)
       flatMap {x => g(x) flatMap f}
MZ1    mzero flatMap f ≡ mzero          mzero >>= f ≡ mzero
MZ2    m flatMap {x => mzero} ≡ mzero   m >>= (x -> mzero) ≡ mzero
MZ3    mzero plus m ≡ m                 mzero 'mplus' m ≡ m
MZ4    m plus mzero ≡ m                 m 'mplus' mzero ≡ m
FIL1   m filter p ≡ m flatMap {x =>     mfilter p m ≡ m >>= (x -> if p x then
       if(p(x)) unit(x) else mzero}     return x else mzero)

More Related Content

What's hot

Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsPhilip Schwarz
 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsPhilip Schwarz
 
Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Philip Schwarz
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverseLuka Jacobowitz
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2Hang Zhao
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional ProgrammingLuka Jacobowitz
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsPhilip Schwarz
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Philip Schwarz
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adtsHang Zhao
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!priort
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator PatternEric Torreborre
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoidsLuka Jacobowitz
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Luka Jacobowitz
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskellLuca Molteni
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskellJongsoo Lee
 
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...Philip Schwarz
 

What's hot (20)

Abstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generatorsAbstracting over the Monad yielded by a for comprehension and its generators
Abstracting over the Monad yielded by a for comprehension and its generators
 
Monoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and CatsMonoids - Part 2 - with examples using Scalaz and Cats
Monoids - Part 2 - with examples using Scalaz and Cats
 
Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...Scala collection methods flatMap and flatten are more powerful than monadic f...
Scala collection methods flatMap and flatten are more powerful than monadic f...
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
 
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
Quicksort - a whistle-stop tour of the algorithm in five languages and four p...
 
Fp in scala with adts
Fp in scala with adtsFp in scala with adts
Fp in scala with adts
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
Monoids, monoids, monoids
Monoids, monoids, monoidsMonoids, monoids, monoids
Monoids, monoids, monoids
 
Functor Composition
Functor CompositionFunctor Composition
Functor Composition
 
Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020Monoids, Monoids, Monoids - ScalaLove 2020
Monoids, Monoids, Monoids - ScalaLove 2020
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
Humble introduction to category theory in haskell
Humble introduction to category theory in haskellHumble introduction to category theory in haskell
Humble introduction to category theory in haskell
 
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
 
Functor Laws
Functor LawsFunctor Laws
Functor Laws
 

Viewers also liked

Why functional why scala
Why functional  why scala Why functional  why scala
Why functional why scala Neville Li
 
Scala the language matters
Scala the language mattersScala the language matters
Scala the language mattersXiaojun REN
 
Introduction to Option monad in Scala
Introduction to Option monad in ScalaIntroduction to Option monad in Scala
Introduction to Option monad in ScalaJan Krag
 
Thinking functional-in-scala
Thinking functional-in-scalaThinking functional-in-scala
Thinking functional-in-scalaKnoldus Inc.
 
Developers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala MonadDevelopers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala MonadSangwon Han
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in ScalaPatrick Nicolas
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar Hyder
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 

Viewers also liked (9)

Why functional why scala
Why functional  why scala Why functional  why scala
Why functional why scala
 
Scala the language matters
Scala the language mattersScala the language matters
Scala the language matters
 
Introduction to Option monad in Scala
Introduction to Option monad in ScalaIntroduction to Option monad in Scala
Introduction to Option monad in Scala
 
Thinking functional-in-scala
Thinking functional-in-scalaThinking functional-in-scala
Thinking functional-in-scala
 
Developers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala MonadDevelopers Summit 2015 - Scala Monad
Developers Summit 2015 - Scala Monad
 
Advanced Functional Programming in Scala
Advanced Functional Programming in ScalaAdvanced Functional Programming in Scala
Advanced Functional Programming in Scala
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 

Similar to Introduction to Monads in Scala (2)

functions limits and continuity
functions limits and continuityfunctions limits and continuity
functions limits and continuityPume Ananda
 
Math - Operations on Functions, Kinds of Functions
Math - Operations on Functions, Kinds of FunctionsMath - Operations on Functions, Kinds of Functions
Math - Operations on Functions, Kinds of FunctionsChuckie Balbuena
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- BasicsRabin BK
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
Number Theory for Security
Number Theory for SecurityNumber Theory for Security
Number Theory for SecurityAbhijit Mondal
 
Intro to Matlab programming
Intro to Matlab programmingIntro to Matlab programming
Intro to Matlab programmingAhmed Moawad
 
Functions for Grade 10
Functions for Grade 10Functions for Grade 10
Functions for Grade 10Boipelo Radebe
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to MonadsLawrence Evans
 
S1 3 derivadas_resueltas
S1 3 derivadas_resueltasS1 3 derivadas_resueltas
S1 3 derivadas_resueltasjesquerrev1
 
100derivadasresueltasyosoytuprofe
100derivadasresueltasyosoytuprofe100derivadasresueltasyosoytuprofe
100derivadasresueltasyosoytuprofeJavier Rangel
 
Operation on Functions.pptx
Operation on Functions.pptxOperation on Functions.pptx
Operation on Functions.pptxAPHRODITE51
 

Similar to Introduction to Monads in Scala (2) (20)

functions limits and continuity
functions limits and continuityfunctions limits and continuity
functions limits and continuity
 
Functions limits and continuity
Functions limits and continuityFunctions limits and continuity
Functions limits and continuity
 
Matlab differential
Matlab differentialMatlab differential
Matlab differential
 
Math - Operations on Functions, Kinds of Functions
Math - Operations on Functions, Kinds of FunctionsMath - Operations on Functions, Kinds of Functions
Math - Operations on Functions, Kinds of Functions
 
Calculus- Basics
Calculus- BasicsCalculus- Basics
Calculus- Basics
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
Number Theory for Security
Number Theory for SecurityNumber Theory for Security
Number Theory for Security
 
Intro to Matlab programming
Intro to Matlab programmingIntro to Matlab programming
Intro to Matlab programming
 
Function
FunctionFunction
Function
 
Functions for Grade 10
Functions for Grade 10Functions for Grade 10
Functions for Grade 10
 
Integration
IntegrationIntegration
Integration
 
Derivatives
DerivativesDerivatives
Derivatives
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
Chap05alg
Chap05algChap05alg
Chap05alg
 
S1 3 derivadas_resueltas
S1 3 derivadas_resueltasS1 3 derivadas_resueltas
S1 3 derivadas_resueltas
 
100derivadasresueltasyosoytuprofe
100derivadasresueltasyosoytuprofe100derivadasresueltasyosoytuprofe
100derivadasresueltasyosoytuprofe
 
Operation on Functions.pptx
Operation on Functions.pptxOperation on Functions.pptx
Operation on Functions.pptx
 
2020 preTEST4A
2020 preTEST4A2020 preTEST4A
2020 preTEST4A
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 

Recently uploaded

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Introduction to Monads in Scala (2)

  • 1. Monads Part 2 functional programming with scala
  • 5. Choose your… • DSL • Actors • OOP • Procedures • Functional
  • 6. public class VersionFormatter { private final String comp; public VersionFormatter(String comp) { this.comp = comp; } public String generate(String app) { return ”{" + comp + " - " + app + “}"; } } VersionFormatter myCompFormat = new VersionFormatter("Co"); System.out.println(myCompFormat.generate("App"));
  • 7. case class VersionFormatter(val comp: String) { def generateFor(app: String): String = ”{" + comp + " - " + app + “}" } val myCompFormat = VersionFormatter("Co") println(myCompFormat generateFor "App")
  • 8. def generate(comp: String, app: String) = ”{%s - %s}".format(comp, app) val myCompFormat = generate ("Co", _: String) println(myCompFormat("App"))
  • 9. Examples return output filterById map toJson ls | grep '^d'
  • 10. val service = { path("orders") { authenticate(httpBasic(realm = "admin area")) { user => get { cacheResults(LruCache(maxCapacity = 1000, timeToIdle = Some(30.minutes))) { encodeResponse(Deflate) { completeWith { // marshal custom object with in-scope // marshaller getOrdersFromDB } } } }~
  • 11. var avg: Option[Float] = from(grades)(g => where(g.subjectId === mathId) compute(avg( g.scoreInPercentage)) )
  • 12. “For” but not for… val ns = List(10, 20) val qs = for (n <- ns) yield n * 2 assert (qs == List(20, 40))
  • 13. Map like for val ns = List(10, 20) val qs = ns map {n => n * 2}
  • 14. For like map for (x <- expr) yield resultExpr = expr map {x => resultExpr} = expr flatMap {x => unit(resultExpr)}
  • 15. for [each] n [in] ns [and each] o [in] os yield n * o val in1 = List(1, 2) val in2 = List (3, 4) val out = for (n <- in1; o <- in2) yield n + o assert (out == List (1+3, 1+4, 2+3, 2+4)) = val out2 = in1 flatMap {n => in2 map {o => n + o }} assert(out == out2)
  • 16. Step by step val out = ns flatMap {n => os map {o => n + o }} val qs = ns flatMap {n => List(n + 3, n + 4)} val qs = List(1 + 3, 1 + 4, 2 + 3, 2 + 4)
  • 17. More of For… val out = for (n <- in1; o <- in2; p <- in3) yield n + o + p val qs = ns flatMap {n => os flatMap {o => {ps map {p => n + o + p}}}}
  • 18. rule is recursive For (x1 <- expr1;...x <- expr) yield resultExpr = expr1 flatMap {x1 => for(...;x <- expr) yield resultExpr }
  • 19. Imperative "For" Just loosing yield: val ns = List(1, 2) val os = List (3, 4) for (n <- ns; o <- os) println(n + o) = ns foreach {n => os foreach {o => println(n + o) }}
  • 20. Foreach is imperative form of "for" class M[A] { def map[B](f: A=> B) : M[B] = ... def flatMap[B](f: A => M[B]) : M[B] = ... def foreach[B](f: A=> B) : Unit = { map(f) return () } }
  • 21. Filtering val names = List ( “Mike”, “Raph”, “Don”, “Leo” ) val eNames = for (eName <- names; if eName.contains ( ‘e’ ) ) yield eName + “ is a name contains e” assert(eNames == List( "Mike is a name contains e", "Leo is a name contains e"))
  • 22. Filtering with map val bNames = (names filter { eName => eName.contains('e') }) .map { eName => eName + " is a name contains e" }
  • 23. Basis 2 Haskell Scala do var1<- expn1 for {var1 <- expn1; var2 <- expn2 var2 <- expn2; expn3 result <- expn3 } yield result do var1 <- expn1 for {var1 <- expn1; var2 <- expn2 var2 <- expn2; return expn3 } yield expn3 do var1 <- expn1 >> expn2 for {_ <- expn1; return expn3 var1 <- expn2 } yield expn3
  • 24. Setting the Law f(x) ≡ g(x) But no eq, ==, hashCode, ref All the laws I present implicitly assume that there are no side effects.
  • 25. Breaking the law Mathematic example: a*1≡a a*b≡b*a (a * b) * c ≡ a * (b * c)
  • 26. WTF - What The Functor? In Scala a functor is a class with a map method and a few simple properties. class M[A] { def map[B](f: A => B):M[B] = ... }
  • 27. First Functor Law: Identity def identity[A](x:A) = x identity(x) ≡ x So here's our first functor law: for any functor m – F1. m map identity ≡ m // or equivalently * – F1b. m map {x => identity(x)} ≡ m // or equivalently – F1c. m map {x => x} ≡ m
  • 28. Always must be true – F1d. for (x <- m) yield x ≡ m
  • 29. Second Functor Law: Composition F2. m map g map f ≡ m map {x => f(g(x))}
  • 30. Always must work val result1 = m map (f compose g) val temp = m map g val result2 = temp map f assert result1 == result2
  • 31. For analogue F2b. for (y<- (for (x <-m) yield g(x)) yield f(y) ≡ for (x <- m) yield f(g(x))
  • 32. Functors and Monads All Monads are Functors class M[A] { def map[B](f: A => B):M[B] = ... def flatMap[B](f: A=> M[B]): M[B] = ... def unit[A](x:A):M[A] = … } Scala way for unit Object apply().
  • 33. The Functor/Monad Connection Law FM1. m map f ≡ m flatMap {x => unit(f(x))} Three concepts: unit, map, and flatMap. FM1a. for (x <- m) yield f(x) ≡ for (x <- m; y <- unit(f(x))) yield y
  • 34. Flatten in details FL1. m flatMap f ≡ flatten(m map f) = 1. flatten(m map identity) ≡ m flatMap identity // substitute identity for f 2. FL1a. flatten(m) ≡ m flatMap identity // by F1
  • 35. The First Monad Law: Identity 1. M1. m flatMap unit ≡ m // or equivalently 2. M1a. m flatMap {x => unit(x)} ≡ m Where the connector law connected 3 concepts, this law focuses on the relationship between 2 of them
  • 36. Identity • m flatMap {x => unit(x)} ≡ m // M1a • m flatMap {x => unit(identity(x))}≡ m // identity • F1b. m map {x => identity(x)} ≡ m // by FM1 • M1c. for (x <- m; y <- unit(x)) yield y ≡ m
  • 37. The Second Monad Law: Unit • M2. unit(x) flatMap f ≡ f(x) // or equivalently • M2a. unit(x) flatMap {y => f(y)} ≡ f(x) • M2b. for (y <- unit(x); result <- f(y)) yield result ≡ f(x) It's in precisely this sense that it's safe to say that any monad is a type of container (but that doesn't mean a monad is a collection!).
  • 38. Rephrasing • unit(x) map f ≡ unit(x) map f // no, really, it does! • unit(x) map f ≡ unit(x) flatMap {y => unit(f(y))} // by FM1 • M2c. unit(x) map f ≡ unit(f(x)) // by M2a • M2d. for (y <- unit(x)) yield f(y) ≡ unit(f(x))
  • 39. The Third Monad Law: Composition • M3. m flatMap g flatMap f ≡ m flatMap {x => g(x) flatMap f} // or equivalently • M3a. m flatMap {x => g(x)} flatMap {y => f(y)} ≡ m flatMap {x => g(x) flatMap {y => f(y) }}
  • 40. Mind breaker • M3b. for (a <- m; b <- g(a); result <- f(b) ) yield result ≡ for (a <- m; result <- for(b < g(a); temp <- f(b) ) yield temp ) yield result
  • 41. ? 1. m map g map f ≡ m map g map f // is it? 2. m map g map f ≡ m flatMap {x => unit(g(x))} flatMap {y => unit(f(y))} // by FM1, twice 3. m map g map f ≡ m flatMap {x => unit(g(x)) flatMap {y => unit(f(y))}} // by M3a 4. m map g map f ≡ m flatMap {x => unit(g(x)) map {y => f(y)}} // by FM1a 5. m map g map f ≡ m flatMap {x => unit(f(g(x))} // by M2c 6. F2. m map g map f ≡ m map {x => f(g(x))} // by FM1a
  • 42. Monadic zeros Nill for List None for Option
  • 43. The First Zero Law: Identity MZ1. mzero flatMap f ≡ mzero 1. mzero map f ≡ mzero map f // identity 2. mzero map f ≡ mzero flatMap {x => unit(f(x)) // by FM1 3. MZ1b. mzero map f ≡ mzero // by MZ1
  • 44. Not enough zero unit(null) map {x => "Nope, not empty enough to be a zero"} ≡ unit("Nope, not empty enough to be a zero")
  • 45. The Second Zero Law: M to Zero in Nothing Flat MZ2. m flatMap {x => mzero} ≡ mzero Anything to nothing is nothing
  • 46. What is + Type Method Int + List ::: Option orElse
  • 47. The Third and Fourth Zero Laws: Plus class M[A] { ... def plus(other:M[B >: A]): M[B] = ... } • MZ3. mzero plus m ≡ m • MZ4. m plus mzero ≡ m
  • 48. Filtering Again class M[A] { def map[B](f: A => B):M[B] = ... def flatMap[B](f: A=> M[B]): M[B] = ... def filter(p: A=> Boolean): M[A] = ... }
  • 49. Filter law FIL1. m filter p ≡ m flatMap {x => if(p(x)) unit(x) else mzero}
  • 50. Filter results 1 • m filter {x => true} ≡ m filter {x => true} // identity • m filter {x => true} ≡ m flatMap {x => if (true) unit(x) else mzero} // by FIL1 • m filter {x => true} ≡ m flatMap {x => unit(x)} // by definition of if • FIL1a. m filter {x => true} ≡ m // by M1
  • 51. Filter results 2 • m filter {x => false} ≡ m filter {x => false} // identity • m filter {x => false} ≡ m flatMap {x => if (false) unit(x) else mzero} // by FIL1 • m filter {x => false} ≡ m flatMap {x => mzero} // by definition of if • FIL1b. m filter {x => false} ≡ mzero // by MZ1
  • 52. Side Effects m map g map f ≡ m map {x => (f(g(x)) }
  • 53. Basis 3 Scala Haskell FM1 m map f ≡ m flatMap {x => fmap f m ≡ m >>= x -> return (f x) unit(f(x))} M1 m flatMap unit ≡ m m >>= return ≡ m M2 unit(x) flatMap f ≡ f(x) (return x) >>= f ≡ f x M3 m flatMap g flatMap f ≡ m (m >>= f) >>= g ≡ m >>= (x -> f x >>= g) flatMap {x => g(x) flatMap f} MZ1 mzero flatMap f ≡ mzero mzero >>= f ≡ mzero MZ2 m flatMap {x => mzero} ≡ mzero m >>= (x -> mzero) ≡ mzero MZ3 mzero plus m ≡ m mzero 'mplus' m ≡ m MZ4 m plus mzero ≡ m m 'mplus' mzero ≡ m FIL1 m filter p ≡ m flatMap {x => mfilter p m ≡ m >>= (x -> if p x then if(p(x)) unit(x) else mzero} return x else mzero)