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
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
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
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.
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!
a purely functional concurrent effects library built on top of Arrow. • Emphasis in empowering simple and declarative effectful programming for everyone. 23
Fx is a purely functional concurrent effects library built on top of Arrow. • Emphasis in empowering simple and declarative effectful programming for everyone. 24
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
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.
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
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
=> 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