SlideShare a Scribd company logo
Functors,Applicatives and MonadsFunctors,Applicatives and Monads
Pallavi Singh
Software Consultant
Knoldus Software LLP
Objectives:
 Functors
 Applicatives
 Monads
 Demo
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 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
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
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.
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] )
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⇒ ⇒ ⇒
}
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
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
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
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
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
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
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
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
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
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.
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.
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.
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
Demo
https://github.com/knoldus/functors-applicatives-monads
QuestionsQuestions
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/
Thank YouThank You

More Related Content

PDF
Applicative style programming
PDF
Exploring ZIO Prelude: The game changer for typeclasses in Scala
PPTX
Running Free with the Monads
PPTX
Java reflection
PDF
Beyond Scala Lens
PDF
Monadologie
PDF
Functor, Apply, Applicative And Monad
PDF
Sequence and Traverse - Part 1
Applicative style programming
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Running Free with the Monads
Java reflection
Beyond Scala Lens
Monadologie
Functor, Apply, Applicative And Monad
Sequence and Traverse - Part 1

What's hot (20)

PDF
Java 8 Lambda Expressions & Streams
PPTX
Taking your side effects aside
PPTX
Method Overloading in Java
PDF
Understanding Implicits in Scala
PPT
Scala functions
PPTX
Java Queue.pptx
PPSX
Collections - Lists, Sets
PPTX
Inline Functions and Default arguments
PDF
If You Think You Can Stay Away from Functional Programming, You Are Wrong
PPTX
ZIO: Powerful and Principled Functional Programming in Scala
PDF
Writing Parsers and Compilers with PLY
PDF
Collections In Java
PPTX
Constructor and Destructor
PPTX
Introduction to Spring Framework
 
PDF
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
PDF
Data Structures & Algorithm design using C
PDF
Functional programming
PDF
Function overloading ppt
PDF
Java IO
Java 8 Lambda Expressions & Streams
Taking your side effects aside
Method Overloading in Java
Understanding Implicits in Scala
Scala functions
Java Queue.pptx
Collections - Lists, Sets
Inline Functions and Default arguments
If You Think You Can Stay Away from Functional Programming, You Are Wrong
ZIO: Powerful and Principled Functional Programming in Scala
Writing Parsers and Compilers with PLY
Collections In Java
Constructor and Destructor
Introduction to Spring Framework
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
Data Structures & Algorithm design using C
Functional programming
Function overloading ppt
Java IO
Ad

Viewers also liked (20)

PDF
Domain-driven design
PDF
Go for the Money - JSR 354
PDF
Introduction to Option monad in Scala
ODP
Introduction to Scala JS
ODP
Drilling the Async Library
ODP
Akka streams
ODP
Getting Started With AureliaJs
ODP
String interpolation
ODP
Mailchimp and Mandrill - The ‘Hominidae’ kingdom
ODP
Realm Mobile Database - An Introduction
PDF
Kanban
ODP
Shapeless- Generic programming for Scala
ODP
An Introduction to Quill
ODP
Introduction to Scala Macros
ODP
Introduction to Java 8
ODP
Mandrill Templates
ODP
Introduction to Knockout Js
ODP
ANTLR4 and its testing
ODP
Effective way to code in Scala
ODP
Introduction to ScalaZ
Domain-driven design
Go for the Money - JSR 354
Introduction to Option monad in Scala
Introduction to Scala JS
Drilling the Async Library
Akka streams
Getting Started With AureliaJs
String interpolation
Mailchimp and Mandrill - The ‘Hominidae’ kingdom
Realm Mobile Database - An Introduction
Kanban
Shapeless- Generic programming for Scala
An Introduction to Quill
Introduction to Scala Macros
Introduction to Java 8
Mandrill Templates
Introduction to Knockout Js
ANTLR4 and its testing
Effective way to code in Scala
Introduction to ScalaZ
Ad

Similar to Functors, Applicatives and Monads In Scala (20)

PDF
C++ [ principles of object oriented programming ]
DOCX
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
PPTX
Presentation on c programing satcture
PPTX
Presentation on c structures
PDF
Functions2.pdf
PDF
How to start functional programming (in Scala): Day1
PDF
C++ interview question
PDF
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PDF
2-functions.pptx_20240619_085610_0000.pdf
PDF
C++
PPTX
cbse class 12 Python Functions2 for class 12 .pptx
PPTX
OOP-Advanced Programming with c++
PPT
Major terminologies in oop (Lect 2).ppt. Object oriented programming
PPT
C++ classes tutorials
DOCX
java tr.docx
PDF
Object Oriented Programming notes provided
PPTX
C++ tutorial assignment - 23MTS5730.pptx
PDF
How to build a react native app with the help of react native hooks
PDF
C basic questions&ansrs by shiva kumar kella
C++ [ principles of object oriented programming ]
JLK Chapter 5 – Methods and ModularityDRAFT January 2015 Edition.docx
Presentation on c programing satcture
Presentation on c structures
Functions2.pdf
How to start functional programming (in Scala): Day1
C++ interview question
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
CHAPTER 01 FUNCTION in python class 12th.pptx
2-functions.pptx_20240619_085610_0000.pdf
C++
cbse class 12 Python Functions2 for class 12 .pptx
OOP-Advanced Programming with c++
Major terminologies in oop (Lect 2).ppt. Object oriented programming
C++ classes tutorials
java tr.docx
Object Oriented Programming notes provided
C++ tutorial assignment - 23MTS5730.pptx
How to build a react native app with the help of react native hooks
C basic questions&ansrs by shiva kumar kella

More from Knoldus Inc. (20)

PPTX
Angular Hydration Presentation (FrontEnd)
PPTX
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
PPTX
Self-Healing Test Automation Framework - Healenium
PPTX
Kanban Metrics Presentation (Project Management)
PPTX
Java 17 features and implementation.pptx
PPTX
Chaos Mesh Introducing Chaos in Kubernetes
PPTX
GraalVM - A Step Ahead of JVM Presentation
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
Nomad by HashiCorp Presentation (DevOps)
PPTX
DAPR - Distributed Application Runtime Presentation
PPTX
Introduction to Azure Virtual WAN Presentation
PPTX
Introduction to Argo Rollouts Presentation
PPTX
Intro to Azure Container App Presentation
PPTX
Insights Unveiled Test Reporting and Observability Excellence
PPTX
Introduction to Splunk Presentation (DevOps)
PPTX
Code Camp - Data Profiling and Quality Analysis Framework
PPTX
AWS: Messaging Services in AWS Presentation
PPTX
Amazon Cognito: A Primer on Authentication and Authorization
PPTX
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
PPTX
Managing State & HTTP Requests In Ionic.
Angular Hydration Presentation (FrontEnd)
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Self-Healing Test Automation Framework - Healenium
Kanban Metrics Presentation (Project Management)
Java 17 features and implementation.pptx
Chaos Mesh Introducing Chaos in Kubernetes
GraalVM - A Step Ahead of JVM Presentation
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
DAPR - Distributed Application Runtime Presentation
Introduction to Azure Virtual WAN Presentation
Introduction to Argo Rollouts Presentation
Intro to Azure Container App Presentation
Insights Unveiled Test Reporting and Observability Excellence
Introduction to Splunk Presentation (DevOps)
Code Camp - Data Profiling and Quality Analysis Framework
AWS: Messaging Services in AWS Presentation
Amazon Cognito: A Primer on Authentication and Authorization
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Managing State & HTTP Requests In Ionic.

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PPT
JAVA ppt tutorial basics to learn java programming
PPTX
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
PPTX
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
PDF
How to Confidently Manage Project Budgets
DOCX
The Five Best AI Cover Tools in 2025.docx
PPTX
ai tools demonstartion for schools and inter college
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
PPTX
Introduction to Artificial Intelligence
PPTX
Benefits of DCCM for Genesys Contact Center
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
top salesforce developer skills in 2025.pdf
JAVA ppt tutorial basics to learn java programming
What to Capture When It Breaks: 16 Artifacts That Reveal Root Causes
CRUISE TICKETING SYSTEM | CRUISE RESERVATION SOFTWARE
How to Confidently Manage Project Budgets
The Five Best AI Cover Tools in 2025.docx
ai tools demonstartion for schools and inter college
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Most Effective Social Media Agency in Bangalore.pdf
Introduction to Artificial Intelligence
Benefits of DCCM for Genesys Contact Center
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
The Role of Automation and AI in EHS Management for Data Centers.pdf
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
ManageIQ - Sprint 268 Review - Slide Deck
Which alternative to Crystal Reports is best for small or large businesses.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Materi_Pemrograman_Komputer-Looping.pptx
Perfecting Gamer’s Experiences with Performance Testing for Gaming Applicatio...
Materi-Enum-and-Record-Data-Type (1).pptx

Functors, Applicatives and Monads In Scala

  • 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
  • 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.
  • 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/