# functors, applicatives and monads in scala

Post on 07-Jan-2017

1.597 views

Embed Size (px)

TRANSCRIPT

Functors,Applicatives and Monads

Pallavi SinghSoftware ConsultantKnoldus Software LLP

Objectives:

Functors

Applicatives

Monads

Demo

Problem :

We have a simple value

We apply a function to it

Lets extend it , the value can be in a context,Now when we apply a functionto this value,we get results depending on the context.

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

FunctorsWhen a value is wrapped in a context, you cant apply a normal function to the value.We need a map. The map knows how to apply functions to values that are wrapped in a context.

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Functors

Definition : Functor is a type class. A Functor is any data type that defines how map applies to it.

Speaking in-formally you apply a function to a wrapped value using map. The map knows how to apply the function.

Functors

We have a Constructor C[_] and two types A and B,we want to apply functions of type C[A]=>C[B], so we need adequate transformations

( A=>B ) => ( C[A]=>C[B] )

And we need to define a map

def map[A,B](A=>B):(F[A]=>F[B] )

Functors

Example: Options , Streams

Object OptionFunctor extends Functor[Option] { def map[A, B](f: A B): Option[A] Option[B] = option option map f }

Problem :Given a function

what happens when you apply a function to another function , we have another function.

Functions are functors too ! A map on a function is function composition.

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Problem :

We have a value wrapped in context

And functions are wrapped in a context too!

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

ApplicativesWhen a value and function both are wrapped in a context. We cant apply it as we apply a simple function. We need an apply. It knows how to apply a function wrapped in a context to a value wrapped in a context.

56rt67

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Applicatives

Def : Applicative is typeclass. Applicative is any data type that defines how apply applies to it.

Apply takes a functor that has a function in it and another functor and extracts that function from the first functor and then maps it over the second one.

Speaking in-formally you apply a function wrapped in context to a value wrapped in context.

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Applicatives

We have a Constructor C[_] and two types A and B, we want to apply functions of type C[A]=>C[B],so we need adequate transformations

(C[A=>B] ) => ( C[A]=>C[B] )

And we need to define a apply

def apply[A,B](F[A=>B]):(F[A]=>F[B] )

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Problem :Given a function

what happens if we feed it awrapped value?

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Monads

Monads apply a function that returns a wrapped value to a wrapped value.

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Image Source : http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

Monads

Definition: Monad is a type class. A monad is a data type that implements the flatMap.

Speaking in-formally you apply a function that returns a wrapped value, to a wrapped value.

Monads

We have a Constructor C[_] and two types A and B,we want to apply functions of type C[A]=>C[B],so we need adequate transformations

( A=>C[B] ) => ( C[A]=>C[B] )

And we need to define a flatMap

def flatMap[A,B](A=>F[B]):(F[A]=>F[B] )

Monads

Example: List , Set , Option and Future all are Monads

Future is a wrapper over some asynchronous operation. Once the future has been completed you can do whatever it is you need to do with its result.

Difference b/w Monad and Monoids ?

Monoid : Given a type T, a binary operation Op:(T,T)=>T and instance Zero:T then the triple(T, Op , Zero) is called a Monoid if it has the following properties: Neutral Element and Associativity.

Monad instance simply wraps the value of type A within the given context and expose a certain set of methods to operate on them,

Monoid instance already knows how to combine these values of type A within the given context.

Are Monads powerful than Applicatives?

Applicatives and monads both model running computations in sequence. But Monads are more powerful because with applicatives you can sequence the computations, but monads allow you to sequence computations with the additional property that the result of subsequent computations can depend on the result of previous computation.

Demo

https://github.com/knoldus/functors-applicatives-monads

Questions

References

http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html

http://www.smartjava.org/content/scalaz-features-everyday-usage-part-1-typeclasses-and-scala-extensions

http://eed3si9n.com/learning-scalaz/Applicative.html

https://thedet.wordpress.com/2013/02/11/functors-in-images/

https://thedet.wordpress.com/2012/05/20/functors-monads-applicatives-playing-with-map-functor/

Thank You

Recommended