Share
Embed
Functional Reactive And Arrow-kt
Pure functional programming using Kotlin
Ersin Ertan
January 29, 2018
Transcript
17 slides
-
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 } -
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 -
What is Pure Functional? Programming process To model behaviour, are
verbs/actions Only operate on input, and provide output Limited to functions scope -
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) -
Why Pure Functions? Everyone can call it Is simple business
rules Easy to understand Easy to test Compose larger behaviours via smaller ones -
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) -
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 -
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 -
Algebraic Data Types Not operating on values, but types sealed
class MyBoolean{ fun and(other:MyBoolean):MyBoolean class MyTrue:MyBoolean(){...} class MyFalse:MyBoolean(){...} } -
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 -
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 -
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 -
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) -
Usage and To Solve fun <AbstractionForNullableType, AbstractionForOneOfTwoTypes> myDomainBehaviour(a:AbstractionForSequentialComputation, b:AbstractionForComputationContainingDependencies, c:(AbstractionForParallelComputation)
-> AbstractionForValidation):AbstractionThatReturns<AList, FromInput, OfMyType<AbstractionDoesSequentialTryAndFailsOnFirstException>> { abstractionForIoOperation(abstractionForReactiveImplementation)(abstraction ForFrontEndUiEventRegardlessOfPlatform) -
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