31
Humans

Thesis PPT

Embed Size (px)

Citation preview

Page 1: Thesis PPT

Monads for Humans

Page 2: Thesis PPT

Paradigm Blend

Knowledge Gap for Devs

Page 3: Thesis PPT

Those interested in Monads have no good starting places

Category Theory

Page 4: Thesis PPT

Functional abstractions are patterns

Origin in math

Page 5: Thesis PPT

What’s a Monad?

A Monad is just a Monoid in the category of endofunctors

Page 6: Thesis PPT

Monads are not

metaphors

Page 7: Thesis PPT

Functors

From the ground up

Page 8: Thesis PPT

Functors are

Type Constructors - F[A]

containercontainerss

Page 9: Thesis PPT

Functors need to have a map function

You did take PLP with Wallingford, right?

MaMapp

Page 10: Thesis PPT

def map[A,B](cont: List[A], f: A => B)

Apply the function f to each elementHigher Order Higher Order

FunctionsFunctionsHigher Order Higher Order

FunctionsFunctions

Page 11: Thesis PPT

Functors need a map method

Functors are containers

Lift arity-1 function to a function that works on computational context

Page 12: Thesis PPT

Applicative Functors

currying functions is fun

Applicative Applicative FunctorsFunctors

Page 13: Thesis PPT

Lifts an n-arity function to work on computational context through currying.

x => (y => x + y)

Currying?

Page 14: Thesis PPT

Applicative is just a more generalized Functor

Page 15: Thesis PPT

pure(+) <*> pure(3) <*> pure(4)

Applicative Style

Page 16: Thesis PPT

Functors lift 1-arity

Applic lift n-arity

RecaRecap:p:

Page 17: Thesis PPT

a distant cousinMonoidMonoid

Page 18: Thesis PPT

Type-class which have values that can be

Binary operation must be associative

combinecombinedd

Page 19: Thesis PPT

    object StringMonoid {        def mAppend(x: String, y: String): String = x + y        def mEmpty: String = ""      }

Page 20: Thesis PPT

MonadMonadThe Main Attraction

Page 21: Thesis PPT

Structure computations

Compose functions of different types that can be logically combined

Page 22: Thesis PPT

Monads in a purely functional language (Haskell)

Monads in Scala

Page 23: Thesis PPT

case class Identity[A](value: A) { def map[B](f: A => B) = Identity(f(value)) def flatMap[B](f: A => Identity[B]) (value) def unit(a: A) = Identity(a)}

Page 24: Thesis PPT

object Monoid {    def append(a1: List[A], a2: List[A]) = a1 ::: a2    def empty = Nil  }

Page 25: Thesis PPT

case class Writer[Log, A](log: Log, value: A) {  def map[B](f: A => B) =  Writer(log, f(value))  def flatMap[B](f: A => Writer[Log, B])(m: Monoid[Log]) = {    val x: Writer[Log,B] = f(value)    Writer(m.append(log, x.log), x.value)  }  def unit[Log, A](value: A)(m: Monoid[Log]) =    Writer(m.empty, value)}

Page 26: Thesis PPT

 def main(args: Array[String]) {    val start = 3    val finalWriter =      for(a <- addOne(start);          b <- intString(a);          c <- lengthGreaterThan5(b);          d <- noLog(oneOrZero(c));          e <- squareOf(d)         ) yield e  }

“Adding one to 3. Changing 4 to a String.Checking if length is greater than 5.Squaring 1.”

Page 27: Thesis PPT

FIN?

Page 28: Thesis PPT

Monads and Scala

For - Comprehensions

Page 29: Thesis PPT

Scala has Monads everywhere!

Scala recognizes monadic code.

Page 30: Thesis PPT

val first = List(1)val second = List(4)val third = List(8)

for {  x <- first  y <- second  z <- third}yield ( x + y + z)

first flatMap {  x => second flatMap {    y=> third map {      z => x + y + z    )  ))

Page 31: Thesis PPT

case class Transaction(memberInfo: Option[MemberInfo], id: String)case class MemberInfo(privateInfo: Option[PrivateMember], birthdate: String)case class PrivateInfo(socialSecurityNumber: String)

val ssNumber = “389-39-2983”val priv = PrivateInfo(ssNumber)val member = MemberInfo(priv, “10-20-87”)val optionalTransaction = Transaction(member, “28948”)

for {  transaction <- optionalTransaction  memberInfo <- transaction.memberInfo  privInformation <- memberInfo.privateInfo}yield privInformation.socialSecurityNumber