Slide 1

Slide 1 text

47deg.com Effectful Android apps with ArrowFx suspend all effects! Alberto Ballano #dcMadrid19

Slide 2

Slide 2 text

47deg.com Speaker Alberto Ballano 2

Slide 3

Slide 3 text

47deg.com About impure functions Traditional effects

Slide 4

Slide 4 text

47deg.com About impure functions Alberto Ballano #dcMadrid19 - Impure functions, or side-effects, are functions that are not pure: - Pure functions are deterministic: same input -> same output - Pure functions do not cause observable changes in the "external world" (db, network, ui, etc) - If you build an app using side effects -> bugs and harder ability to reproduce them 4

Slide 5

Slide 5 text

47deg.com About impure functions Alberto Ballano #dcMadrid19 Effects can't be avoided, but we can use certain tools handle them better. Also we want to build apps with effects that: ● Can handle errors: For determinism ● Can switch threads: To not block the main thread ● Can run scoped: To not leak resources 5

Slide 6

Slide 6 text

Slide 7

Slide 7 text

47deg.com Effectful apps with Arrow Controlled effects

Slide 8

Slide 8 text

47deg.com Effectful apps with Arrow Alberto Ballano #dcMadrid19 In Arrow, we use suspend fun to denote a function that may cause side effects when invoked. But, why suspend? The Kotlin compiler disallows the invocation of suspended functions in a non-execution environment because suspend requires declaration inside another suspended function or a continuation. A continuation is a Kotlin Interface, Continuation, that proves we know how to handle success and error cases resulting from running the suspended effect. This means that we cannot call suspend functions without handling result and error cases.

Slide 9

Slide 9 text

47deg.com Effectful apps with Arrow Alberto Ballano #dcMadrid19 The suspend keyword gives us a basic level of concurrency and a safe wrapper for effects, from which we can start building our apps. 9 But, you can't run suspend functions without a concurrent runtime!

Slide 10

Slide 10 text

47deg.com An effects wrapper Alberto Ballano #dcMadrid19 10

Slide 11

Slide 11 text

47deg.com Arrow's IO Alberto Ballano #dcMadrid19 IO can wrap (suspend) effects either in its constructor or with the effect function: 11

Slide 12

Slide 12 text

47deg.com Arrow's IO Alberto Ballano #dcMadrid19 12

Slide 13

Slide 13 text

47deg.com Arrow's IO Alberto Ballano #dcMadrid19 13

Slide 14

Slide 14 text

47deg.com Tricks and techniques with IO Controlled effects

Slide 15

Slide 15 text

47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 ● parMapN: runs effects in parallel non-blocking waiting for all results to complete 15

Slide 16

Slide 16 text

47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 16

Slide 17

Slide 17 text

47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 ● raceN: runs effects in parallel non-blocking waiting for the first result to complete 17

Slide 18

Slide 18 text

47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 ● async & cancelable: used to integrate with existing frameworks that have asynchronous calls. 18

Slide 19

Slide 19 text

47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 ● async & cancelable: used to integrate with existing frameworks that have asynchronous calls. 19 Just an example

Slide 20

Slide 20 text

47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 ● bracket: A try-with-resources for asynchronous operations 20

Slide 21

Slide 21 text

47deg.com The declarative style caveat Alberto Ballano #dcMadrid19 One problem of wrappers is that, when composed, you sacrifice readability: 21 So, ideally, we would like to use them in a direct way...

Slide 22

Slide 22 text

47deg.com Effectful apps with Arrow Fx Controlled effects in direct-style

Slide 23

Slide 23 text

47deg.com Arrow Fx Alberto Ballano #dcMadrid19 ● Arrow Fx is a purely functional concurrent effects library built on top of Arrow. ● Emphasis in empowering simple and declarative effectful programming for everyone. 23

Slide 24

Slide 24 text

47deg.com Arrow Fx with Meta Alberto Ballano #dcMadrid19 ● Arrow Fx is a purely functional concurrent effects library built on top of Arrow. ● Emphasis in empowering simple and declarative effectful programming for everyone. 24

Slide 25

Slide 25 text

47deg.com Arrow Fx for other datatypes Alberto Ballano #dcMadrid19 ● Arrow Fx can be used for many other data types, also for integrations: Option, Either, ObservableK, DeferredK, etc 25

Slide 26

Slide 26 text

47deg.com Polymorphic Arrow Fx Alberto Ballano #dcMadrid19 ● Arrow Fx also supports polymorphism (mainly for libs): 26

Slide 27

Slide 27 text

47deg.com What about coroutines?

Slide 28

Slide 28 text

47deg.com ● Single API: No need to remember to use A or B depending on the concurrency problem ● Lazy by default: Nothing is executed until explicitly run, prevents ordering problems ● Wraps all recoverable errors: No try-catch needed ● Cancelable: Not necessary to check if cancelation happened ● No child-sibling issues: Unless explicitly avoided, exceptions are always propagated up IO vs Coroutines Alberto Ballano #dcMadrid19

Slide 29

Slide 29 text

47deg.com But Arrow is much more A complete toolbox 29

Slide 30

Slide 30 text

47deg.com More from Arrow Alberto Ballano #dcMadrid19 And more to come! IO, Streams, Android module… Stay tuned! 30

Slide 31

Slide 31 text

47deg.com And it all comes in a modular fashion

Slide 32

Slide 32 text

47deg.com Arrow's modularity Just pick what you need: Alberto Ballano #dcMadrid19

Slide 33

Slide 33 text

47deg.com Let's break a myth about functional programming 33

Slide 34

Slide 34 text

47deg.com Big reveal about FP Alberto Ballano #dcMadrid19 You don't need to know functional programming to benefit from it.

Slide 35

Slide 35 text

47deg.com About Arrow and FP Alberto Ballano #dcMadrid19 ● Functional programming is all about a series of guides and good practices pretty much like object oriented programming, but this doesn't mean you have to follow it all ● Y'all been using FP for a while, specially with Kotlin and the stdlib: map, flatMap, fold, immutability, high order functions, etc. ● Using Arrow doesn't necessarily mean getting rid of other tools, it can complement them. ● As always, evaluate each tool based on your project needs with pragmatism.

Slide 36

Slide 36 text

47deg.com Recap

Slide 37

Slide 37 text

47deg.com Quick recap Alberto Ballano #dcMadrid19 ● Side effects/impure functions are bad, so we mark them with suspend to avoid wrong usage ● IO lazily wraps suspend functions to allow us to handle errors, switch threads, run scoped and finally execute them. It also offers a couple more operators for specific needs. ● Arrow Fx lets us run chained-style calls as direct-style for IO and other types ● Arrow provides many tools for different needs, and because is modular you can choose which ones to use ● You don't need to understand category theory, neither know what a Monad is, to benefit from functional programming

Slide 38

Slide 38 text

47deg.com 38 Thanks to Jetbrains, the whole Arrow community and all of you for being here! ● Arrow docs and samples https://arrow-kt.io/ ● Simon's talk about Fx & Meta https://www.youtube.com/watch?v=DaognWtZCbs&t=3s ● Paco's post about ArrowFx https://www.pacoworks.com/2019/12/15/kotlin-coroutines-with-arrow-fx/ ● Amanda and Rachel's talk at KotlinConf https://www.youtube.com/watch?v=n9smQNxUyBI Links and resources Alberto Ballano #dcMadrid19

Slide 39

Slide 39 text

47deg.com Thank you! Wanna get in touch with us? Github => https://github.com/arrow-kt Slack => https://slack.kotlinlang.org (arrow channels) Gitter => https://gitter.im/arrow-kt/Lobby Or just ping us in Twitter @arrow_kt