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

Deb19bb8408191bc86c90b4ade6ffacd?s=128

Ersin Ertan

January 29, 2018
Tweet

Transcript

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

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

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

    rules Easy to understand Easy to test Compose larger behaviours via smaller ones
  7. 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)
  8. Can my ADT compose/operate with your ADT? Yes

  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
  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
  11. Algebraic Data Types Not operating on values, but types sealed

    class MyBoolean{ fun and(other:MyBoolean):MyBoolean class MyTrue:MyBoolean(){...} class MyFalse:MyBoolean(){...} }
  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
  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
  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
  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)
  16. Usage and To Solve fun <AbstractionForNullableType, AbstractionForOneOfTwoTypes> myDomainBehaviour(a:AbstractionForSequentialComputation, b:AbstractionForComputationContainingDependencies, c:(AbstractionForParallelComputation)

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