3. 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 function
to this value,we get results
depending on the context.
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 function
to 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
4. Functors
When a value is wrapped in a context, you
can’t 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.
Functors
When a value is wrapped in a context, you
can’t 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
5. 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
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.
6. 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
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] )
8. 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.
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
9. Problem :
We have a value wrapped in context
And functions are wrapped in a context too!
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
10. Applicatives
When a value and function both are wrapped in a
context. We can’t 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
Applicatives
When a value and function both are wrapped in a
context. We can’t 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
11. 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.
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
12. 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] )
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
13. Problem :
Given a function
what happens if we feed it a
wrapped value?
Problem :
Given a function
what happens if we feed it a
wrapped value?
Image Source :
http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html
14. Monads
Monads apply a
function that
returns a
wrapped value to
a wrapped value.
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
16. 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
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.
17. 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
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] )
18. 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.
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.
19. 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.
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.
20. 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.
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.