Successfully reported this slideshow.
Upcoming SlideShare
×

# Functors, Applicatives and Monads In Scala

3,866 views

Published on

In this presentation we will be discussing about Functors, Applicatives and Monads in Scala.

Published in: Software
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Functors, Applicatives and Monads In Scala

1. 1. Functors,Applicatives and MonadsFunctors,Applicatives and Monads Pallavi Singh Software Consultant Knoldus Software LLP
2. 2. Objectives:  Functors  Applicatives  Monads  Demo Objectives:  Functors  Applicatives  Monads  Demo
3. 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. 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. 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. 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] )
7. 7. Functors Example: Options , Streams Object OptionFunctor extends Functor[Option] { def map[A, B](f: A B): Option[A] Option[B] = option option map f⇒ ⇒ ⇒ } Functors Example: Options , Streams Object OptionFunctor extends Functor[Option] { def map[A, B](f: A B): Option[A] Option[B] = option option map f⇒ ⇒ ⇒ }
8. 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. 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. 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. 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. 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. 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. 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