SlideShare a Scribd company logo
1 of 22
Download to read offline
How to be functional in
concurrent programming
with Monix
Presented By: Shubham Verma
Senior Software Consultant
Knoldus Inc.
01 What is FP
02 Terminology
03 Scala Future API
04 Monix
05 Magic of Monix
Our Agenda
What is FP aka Functional programming?
● Functions - First class citizens
● Lazy Evaluation
● Immutability or avoid using shared mutable
state
● No Side-effects
● Referential Transparency
Scala Future API
● Scala’s inbuilt concurrency mechanism which enables us programming using
threads, which is built to provide functional programming essence using monad
pattern.
● Does not provide good approach for dealing with side effects which breaks the
pattern for functional programming.
● Uses shared mutable state, as in sharing the Execution context
Monix
● Monix is a high-performance Scala /
Scala.js library for composing
asynchronous, event-based
programs.
● Clean modular API and helps us in
writing our code using functional
constructs.
● Exposes different types of
mechanism and utilities for the
building concurrent or event based
programs .
● Nice interoperability with futures.
https://monix.io/docs/current/intro/usage.html#sub-modules--dependencies-graph
Lazy Evaluation
Future API
val eagerFuture: Future[Int] =
Future {
println("========== You just started
me ==============")
34
}
Lazy Evaluation
Future API
val eagerFuture: Future[Int] =
Future {
println("========== You just
started me ==============")
34
}
Fix
def theLazyIllusion: Future[Int] =
Future {
println("========== You get the idea
==============")
34
}
Lazy Evaluation
Future API
val eagerFuture: Future[Int] =
Future {
println("========== You just
started me ==============")
34
}
Fix
def theLazyIllusion: Future[Int] =
Future {
println("========== You get the idea
==============")
34
}
Lazy Evaluation
Future API
val eagerFuture: Future[Int] =
Future {
println("========== You just
started me ==============")
34
}
Fix
def theLazyIllusion: Future[Int] =
Future {
println("========== You get the idea
==============")
34
}
Monix
val iAmLazy: Task[Int] = Task(34)
Lazy Evaluation
Future API
val eagerFuture: Future[Int] =
Future {
println("========== You just
started me ==============")
34
}
Fix
def theLazyIllusion: Future[Int] =
Future {
println("========== You get the idea
==============")
34
}
Monix
val iAmLazy: Task[Int] = Task(34)
val nowTask: Task[Int] = Task.now(45)
Lazy Evaluation
Future API
val eagerFuture: Future[Int] =
Future {
println("========== You just
started me ==============")
34
}
Fix
def theLazyIllusion: Future[Int] =
Future {
println("========== You get the idea
==============")
34
}
Monix
val iAmLazy: Task[Int] = Task(34)
val nowTask: Task[Int] = Task.now(45)
val evalOnce = Task.evalOnce {
println("Only gets executed once")
67
}
Shared State
Future API
def apply[T]
(body: =>T)
(implicit @deprecatedName('execctx) executor: ExecutionContext)
: Future[T]
def flatMap[S]
(f: T => Future[S])
(implicit executor: ExecutionContext)
: Future[S]
Shared State
Future API (Problem)
import scala.concurrent.ExecutionContext.Implicits.global
Without the above statement the below code would show a demand for an implicit parameter for
execution context
val eagerFuture: Future[Int] = Future {
println("========== You just started me ==============")
34
}
def theLazyIllusion: Future[Int] = Future {
println("========== You get the idea ==============")
34
}
Shared State
Monix
object MonixSharedState extends App {
val ec : ExecutionContextExecutorService =
ExecutionContext. fromExecutorService(Executors.newFixedThreadPool(8))
def future(ec: ExecutionContext) = Future {
println("Started executing" )
34
}(ec)
val task = Task.fromFuture(future(ec))
val taskDelayed = Task.deferFuture(future(ec))
//Or
val suspended = Task.suspend(task)
}
Shared State
Monix
object MonixSharedState extends App {
val ec : ExecutionContextExecutorService =
ExecutionContext. fromExecutorService(Executors.newFixedThreadPool(8))
def future(ec: ExecutionContext) = Future {
println("Started executing" )
34
}(ec)
val task = Task.fromFuture(future(ec))
val taskDelayed = Task.deferFuture(future(ec))
//Or
val suspended = Task.suspend(task)
}
Is monix solving the
problem ??
Shared State
Monix
object MonixSharedState extends App {
val ec : ExecutionContextExecutorService =
ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(8))
def future(ec: ExecutionContext) = Future {
println("Started executing")
34
}(ec)
val task = Task.fromFuture(future(ec))
val taskDelayed = Task.deferFuture(future(ec))
//Or
val suspended = Task.suspend(task)
//Let’s us define pure functions
def edSolution: Task[Int] = Task.deferFutureAction { scheduler =>
future(scheduler)
}
No Side effects
Future API
def read(implicit ec:ExecutionContext): Future[Option[String]] = Future
{
Try{
val file = new FileInputStream(new File("some file.txt"))
val str = file.toString
file.close()
str
}.toOption
}
No Side effects
Monix
val task = Task(new FileInputStream(new File("some
file.txt")))
val looksCool: Task[String] = task.bracket{ stream =>
Task.evalOnce(stream.toString)
} { doneWithStream =>
Task(doneWithStream.close())
}
Modules at glance
● Monix-execution - the lower level primitives for dealing with asynchronous
execution, thus exposing Scheduler and Cancelable.
● Monix-catnap - You can use just monix-catnap (see API Docs), the high-level
primitives building on top of Cats Effect.
● Monix-eval - You can use just monix-eval, the sub-project that exposes Task and
Coeval:
● Monix-reactive - You can use just monix-reactive, the sub-project that exposes the
Observable pattern.
● Monix-tail - You can use just monix-tail, the sub-project that exposes Iterant for
pull based streaming.
○
References
● https://monix.io/docs/current/intro/usage.html#sub-modules--dependencies-gr
aph
● https://www.youtube.com/watch?v=RzV-VWLPGmM&t
● https://www.youtube.com/watch?v=40xEzPNJdEA
● https://monix.io/
Any Questions?
Thank You
: )

More Related Content

What's hot

A simple program C# program
A simple program C# programA simple program C# program
A simple program C# programMicheal Ogundero
 
Exp 3-2 d422 (1)
Exp 3-2  d422 (1)Exp 3-2  d422 (1)
Exp 3-2 d422 (1)Omkar Rane
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or notaluavi
 
Architecture for scalable Angular applications
Architecture for scalable Angular applicationsArchitecture for scalable Angular applications
Architecture for scalable Angular applicationsPaweł Żurowski
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structuresMicheal Ogundero
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix CoevalKnoldus Inc.
 
Xilinx ISE introduction Tutorial #1
Xilinx ISE introduction Tutorial #1Xilinx ISE introduction Tutorial #1
Xilinx ISE introduction Tutorial #1guest1e88645e
 

What's hot (8)

A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
 
Exp 3-2 d422 (1)
Exp 3-2  d422 (1)Exp 3-2  d422 (1)
Exp 3-2 d422 (1)
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
Architecture for scalable Angular applications
Architecture for scalable Angular applicationsArchitecture for scalable Angular applications
Architecture for scalable Angular applications
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
Aligning OCL and UML
Aligning OCL and UMLAligning OCL and UML
Aligning OCL and UML
 
Introduction to Monix Coeval
Introduction to Monix CoevalIntroduction to Monix Coeval
Introduction to Monix Coeval
 
Xilinx ISE introduction Tutorial #1
Xilinx ISE introduction Tutorial #1Xilinx ISE introduction Tutorial #1
Xilinx ISE introduction Tutorial #1
 

Similar to Monix : A Birds’ eye view

Scala Future & Promises
Scala Future & PromisesScala Future & Promises
Scala Future & PromisesKnoldus Inc.
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of SignalsCoding Academy
 
How To Use IO Monads in Scala?
 How To Use IO Monads in Scala? How To Use IO Monads in Scala?
How To Use IO Monads in Scala?Knoldus Inc.
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)jeffz
 
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scalaStratio
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom componentsJeremy Grancher
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav DukhinFwdays
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptKaty Slemon
 
Delegateless Coordinators - take 2
Delegateless Coordinators - take 2Delegateless Coordinators - take 2
Delegateless Coordinators - take 2Tales Andrade
 
Programming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidProgramming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidEmanuele Di Saverio
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka RemotingKnoldus Inc.
 
FRP: What does "declarative" mean
FRP: What does "declarative" meanFRP: What does "declarative" mean
FRP: What does "declarative" meanPeter Ovchinnikov
 
Drilling the Async Library
Drilling the Async LibraryDrilling the Async Library
Drilling the Async LibraryKnoldus Inc.
 
Using Akka Futures
Using Akka FuturesUsing Akka Futures
Using Akka FuturesKnoldus Inc.
 
C sharp 8.0 new features
C sharp 8.0 new featuresC sharp 8.0 new features
C sharp 8.0 new featuresMSDEVMTL
 
C sharp 8.0 new features
C sharp 8.0 new featuresC sharp 8.0 new features
C sharp 8.0 new featuresMiguel Bernard
 
xlwings - For Python Quants Conference (London 2014)
xlwings - For Python Quants Conference (London 2014)xlwings - For Python Quants Conference (London 2014)
xlwings - For Python Quants Conference (London 2014)xlwings
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
Going open source with small teams
Going open source with small teamsGoing open source with small teams
Going open source with small teamsJamie Thomas
 

Similar to Monix : A Birds’ eye view (20)

Scala Future & Promises
Scala Future & PromisesScala Future & Promises
Scala Future & Promises
 
Angular 16 – the rise of Signals
Angular 16 – the rise of SignalsAngular 16 – the rise of Signals
Angular 16 – the rise of Signals
 
How To Use IO Monads in Scala?
 How To Use IO Monads in Scala? How To Use IO Monads in Scala?
How To Use IO Monads in Scala?
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
 
Introduction to Asynchronous scala
Introduction to Asynchronous scalaIntroduction to Asynchronous scala
Introduction to Asynchronous scala
 
React Native custom components
React Native custom componentsReact Native custom components
React Native custom components
 
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
"Applied Enterprise Metaprogramming in JavaScript", Vladyslav Dukhin
 
How to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescriptHow to build to do app using vue composition api and vuex 4 with typescript
How to build to do app using vue composition api and vuex 4 with typescript
 
Delegateless Coordinators - take 2
Delegateless Coordinators - take 2Delegateless Coordinators - take 2
Delegateless Coordinators - take 2
 
Programming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidProgramming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for Android
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka Remoting
 
FRP: What does "declarative" mean
FRP: What does "declarative" meanFRP: What does "declarative" mean
FRP: What does "declarative" mean
 
Drilling the Async Library
Drilling the Async LibraryDrilling the Async Library
Drilling the Async Library
 
Using Akka Futures
Using Akka FuturesUsing Akka Futures
Using Akka Futures
 
C sharp 8.0 new features
C sharp 8.0 new featuresC sharp 8.0 new features
C sharp 8.0 new features
 
C sharp 8.0 new features
C sharp 8.0 new featuresC sharp 8.0 new features
C sharp 8.0 new features
 
xlwings - For Python Quants Conference (London 2014)
xlwings - For Python Quants Conference (London 2014)xlwings - For Python Quants Conference (London 2014)
xlwings - For Python Quants Conference (London 2014)
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Pure Future
Pure FuturePure Future
Pure Future
 
Going open source with small teams
Going open source with small teamsGoing open source with small teams
Going open source with small teams
 

More from Knoldus Inc.

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 ParsingKnoldus Inc.
 
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 IntroductionKnoldus Inc.
 
Entity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptxEntity Core with Core Microservices.pptx
Entity Core with Core Microservices.pptxKnoldus Inc.
 
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.pptxKnoldus Inc.
 
GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfKnoldus Inc.
 
NuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxNuGet Packages Presentation (DoT NeT).pptx
NuGet Packages Presentation (DoT NeT).pptxKnoldus Inc.
 
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 TestingKnoldus Inc.
 
K8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesK8sGPTThe AI​ way to diagnose Kubernetes
K8sGPTThe AI​ way to diagnose KubernetesKnoldus Inc.
 
Introduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxIntroduction to Circle Ci Presentation.pptx
Introduction to Circle Ci Presentation.pptxKnoldus Inc.
 
Robusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxRobusta -Tool Presentation (DevOps).pptx
Robusta -Tool Presentation (DevOps).pptxKnoldus Inc.
 
Optimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxOptimizing Kubernetes using GOLDILOCKS.pptx
Optimizing Kubernetes using GOLDILOCKS.pptxKnoldus Inc.
 
Azure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxAzure Function App Exception Handling.pptx
Azure Function App Exception Handling.pptxKnoldus Inc.
 
CQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxCQRS Design Pattern Presentation (Java).pptx
CQRS Design Pattern Presentation (Java).pptxKnoldus Inc.
 
ETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationETL Observability: Azure to Snowflake Presentation
ETL Observability: Azure to Snowflake PresentationKnoldus Inc.
 
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 PresentationKnoldus Inc.
 
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 APIsKnoldus Inc.
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II PresentationKnoldus Inc.
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Configuring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRAConfiguring Workflows & Validators in JIRA
Configuring Workflows & Validators in JIRAKnoldus Inc.
 
Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)Knoldus Inc.
 

More from Knoldus Inc. (20)

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
 
Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)Advanced Python (with dependency injection and hydra configuration packages)
Advanced Python (with dependency injection and hydra configuration packages)
 

Recently uploaded

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

Monix : A Birds’ eye view

  • 1. How to be functional in concurrent programming with Monix Presented By: Shubham Verma Senior Software Consultant Knoldus Inc.
  • 2. 01 What is FP 02 Terminology 03 Scala Future API 04 Monix 05 Magic of Monix Our Agenda
  • 3. What is FP aka Functional programming? ● Functions - First class citizens ● Lazy Evaluation ● Immutability or avoid using shared mutable state ● No Side-effects ● Referential Transparency
  • 4. Scala Future API ● Scala’s inbuilt concurrency mechanism which enables us programming using threads, which is built to provide functional programming essence using monad pattern. ● Does not provide good approach for dealing with side effects which breaks the pattern for functional programming. ● Uses shared mutable state, as in sharing the Execution context
  • 5. Monix ● Monix is a high-performance Scala / Scala.js library for composing asynchronous, event-based programs. ● Clean modular API and helps us in writing our code using functional constructs. ● Exposes different types of mechanism and utilities for the building concurrent or event based programs . ● Nice interoperability with futures. https://monix.io/docs/current/intro/usage.html#sub-modules--dependencies-graph
  • 6. Lazy Evaluation Future API val eagerFuture: Future[Int] = Future { println("========== You just started me ==============") 34 }
  • 7. Lazy Evaluation Future API val eagerFuture: Future[Int] = Future { println("========== You just started me ==============") 34 } Fix def theLazyIllusion: Future[Int] = Future { println("========== You get the idea ==============") 34 }
  • 8. Lazy Evaluation Future API val eagerFuture: Future[Int] = Future { println("========== You just started me ==============") 34 } Fix def theLazyIllusion: Future[Int] = Future { println("========== You get the idea ==============") 34 }
  • 9. Lazy Evaluation Future API val eagerFuture: Future[Int] = Future { println("========== You just started me ==============") 34 } Fix def theLazyIllusion: Future[Int] = Future { println("========== You get the idea ==============") 34 } Monix val iAmLazy: Task[Int] = Task(34)
  • 10. Lazy Evaluation Future API val eagerFuture: Future[Int] = Future { println("========== You just started me ==============") 34 } Fix def theLazyIllusion: Future[Int] = Future { println("========== You get the idea ==============") 34 } Monix val iAmLazy: Task[Int] = Task(34) val nowTask: Task[Int] = Task.now(45)
  • 11. Lazy Evaluation Future API val eagerFuture: Future[Int] = Future { println("========== You just started me ==============") 34 } Fix def theLazyIllusion: Future[Int] = Future { println("========== You get the idea ==============") 34 } Monix val iAmLazy: Task[Int] = Task(34) val nowTask: Task[Int] = Task.now(45) val evalOnce = Task.evalOnce { println("Only gets executed once") 67 }
  • 12. Shared State Future API def apply[T] (body: =>T) (implicit @deprecatedName('execctx) executor: ExecutionContext) : Future[T] def flatMap[S] (f: T => Future[S]) (implicit executor: ExecutionContext) : Future[S]
  • 13. Shared State Future API (Problem) import scala.concurrent.ExecutionContext.Implicits.global Without the above statement the below code would show a demand for an implicit parameter for execution context val eagerFuture: Future[Int] = Future { println("========== You just started me ==============") 34 } def theLazyIllusion: Future[Int] = Future { println("========== You get the idea ==============") 34 }
  • 14. Shared State Monix object MonixSharedState extends App { val ec : ExecutionContextExecutorService = ExecutionContext. fromExecutorService(Executors.newFixedThreadPool(8)) def future(ec: ExecutionContext) = Future { println("Started executing" ) 34 }(ec) val task = Task.fromFuture(future(ec)) val taskDelayed = Task.deferFuture(future(ec)) //Or val suspended = Task.suspend(task) }
  • 15. Shared State Monix object MonixSharedState extends App { val ec : ExecutionContextExecutorService = ExecutionContext. fromExecutorService(Executors.newFixedThreadPool(8)) def future(ec: ExecutionContext) = Future { println("Started executing" ) 34 }(ec) val task = Task.fromFuture(future(ec)) val taskDelayed = Task.deferFuture(future(ec)) //Or val suspended = Task.suspend(task) } Is monix solving the problem ??
  • 16. Shared State Monix object MonixSharedState extends App { val ec : ExecutionContextExecutorService = ExecutionContext.fromExecutorService(Executors.newFixedThreadPool(8)) def future(ec: ExecutionContext) = Future { println("Started executing") 34 }(ec) val task = Task.fromFuture(future(ec)) val taskDelayed = Task.deferFuture(future(ec)) //Or val suspended = Task.suspend(task) //Let’s us define pure functions def edSolution: Task[Int] = Task.deferFutureAction { scheduler => future(scheduler) }
  • 17. No Side effects Future API def read(implicit ec:ExecutionContext): Future[Option[String]] = Future { Try{ val file = new FileInputStream(new File("some file.txt")) val str = file.toString file.close() str }.toOption }
  • 18. No Side effects Monix val task = Task(new FileInputStream(new File("some file.txt"))) val looksCool: Task[String] = task.bracket{ stream => Task.evalOnce(stream.toString) } { doneWithStream => Task(doneWithStream.close()) }
  • 19. Modules at glance ● Monix-execution - the lower level primitives for dealing with asynchronous execution, thus exposing Scheduler and Cancelable. ● Monix-catnap - You can use just monix-catnap (see API Docs), the high-level primitives building on top of Cats Effect. ● Monix-eval - You can use just monix-eval, the sub-project that exposes Task and Coeval: ● Monix-reactive - You can use just monix-reactive, the sub-project that exposes the Observable pattern. ● Monix-tail - You can use just monix-tail, the sub-project that exposes Iterant for pull based streaming. ○