Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- Domain-driven design by Knoldus Inc. 3621 views
- Go for the Money - JSR 354 by Anatole Tresch 1311 views
- Introduction to Option monad in Scala by Jan Krag 7240 views
- Introduction to Scala JS by Knoldus Inc. 2965 views
- Drilling the Async Library by Knoldus Inc. 906 views
- Akka streams by Knoldus Inc. 2272 views

3,866 views

Published on

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

Published in:
Software

No Downloads

Total views

3,866

On SlideShare

0

From Embeds

0

Number of Embeds

1,460

Shares

0

Downloads

45

Comments

0

Likes

6

No embeds

No notes for slide

- 1. Functors,Applicatives and MonadsFunctors,Applicatives and Monads Pallavi Singh Software Consultant Knoldus Software LLP
- 2. Objectives: Functors Applicatives Monads Demo Objectives: Functors Applicatives Monads Demo
- 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] )
- 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. 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
- 15. 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.
- 21. Demo https://github.com/knoldus/functors-applicatives-monads Demo https://github.com/knoldus/functors-applicatives-monads
- 22. QuestionsQuestions
- 23. 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/ 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/
- 24. Thank YouThank You

No public clipboards found for this slide

Be the first to comment