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

Functional Reactive And Arrow-kt

Functional Reactive And Arrow-kt

Pure functional programming using Kotlin

Ersin Ertan

January 29, 2018
Tweet

More Decks by Ersin Ertan

Other Decks in Programming

Transcript

  1. Functional Reactive and
    Arrow-kt
    Lightning Talk
    Jan 29 2018
    github.com/
    ersin-ertan

    View full-size slide

  2. Android/Java/Kotlin can functional w libs, code
    google/autovalue - immutable value-type
    google/dagger - compile-time dependency injection
    evant/auto-value-lens - modify composed values
    twitter/
    inline fun tryOrDefault(default: T, block:() ->T) =try { block() } catch(e:Exception) default }

    View full-size slide

  3. What is Arrow?
    Companion to Kotlin's standard library enabling pure functional programming
    Not a new concept, but new to Kotlin/Android
    Pure Functional is to domain modeling as Kotlin native is to platform

    View full-size slide

  4. What is Pure Functional?
    Programming process
    To model behaviour, are verbs/actions
    Only operate on input, and provide output
    Limited to functions scope

    View full-size slide

  5. How do we work with Pure Functional?
    Reduce behaviour
    Use domain vocabulary
    Use immutable objects, state change requires a new object
    fun addOne(a:A) = A(a.num + 1)

    View full-size slide

  6. Why Pure Functions?
    Everyone can call it
    Is simple business rules
    Easy to understand
    Easy to test
    Compose larger behaviours via smaller ones

    View full-size slide

  7. How do we compose?
    Be generic, use parametric types and functions
    Parametric types are in containers
    List
    List is an Abstract Data Type(ADT)

    View full-size slide

  8. Can my ADT compose/operate with your ADT?
    Yes

    View full-size slide

  9. Can my ADT compose/operate with your ADT?
    Yes, because we use Arrow’s abstractions
    They are generic container classes specifically to do this
    Arrow is modeling the domain of Category Theory

    View full-size slide

  10. What is Category Theory?
    The mathematics of composition/morphisms
    Conceptual framework for structure/system of structures
    Has laws(associativity, identity)
    Arrows types(abstractions) obey these laws
    Operate on sets

    View full-size slide

  11. Algebraic Data Types
    Not operating on values, but types
    sealed class MyBoolean{
    fun and(other:MyBoolean):MyBoolean
    class MyTrue:MyBoolean(){...}
    class MyFalse:MyBoolean(){...}
    }

    View full-size slide

  12. when(myBoolean){
    is MyTrue -> {...}
    is MyFalse -> {...}
    }
    Compiler verifies type of our Algebraic Data Types
    Verified when passing as function input/output
    Now have referential transparency

    View full-size slide

  13. Why?
    Logic is explicit, type safe/constrained
    The domain model is pure/encoded within the type system
    Project’s domain models(types/algebra) are a superset of the Arrows types/algebra
    We have a common language to describe computation

    View full-size slide

  14. Why?
    Logic is explicit, type safe/constrained
    The domain model is pure/encoded within the type system
    Project’s domain models(types/algebra) are a superset of the Arrows types/algebra
    We have a common language to describe computation
    The domain algebra is decoupled from the projects implementation
    Ad-hoc polymorphism

    View full-size slide

  15. Why?
    Logic is explicit, type safe/constrained
    The domain model is pure/encoded within the type system
    Project’s domain models(types/algebra) are a superset of the Arrows types/algebra
    We have a common language to describe computation
    The domain algebra is decoupled from the projects implementation
    Ad-hoc polymorphism (soon-ish KEEP#87)

    View full-size slide

  16. Usage and To Solve
    fun
    myDomainBehaviour(a:AbstractionForSequentialComputation,
    b:AbstractionForComputationContainingDependencies,
    c:(AbstractionForParallelComputation) ->
    AbstractionForValidation):AbstractionThatReturnsOfMyType> {
    abstractionForIoOperation(abstractionForReactiveImplementation)(abstraction
    ForFrontEndUiEventRegardlessOfPlatform)

    View full-size slide

  17. Where?
    http://arrow-kt.io/
    https://github.com/arrow-kt/arrow
    https://gitter.im/arrow-kt/Lobby
    https://github.com/Kotlin/KEEP/pull/87
    compile 'io.arrow-kt:arrow-effects-rx2:0.6.0' // optional
    compile 'io.arrow-kt:arrow-effects-kotlinx-coroutines:0.6.0' // optional
    http://arrow-kt.io/docs/quickstart/blogs/ and in depth presentations
    http://arrow-kt.io/docs/quickstart/projects/
    https://github.com/JorgeCastilloPrz/ArrowAndroidSamples Functional Architecture
    github.com/
    ersin-ertan

    View full-size slide