Monads are nothing more than a mechanism to sequence computations around values augmented with some additional feature. The concept of monads comes directly from mathematics, precisely from category theory (Cats).
Cats library helps to write purely functional code, is a quite young project in Scala. It brings many structures and functional constructs for you to use. The library itself is designed in a modular way, so we can use only the things that we really need. In this webinar, Muskan gupta will demonstrate the Monad concept and implement it using the cat's library.
Learn how to use Cats Libraries for writing abstraction or a pure functional code
The focus of the webinar will be:
~ What is Type class and how to use Implicits
~ What is Monad?
~ Introduction to Cats (Categories Theory)
~ Monad in Cats Library
~ Brief overview of Either Monad, Identity Monad, and, Eval Monad
1. Diving into Monads
in Cats Library
Presented By: Muskan Gupta
Software Consultant
Knoldus Inc.
2. About Knoldus
Knoldus is a technology consulting firm with focus on modernizing the digital systems
at the pace your business demands.
DevOps
Functional. Reactive. Cloud Native
3. 01 What is Monad
02 Introduction to Cats
03 Monads in Cats Library
04 Either Monad
05 Identity Monad
Our Agenda
05
06 Eval Monad
4. c
What is Monad?
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
5. c
What is Monad?
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
Monad has at least
two methods :
○ pure of type[A] =>
F[A]
This can be
considered as
constructor.
○ flatMap of type
(F[A], A => F[B])
=> F[B]
This is for
sequencing.
6. c
Monad Laws
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
pure and flatMap should follow following laws:
● Left Identity: calling pure and transforming the result with func is the
same as calling func:
pure(a).flatMap(func) == func(a)
● Right Identity: passing pure to flatMap is the same as doing nothing:
m.flatMap(pure) == m
● Associativity: flatMapping over two functions f and g is the same as
flatMapping over f and then flatMapping over g:
m.flatMap(f).flatMap(g) == m.flatMap(x => f(x).flatMap(g))
7. c
What is Cats?
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
● Cats is a library which provides abstractions for functional
programming in the Scala programming language.
● Most of the tools provided by Cats are implemented as type classes.
● Following are the imports:
○ For getting that type class - import cats.{typeClass Name}
○ For getting all instances - import.instances.all._
○ For getting all the syntax - import cats.syntax.all._
○ For getting all the instances and syntax together - import
cats.implicits._
8. c
Monads in Cats
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
● Monad is a simple type class that extends two type classes:
○ FlatMap to get flatMap method
○ Applicative for pure method
● Simple imports:
○ For using Monad in code : import cats.Monad
○ For using default instances in the code : import
cats.instances.all._
○ For getting monad syntax: import cats.syntax.flatMap._
import cats.synatx.map._
import cats.syntax.applicative._
9. c
Either Monad
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
Either in Scala Library
Either in Cats Library
10. c
Either in Cats vs Either in Scala
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
Issue with return type resolved
Right Biased
Extension Methods
11. c
ID Monad
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
According to book
In simple words :
ID monad provides you a monadic instance for non-monadic type.
For example:
12. c
Need of ID Monad
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
It let’s you abstract over monadic and non-monadic code.
Example:
Calling with Monadic Values
Calling with Non-Monadic Values
ID Monad to rescue
13. c
Eval Monad
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
Model of evaluation
● Eager: computations happen immediately.
● Lazy: computations happen on access.
● Memoized: computations happen on first access only and then the results are cached.
Eval monad has three subtypes:
● Now - It is eager and memoized.
14. c
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
● Always - It is lazy and non-memoized.
● Later - It is lazy and memoized.
15. c
Why Eval Monad?
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
● .map and flatMap method, helpful in chaining methods.
● memoize capability in chaining methods.
16. c
Why Eval Monad
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
● stack-safety of map and flatMap method and defer method
A simple factorial method:
● Eval.defer to rescue:
17. c
References
“Contrary to popular belief, Lorem
Ipsum is not simply random text.
Lorem Ipsum is not simply
random text. “
Norton
Bible-Designer
● Scala with Cats Book by Noel Welsh and Dave Gurnell
● Exploring Eval Monad in Cats
● Explore ID Monad in Cats
● Either in Cats vs Either in Scala