Slide 1

Slide 1 text

Developing a principled approach to programming IO in Scala Raymond Tay

Slide 2

Slide 2 text

A little about me

Slide 3

Slide 3 text

A little bit more about me Contributed to cats, dotty, eff- monad, akka, akka-http, scala compiler.

Slide 4

Slide 4 text

Effects What is it? Anything but you are probably thinking about side-effect(s). Controlling how side-effects are effected is the purpose of the IO Monad.

Slide 5

Slide 5 text

Effects (Scala 2)

Slide 6

Slide 6 text

What is needed … - Sequencing computations - Stack safety - Support evaluation modes - Lazy - Asynchronous - Strict

Slide 7

Slide 7 text

What is needed … - Sequencing computations - Stack safety - Support evaluation modes - Lazy - Asynchronous - Strict

Slide 8

Slide 8 text

Stack execution

Slide 9

Slide 9 text

Trampoline

Slide 10

Slide 10 text

What is needed … - Sequencing computations - Stack safety - Support evaluation modes - Lazy - Asynchronous - Strict

Slide 11

Slide 11 text

Getting started with IO

Slide 12

Slide 12 text

What exactly is IO? A value of type IO[A] is a computation which, when evaluated, can perform effects before returning a value of type A IO values are pure, immutable values and thus preserves referential transparency, being usable in functional programming. An IO is a data structure that represents just a description of a side effectful computation.

Slide 13

Slide 13 text

IO Typeclass Hierarchy

Slide 14

Slide 14 text

Asynchronous & Cancelable

Slide 15

Slide 15 text

Asynchronous & Cancelable in action ===== Start =======> => Concurrent-[scala-execution-context-global-180] Yes:5 => Concurrent-[scala-execution-context-global-178] Yes:2 => Concurrent-[scala-execution-context-global-181] Yes:4 Task-5 is no longer active canceled Task-3 is no longer active canceled Task-4 is no longer active canceled Task-6 is no longer active canceled => Concurrent-[scala-execution-context-global-179] Yes:3 Task-2 is no longer active canceled Task-8 is no longer active canceled Task-7 is no longer active canceled Task-9 is no longer active canceled Task-10 is no longer active canceled Task-1 is no longer active canceled … => Concurrent-[scala-execution-context-global-179] Yes:8 Left(Hello!_2)

Slide 16

Slide 16 text

Asynchronous & Non-Cancelable

Slide 17

Slide 17 text

Asynchronous & Non-Cancelable ===== Start =======> => Async-[scala-execution-context-global-179] Yes:2 => Async-[scala-execution-context-global-179] Yes:3 … => Async-[scala-execution-context-global-179] Yes:4 => Async-[scala-execution-context-global-179] Yes:7 => Async-[scala-execution-context-global-179] Yes:8 Left(Hello!_2) ===== End =======>

Slide 18

Slide 18 text

Cancelable

Slide 19

Slide 19 text

Cancelable

Slide 20

Slide 20 text

Error Handling

Slide 21

Slide 21 text

Resource management

Slide 22

Slide 22 text

Safely Acquire & Release

Slide 23

Slide 23 text

Concurrent Modelling

Slide 24

Slide 24 text

Concurrent Datatypes (I) - Buffered Channel

Slide 25

Slide 25 text

Encoding the Datatype

Slide 26

Slide 26 text

Modelling Concurrent Algos

Slide 27

Slide 27 text

Buffered Channel in Action

Slide 28

Slide 28 text

Concurrent Datatypes (II) - Skip Channel

Slide 29

Slide 29 text

Skip Channel in action

Slide 30

Slide 30 text

Logging with Cats-effect

Slide 31

Slide 31 text

Setting the stage…

Slide 32

Slide 32 text

Logging …

Slide 33

Slide 33 text

Do I have to do this every time ?

Slide 34

Slide 34 text

Gotchas

Slide 35

Slide 35 text

Stack safety

Slide 36

Slide 36 text

Cancellation assumes no-blocking

Slide 37

Slide 37 text

Just because it compiles …

Slide 38

Slide 38 text

Thank You JCConf 2019 ! @RaymondTayBL @https:/ /www.linkedin.com/in/ raymondtayboonleong

Slide 39

Slide 39 text

References => code repo : https://github.com/raymondtay/JCConf_2019 => cats effect : https://typelevel.org/cats-effect => cats effect tutorial : https://typelevel.org/cats- effect/tutorial/tutorial.html => monix : http://monix.io/ => fs2 : https://fs2.io => log4cats : https://christopherdavenport.github.io/ log4cats/ => concurrent Haskell paper : https://www.microsoft.com/ en-us/research/wp-content/uploads/1996/01/concurrent- haskell.pdf