Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Effectful Android apps with ArrowFx

Effectful Android apps with ArrowFx

Alberto Ballano

December 21, 2019
Tweet

More Decks by Alberto Ballano

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 47deg.com About impure functions Alberto Ballano #dcMadrid19 Luckily we're in

    good hands since a while: - Streams: Observable<T>/Flow<A>/etc - Android-specific streams: LiveData<A> - Concurrent wrappers: Job/Deferred<A> - Advanced concurrent wrappers: IO<A> All those tools and frameworks (wrappers) and many more keep us away from uncontrolled side effects. 6
  4. 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<A>, 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.
  5. 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!
  6. 47deg.com Arrow's IO Alberto Ballano #dcMadrid19 IO can wrap (suspend)

    effects either in its constructor or with the effect function: 11
  7. 47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 •

    parMapN: runs effects in parallel non-blocking waiting for all results to complete 15
  8. 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
  9. 47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 •

    async & cancelable: used to integrate with existing frameworks that have asynchronous calls. 18
  10. 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
  11. 47deg.com Tricks and techniques with IO Alberto Ballano #dcMadrid19 •

    bracket: A try-with-resources for asynchronous operations 20
  12. 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...
  13. 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
  14. 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
  15. 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
  16. 47deg.com Polymorphic Arrow Fx Alberto Ballano #dcMadrid19 • Arrow Fx

    also supports polymorphism (mainly for libs): 26
  17. 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
  18. 47deg.com More from Arrow Alberto Ballano #dcMadrid19 And more to

    come! IO<E, A>, Streams, Android module… Stay tuned! 30
  19. 47deg.com Big reveal about FP Alberto Ballano #dcMadrid19 You don't

    need to know functional programming to benefit from it.
  20. 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.
  21. 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
  22. 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
  23. 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