SlideShare a Scribd company logo
1 of 17
Download to read offline
Cats IO
Abhishek Srivastava
Handling Effects Functionally
• What are effects

• Reading / Writing from a database

• Reading / Writing to Files or Console

• Performing Mutation of variables
!2
Handling Effects is hard
• Effects are hard to test.

• Introduce Concurrency Problems

• Make refactoring Hard
!3
What we need
• Safely manipulate effects

• Test code with Effects with ease.

• Build a description of the program

• Description can be changed by runtime.

• Treat code as a data structure

• Separate composition from declaration
!4
Why is IO a Monad
• Monads are about sequencing computation and when dealing with
effects, sequencing is required.
!5
Origin of IO
• In a Haskell program all roads lead to an IO

• Compare that to Scala
main::IO()
main = putStrLn "Hello World"
def main(args: Array[String]) : Unit = {
println(“Hello World”)
}
!6
Future as IO
• Futures are not lazy

• Do not separate specification from execution

• Futures are not referentially transparent
val program = for {
x <- Future { println(“foo”); 10 }
y <- Future{ println(“foo”); 10}
} yield x + y
val result = Await.result(program, Duration.Inf)
println(result)
val future = Future { println(“foo”); 10}
val program = for {
x <- future
Y <- future
} x + y
val result = Await.result(program, Duration.Inf)
println(result)
!7
Previous attempts at IO
• ScalaZ 7 had IO and Task

• Other libraries like fs2, monix etc also came up with Task implementations

• Slick has DBIO
!8
Cats IO
• SBT Dependency

• Now the Scala program looks like
"org.typelevel" %% "cats-effect" % "1.0.0"
import cats.effect._
import cats.syntax.all._
object CatsIOEx1 extends IOApp {
def run(args: List[String]) : IO[ExitCode] = {
IO { println("Hello World") }.as(ExitCode.Success)
}
}
!9
Referential Transparency
import cats.effect._
import cats.syntax.all._
object CatsIOEx3 extends IOApp {
def run(args: List[String]) : IO[ExitCode]
= {
val program = for {
x <- IO {println("foo"); 10}
y <- IO {println("foo"); 10}
} yield x + y
program.as(ExitCode.Success)
}
}
import cats.effect._
import cats.syntax.all._
object CatsIOEx4 extends IOApp {
def run(args: List[String]) : IO[ExitCode] =
{
val io = IO { println("foo"); 10}
val program = for {
x <- io
y <- io
} yield x + y
program.as(ExitCode.Success)
}
}
!10
Stack Safety
IO is trampolined in its flatMap evaluation. This means that you can safely
call flatMap in a recursive function of arbitrary depth without blowing up
the stack.
def fib(n: Int, a: BigDecimal = 0, b: BigDecimal = 1) : IO[BigDecimal] = {
IO(a + b).flatMap{b2 =>
if (n > 1)
fib(n - 1, b, b2)
else IO.pure(b2)
}
}
!11
Resource cleanup
Traditionally we use try/catch/finally as a mechanism of resource cleanup.
The problem with this is that its tied to “exception handling” which is not
considered functional

val program = IO(new BufferedReader(new FileReader(new File("~/temp/names.txt")))).bracket {in =>
var content: String = ""
var line = in.readLine()
while(line != null) {
content += line
line = in.readLine()
}
IO(content)
} {in =>
IO(in.close())
}
!12
Error Handling
Error Handling is typically done with try/catch/finally. But its hard to
implement retry semantics with try/catch/finally
def retryWithBackOff[A](io: IO[A], initDelay: FiniteDuration, maxRetries: Int) :
IO[A] = {
io.handleErrorWith{err =>
if (maxRetries > 1)
IO.sleep(initDelay) *> retryWithBackOff(io, initDelay * 2, maxRetries -
1)
else
IO.raiseError(err)
}
}
!13
Thread Shifting
In Scala we have a best practice of having 2 thread pools. One is a
bounded thread pool for CPU intensive tasks. One is a unbounded pool
for IO waits. With IO its easy to switch between these connection pools
object CatsIOEx9 extends App {
val Main = ExecutionContext.global
val BlockingIO = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
val program = for {
_ <- IO { println("what is your name")}
_ <- IO.shift(BlockingIO)
name <- IO { readLine }
_ <- IO.shift(Main)
} yield s"Hello $name"
val output = program.unsafeRunSync
println(output)
}
!14
Parallelism
We can also run multiple IOs at once and then process the results when all
of them have completed.

object CatsIOEx10 extends IOApp {
def run(args: List[String]) : IO[ExitCode] = {
val io1 = IO.delay(5 seconds) *> IO(20)
val io2 = IO.delay(2 seconds) *> IO(10)
val io3 = IO.delay(1 seconds) *> IO(50)
val program = (io1, io2, io3).parMapN{ (a, b, c) => a + b + c}
program.flatMap(x => IO{println(s"result $x")}).as(ExitCode.Success)
}
}
!15
Parallelism
Just like futures, its possible to take a list of IOs and convert into a single
IO (like Future.sequence).

object CatsIOEx11 extends IOApp {
def run(args: List[String]) : IO[ExitCode] = {
val io1 = IO.delay(5 seconds) *> IO(20)
val io2 = IO.delay(2 seconds) *> IO(10)
val io3 = IO.delay(1 seconds) *> IO(50)
val ioList = NonEmptyList.of(io1, io2, io3)
ioList.parSequence.flatMap{list => IO{println(s"sum: ${list.foldLeft(0)(_ +
_)}")}}.as(ExitCode.Success)
}
}
!16
Cats IO in the real world
The following libraries use the Cats IO Monad 

• Doobie

• Http4s

• Sttp

• Monix

• FS2

• PureConfig
!17

More Related Content

What's hot

Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Yevhen Bobrov
 
The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016Frank Lyaruu
 
From Java 6 to Java 7 reference
From Java 6 to Java 7 referenceFrom Java 6 to Java 7 reference
From Java 6 to Java 7 referenceGiacomo Veneri
 
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to CeleryIdan Gazit
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Morris Singer
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaFrank Lyaruu
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task QueueDuy Do
 
Finch.io - Purely Functional REST API with Finagle
Finch.io - Purely Functional REST API with FinagleFinch.io - Purely Functional REST API with Finagle
Finch.io - Purely Functional REST API with FinagleVladimir Kostyukov
 
ESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. NowESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. NowKrzysztof Szafranek
 
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...Víctor Bolinches
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSOswald Campesato
 
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
Node.js: Continuation-Local-Storage and the Magic of AsyncListenerNode.js: Continuation-Local-Storage and the Magic of AsyncListener
Node.js: Continuation-Local-Storage and the Magic of AsyncListenerIslam Sharabash
 
Java libraries you can't afford to miss
Java libraries you can't afford to missJava libraries you can't afford to miss
Java libraries you can't afford to missAndres Almiray
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Alexey Fyodorov
 
Java Play RESTful ebean
Java Play RESTful ebeanJava Play RESTful ebean
Java Play RESTful ebeanFaren faren
 

What's hot (20)

Orleankka Intro Circa 2015
Orleankka Intro Circa 2015Orleankka Intro Circa 2015
Orleankka Intro Circa 2015
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016
 
From Java 6 to Java 7 reference
From Java 6 to Java 7 referenceFrom Java 6 to Java 7 reference
From Java 6 to Java 7 reference
 
An Introduction to Celery
An Introduction to CeleryAn Introduction to Celery
An Introduction to Celery
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
 
Fiber supervision in ZIO
Fiber supervision in ZIOFiber supervision in ZIO
Fiber supervision in ZIO
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 
Celery - A Distributed Task Queue
Celery - A Distributed Task QueueCelery - A Distributed Task Queue
Celery - A Distributed Task Queue
 
Finch.io - Purely Functional REST API with Finagle
Finch.io - Purely Functional REST API with FinagleFinch.io - Purely Functional REST API with Finagle
Finch.io - Purely Functional REST API with Finagle
 
ESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. NowESCMAScript 6: Get Ready For The Future. Now
ESCMAScript 6: Get Ready For The Future. Now
 
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
Paradigma FP y OOP usando técnicas avanzadas de Programación | Programacion A...
 
Functional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJSFunctional Reactive Programming (FRP): Working with RxJS
Functional Reactive Programming (FRP): Working with RxJS
 
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
Node.js: Continuation-Local-Storage and the Magic of AsyncListenerNode.js: Continuation-Local-Storage and the Magic of AsyncListener
Node.js: Continuation-Local-Storage and the Magic of AsyncListener
 
Java libraries you can't afford to miss
Java libraries you can't afford to missJava libraries you can't afford to miss
Java libraries you can't afford to miss
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
 
Djangocon
DjangoconDjangocon
Djangocon
 
Java Play RESTful ebean
Java Play RESTful ebeanJava Play RESTful ebean
Java Play RESTful ebean
 
Full Stack Unit Testing
Full Stack Unit TestingFull Stack Unit Testing
Full Stack Unit Testing
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 

Similar to Handling Effects Functionally with Cats IO

Similar to Handling Effects Functionally with Cats IO (20)

The Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect SystemThe Design of the Scalaz 8 Effect System
The Design of the Scalaz 8 Effect System
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Java
JavaJava
Java
 
What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)What can be done with Java, but should better be done with Erlang (@pavlobaron)
What can be done with Java, but should better be done with Erlang (@pavlobaron)
 
Java 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from OredevJava 7 Whats New(), Whats Next() from Oredev
Java 7 Whats New(), Whats Next() from Oredev
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011Java Bytecode for Discriminating Developers - JavaZone 2011
Java Bytecode for Discriminating Developers - JavaZone 2011
 
ECMAScript 2015
ECMAScript 2015ECMAScript 2015
ECMAScript 2015
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Future vs. Monix Task
Future vs. Monix TaskFuture vs. Monix Task
Future vs. Monix Task
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java Basics V3
Java Basics V3Java Basics V3
Java Basics V3
 

Recently uploaded

Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Recently uploaded (20)

Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

Handling Effects Functionally with Cats IO

  • 2. Handling Effects Functionally • What are effects • Reading / Writing from a database • Reading / Writing to Files or Console • Performing Mutation of variables !2
  • 3. Handling Effects is hard • Effects are hard to test. • Introduce Concurrency Problems • Make refactoring Hard !3
  • 4. What we need • Safely manipulate effects • Test code with Effects with ease. • Build a description of the program • Description can be changed by runtime. • Treat code as a data structure • Separate composition from declaration !4
  • 5. Why is IO a Monad • Monads are about sequencing computation and when dealing with effects, sequencing is required. !5
  • 6. Origin of IO • In a Haskell program all roads lead to an IO • Compare that to Scala main::IO() main = putStrLn "Hello World" def main(args: Array[String]) : Unit = { println(“Hello World”) } !6
  • 7. Future as IO • Futures are not lazy • Do not separate specification from execution • Futures are not referentially transparent val program = for { x <- Future { println(“foo”); 10 } y <- Future{ println(“foo”); 10} } yield x + y val result = Await.result(program, Duration.Inf) println(result) val future = Future { println(“foo”); 10} val program = for { x <- future Y <- future } x + y val result = Await.result(program, Duration.Inf) println(result) !7
  • 8. Previous attempts at IO • ScalaZ 7 had IO and Task • Other libraries like fs2, monix etc also came up with Task implementations • Slick has DBIO !8
  • 9. Cats IO • SBT Dependency • Now the Scala program looks like "org.typelevel" %% "cats-effect" % "1.0.0" import cats.effect._ import cats.syntax.all._ object CatsIOEx1 extends IOApp { def run(args: List[String]) : IO[ExitCode] = { IO { println("Hello World") }.as(ExitCode.Success) } } !9
  • 10. Referential Transparency import cats.effect._ import cats.syntax.all._ object CatsIOEx3 extends IOApp { def run(args: List[String]) : IO[ExitCode] = { val program = for { x <- IO {println("foo"); 10} y <- IO {println("foo"); 10} } yield x + y program.as(ExitCode.Success) } } import cats.effect._ import cats.syntax.all._ object CatsIOEx4 extends IOApp { def run(args: List[String]) : IO[ExitCode] = { val io = IO { println("foo"); 10} val program = for { x <- io y <- io } yield x + y program.as(ExitCode.Success) } } !10
  • 11. Stack Safety IO is trampolined in its flatMap evaluation. This means that you can safely call flatMap in a recursive function of arbitrary depth without blowing up the stack. def fib(n: Int, a: BigDecimal = 0, b: BigDecimal = 1) : IO[BigDecimal] = { IO(a + b).flatMap{b2 => if (n > 1) fib(n - 1, b, b2) else IO.pure(b2) } } !11
  • 12. Resource cleanup Traditionally we use try/catch/finally as a mechanism of resource cleanup. The problem with this is that its tied to “exception handling” which is not considered functional val program = IO(new BufferedReader(new FileReader(new File("~/temp/names.txt")))).bracket {in => var content: String = "" var line = in.readLine() while(line != null) { content += line line = in.readLine() } IO(content) } {in => IO(in.close()) } !12
  • 13. Error Handling Error Handling is typically done with try/catch/finally. But its hard to implement retry semantics with try/catch/finally def retryWithBackOff[A](io: IO[A], initDelay: FiniteDuration, maxRetries: Int) : IO[A] = { io.handleErrorWith{err => if (maxRetries > 1) IO.sleep(initDelay) *> retryWithBackOff(io, initDelay * 2, maxRetries - 1) else IO.raiseError(err) } } !13
  • 14. Thread Shifting In Scala we have a best practice of having 2 thread pools. One is a bounded thread pool for CPU intensive tasks. One is a unbounded pool for IO waits. With IO its easy to switch between these connection pools object CatsIOEx9 extends App { val Main = ExecutionContext.global val BlockingIO = ExecutionContext.fromExecutor(Executors.newCachedThreadPool()) val program = for { _ <- IO { println("what is your name")} _ <- IO.shift(BlockingIO) name <- IO { readLine } _ <- IO.shift(Main) } yield s"Hello $name" val output = program.unsafeRunSync println(output) } !14
  • 15. Parallelism We can also run multiple IOs at once and then process the results when all of them have completed. object CatsIOEx10 extends IOApp { def run(args: List[String]) : IO[ExitCode] = { val io1 = IO.delay(5 seconds) *> IO(20) val io2 = IO.delay(2 seconds) *> IO(10) val io3 = IO.delay(1 seconds) *> IO(50) val program = (io1, io2, io3).parMapN{ (a, b, c) => a + b + c} program.flatMap(x => IO{println(s"result $x")}).as(ExitCode.Success) } } !15
  • 16. Parallelism Just like futures, its possible to take a list of IOs and convert into a single IO (like Future.sequence). object CatsIOEx11 extends IOApp { def run(args: List[String]) : IO[ExitCode] = { val io1 = IO.delay(5 seconds) *> IO(20) val io2 = IO.delay(2 seconds) *> IO(10) val io3 = IO.delay(1 seconds) *> IO(50) val ioList = NonEmptyList.of(io1, io2, io3) ioList.parSequence.flatMap{list => IO{println(s"sum: ${list.foldLeft(0)(_ + _)}")}}.as(ExitCode.Success) } } !16
  • 17. Cats IO in the real world The following libraries use the Cats IO Monad • Doobie • Http4s • Sttp • Monix • FS2 • PureConfig !17