SlideShare a Scribd company logo
1 of 17
Download to read offline
Presented By: Muskan Gupta
Diving into Monads
in Cats Library
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.
Our Agenda
What is Monad?
Introduction to Cats
Monad in Cats Library
Either Monad
Identity Monad
Eval Monad
What is Monad?
Image Placeholder Image Placeholder Image Placeholder
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.
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))
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._
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._
Either Monad
Image Placeholder Image Placeholder
Either in Scala Library
Either in Cats Library
Either in Cats over Either in Scala
Image Placeholder Image Placeholder Image Placeholder
● Issue with return type resolved.
● Right biased
● Extension methods
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:
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:
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.
Why Eval Monad?
Image Placeholder Image Placeholder Image Placeholder
● .map and flatMap method, helpful in chaining methods.
● memoize capability in chaining methods.
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:
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
Thank You !

More Related Content

Similar to Diving into monads in cats library

Similar to Diving into monads in cats library (20)

Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
Xavier Amatriain, VP of Engineering, Quora at MLconf SF - 11/13/15
 
10 more lessons learned from building Machine Learning systems - MLConf
10 more lessons learned from building Machine Learning systems - MLConf10 more lessons learned from building Machine Learning systems - MLConf
10 more lessons learned from building Machine Learning systems - MLConf
 
10 more lessons learned from building Machine Learning systems
10 more lessons learned from building Machine Learning systems10 more lessons learned from building Machine Learning systems
10 more lessons learned from building Machine Learning systems
 
Ase01.ppt
Ase01.pptAse01.ppt
Ase01.ppt
 
Build Your Own Monads
Build Your Own MonadsBuild Your Own Monads
Build Your Own Monads
 
Predictive analytics semi-supervised learning with GANs
Predictive analytics   semi-supervised learning with GANsPredictive analytics   semi-supervised learning with GANs
Predictive analytics semi-supervised learning with GANs
 
Set Transfomer: A Framework for Attention-based Permutaion-Invariant Neural N...
Set Transfomer: A Framework for Attention-based Permutaion-Invariant Neural N...Set Transfomer: A Framework for Attention-based Permutaion-Invariant Neural N...
Set Transfomer: A Framework for Attention-based Permutaion-Invariant Neural N...
 
Design patterns in javascript
Design patterns in javascriptDesign patterns in javascript
Design patterns in javascript
 
Language translation with Deep Learning (RNN) with TensorFlow
Language translation with Deep Learning (RNN) with TensorFlowLanguage translation with Deep Learning (RNN) with TensorFlow
Language translation with Deep Learning (RNN) with TensorFlow
 
Number Crunching in Python
Number Crunching in PythonNumber Crunching in Python
Number Crunching in Python
 
Practical ML
Practical MLPractical ML
Practical ML
 
DDW Clinic Session 1.pdf
DDW Clinic Session 1.pdfDDW Clinic Session 1.pdf
DDW Clinic Session 1.pdf
 
CBIR by deep learning
CBIR by deep learningCBIR by deep learning
CBIR by deep learning
 
Learning stochastic neural networks with Chainer
Learning stochastic neural networks with ChainerLearning stochastic neural networks with Chainer
Learning stochastic neural networks with Chainer
 
Visualization of Deep Learning
Visualization of Deep LearningVisualization of Deep Learning
Visualization of Deep Learning
 
Generative adversarial networks
Generative adversarial networksGenerative adversarial networks
Generative adversarial networks
 
Deep Generative Modelling
Deep Generative ModellingDeep Generative Modelling
Deep Generative Modelling
 
Autoencoders
AutoencodersAutoencoders
Autoencoders
 
Btp viewmorph
Btp viewmorphBtp viewmorph
Btp viewmorph
 
Machine learning Experiments report
Machine learning Experiments report Machine learning Experiments report
Machine learning Experiments report
 

More from Knoldus Inc.

More from Knoldus Inc. (20)

Supply chain security with Kubeclarity.pptx
Supply chain security with Kubeclarity.pptxSupply chain security with Kubeclarity.pptx
Supply chain security with Kubeclarity.pptx
 
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML ParsingMastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
Mastering Web Scraping with JSoup Unlocking the Secrets of HTML Parsing
 
Akka gRPC Essentials A Hands-On Introduction
Akka gRPC Essentials A Hands-On IntroductionAkka gRPC Essentials A Hands-On Introduction
Akka gRPC Essentials A Hands-On Introduction
 
Entity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptxEntity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptx
 
Introduction to Redis and its features.pptx
Introduction to Redis and its features.pptxIntroduction to Redis and its features.pptx
Introduction to Redis and its features.pptx
 
GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdf
 
NuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxNuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptx
 
Data Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable TestingData Quality in Test Automation Navigating the Path to Reliable Testing
Data Quality in Test Automation Navigating the Path to Reliable Testing
 
K8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesK8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose Kubernetes
 
Introduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxIntroduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptx
 
Robusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxRobusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptx
 
Optimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxOptimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptx
 
Azure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxAzure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptx
 
CQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxCQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptx
 
ETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake Presentation
 
Scripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics PresentationScripting with K6 - Beyond the Basics Presentation
Scripting with K6 - Beyond the Basics Presentation
 
Getting started with dotnet core Web APIs
Getting started with dotnet core Web APIsGetting started with dotnet core Web APIs
Getting started with dotnet core Web APIs
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Configuring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRAConfiguring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRA
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Diving into monads in cats library

  • 1. Presented By: Muskan Gupta Diving into Monads in Cats Library
  • 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
  • 4. What is Monad? Image Placeholder Image Placeholder Image Placeholder
  • 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._
  • 9. Either Monad Image Placeholder Image Placeholder Either in Scala Library Either in Cats Library
  • 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