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. 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 <T>tryOrDefault(default: T, block:() ->T) =try { block() } catch(e:Exception) default }
  2. 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
  3. What is Pure Functional? Programming process To model behaviour, are

    verbs/actions Only operate on input, and provide output Limited to functions scope
  4. 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)
  5. Why Pure Functions? Everyone can call it Is simple business

    rules Easy to understand Easy to test Compose larger behaviours via smaller ones
  6. How do we compose? Be generic, use parametric types and

    functions Parametric types are in containers List<A> List is an Abstract Data Type(ADT)
  7. 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
  8. 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
  9. Algebraic Data Types Not operating on values, but types sealed

    class MyBoolean{ fun and(other:MyBoolean):MyBoolean class MyTrue:MyBoolean(){...} class MyFalse:MyBoolean(){...} }
  10. 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
  11. 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
  12. 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
  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 The domain algebra is decoupled from the projects implementation Ad-hoc polymorphism (soon-ish KEEP#87)
  14. Usage and To Solve fun <AbstractionForNullableType, AbstractionForOneOfTwoTypes> myDomainBehaviour(a:AbstractionForSequentialComputation, b:AbstractionForComputationContainingDependencies, c:(AbstractionForParallelComputation)

    -> AbstractionForValidation):AbstractionThatReturns<AList, FromInput, OfMyType<AbstractionDoesSequentialTryAndFailsOnFirstException>> { abstractionForIoOperation(abstractionForReactiveImplementation)(abstraction ForFrontEndUiEventRegardlessOfPlatform)
  15. 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