The Monad is one of the most common abstractions in Scala. We frequently use them in Scala and any other languages, but we often didn’t know them by name. While learning functional programming, besides understanding the rules of referential transparency and pure, developers usually get confused with what Monad, Functor, Semigroup, Monoid are.
Watch this session learn how you can create your Monad with the Cats library by implementing pure, flatMap, and tailRecM
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Mute
Please keep your window on mute.
Avoid Disturbance
Avoid leaving your window
unmuted after asking a question.
3. Our Agenda
What is Monad?
Introduction to Cats
Monad in Cats Library
Either Monad
Identity Monad
Eval Monad
5. What is Monad?
Image Placeholder Image Placeholder Image Placeholder
● 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. Monad Laws
Image Placeholder Image Placeholder Image Placeholder
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. What is Cats?
Image Placeholder Image Placeholder Image Placeholder
● 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. Monads in Cats
Image Placeholder Image Placeholder Image Placeholder
● 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._
10. Either in Cats over Either in Scala
Image Placeholder Image Placeholder Image Placeholder
● Issue with return type resolved.
● Right biased
● Extension methods
11. ID Monad
Image Placeholder Image Placeholder Image Placeholder
According to book:
In simple words :
ID monad provides you a monadic instance for non-monadic type. For example:
12. Need of ID Monad
Image Placeholder Image Placeholder
Example:
It let’s you abstract over monadic and non-monadic code.
Calling with monadic values:
Calling with non-monadic values:
ID Monad to rescue:
13. Eval Monad
Image Placeholder Image Placeholder Image Placeholder
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.
● Later - It is lazy and memoized.
● Always - It is lazy and non-memoized.
14. Why Eval Monad?
Image Placeholder Image Placeholder Image Placeholder
● .map and flatMap method, helpful in chaining methods.
● memoize capability in chaining methods.
15. Why Eval Monad?
Image Placeholder Image Placeholder Image Placeholder
● stack-safety of map and flatMap method and defer method
A simple factorial method:
● Eval.defer to rescue:
16. References
Image Placeholder Image Placeholder Image Placeholder
● 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