Slide 1

Slide 1 text

Leveraging the power of Arrow to build Android(Kotlin) apps using Functional Programming MONIKA KUMAR JETHANI

Slide 2

Slide 2 text

About Me

Slide 3

Slide 3 text

Agenda 1- Overview of Functional Programming 2- Functional Constructs of Kotlin 3- Introduction to Arrow 4- Deep dive into Arrow Data Types: Option, Either, Try 5- Deep dive into Arrow Type Classes: Functor, Monad, Applicative 6- More FP Concepts 7- Sample Apps

Slide 4

Slide 4 text

What is Functional Programming??? Functional Programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Its main focus is on “what to solve” in contrast to an imperative style where the main focus is “how to solve”. Source: Wikipedia

Slide 5

Slide 5 text

Defining a Pure Function Input Output F Characteristics of a Pure function: 1- Provides the same output for the same input at all times. 2- Works only on the parameters supplied to it, doesn’t modify anything outside function.

Slide 6

Slide 6 text

Advantages of Functional Programming over OOP  Brevity  Modularization of code  Easier to test  Code becomes predictable  Encourages safe programming by providing constructs such as immutability and pure functions.

Slide 7

Slide 7 text

Functional Programming Languages  Any Language that treats functions as first class citizens.  Functions can be passed to other functions, returned from other functions and functions can be assigned to a variable.  Examples: Scala, Haskell, Kotlin.

Slide 8

Slide 8 text

Functional Constructs of Kotlin  Immutability  Higher-Order Functions  Lambdas  Lazy Evaluation  Tail Call Recursion  Extension Functions  Null Safety  Pattern Matching

Slide 9

Slide 9 text

Limitations of Kotlin w.r.t. FP  Doesn’t support typed functional programming.  Doesn’t enforce FP.

Slide 10

Slide 10 text

Arrow  Functional Companion to Kotlin’s standard library.  Library that provides Typed Functional Programming in Kotlin.  Introduced in 2017.  Fusion of 2 most popular Kotlin libraries: Kategory and funcKTionale.  Documentation available at : https://arrow-kt.io/  Latest release: 0.7.3

Slide 11

Slide 11 text

Data Types  Option  Either  Try

Slide 13

Slide 13 text

More to Option

Slide 14

Slide 14 text

Option with getOrElse getOrElse() allow us to provide default value if None.

Slide 15

Slide 15 text

Use case for Option  A Registration form filled by users where entities like Name, Date-of-Birth are optional. Searching the user by name, would results in a type Optional.  Can also be used in when clause of Kotlin.

Slide 16

Slide 16 text

Either  As its name implies, Either uses either of the two values.  By convention, right hand side of Either holds successful values and the left side holds the exceptional case.

Slide 17

Slide 17 text

More on Either

Slide 18

Slide 18 text

Extraction of values of Either

Slide 19

Slide 19 text

Using getOrElse with Either getOrElse allows us to provide a default value for the left-side.

Slide 20

Slide 20 text

Use case for Either Displaying Validation Messages: A form contains valid patterns for email, phone, name. The input entered by the user can be either valid or invalid.

Slide 21

Slide 21 text

Try  Try checks if a particular value exists and then routes towards success or failure.  A Try instance where the operation has been successful, which is represented as Success  a Try instance where the computation has failed is represented with a Throwable, which represents Failure.

Slide 22

Slide 22 text

Extracting value of Try We can extract Try inner values using ‘when’ clause.

Slide 23

Slide 23 text

Using recover Recover allows us to handle the exception if it’s a Failure.

Slide 24

Slide 24 text

Use case for Try Catching exceptions- User pass two values to the function for divide. Either the function would return the result or throw an exception.

Slide 25

Slide 25 text

Type Classes  Functor  Monad  Applicative

Slide 26

Slide 26 text

Functor Allow us to apply a function to a wrapped value using map. Here, ‘f’ is a function transforming the wrapped value. And map() returns the transformed value wrapped in same context. Present at: Arrow.typeclasses.Functor

Slide 27

Slide 27 text

How Functor works? fmap :: (a -> b) -> f(a) -> f(b) fmap takes a Function And a Functor And returns a new Functor

Slide 28

Slide 28 text

How Functor works? Continued… 2 (+3) 2 5 5 Unwrap value from Context. Apply Function. Rewrap value in Context. No value. (+3) Don’t apply function. End up with Nothing.

Slide 29

Slide 29 text

Functor applied to Array (+3) (+3) (+3) 2 4 6 5 7 9 Apply the function to each value.

Slide 30

Slide 30 text

Functor applied to a function Source: https://hackernoon.com/kotlin-functors- applicatives-and-monads-in-pictures-part-1-3- c47a1b1ce251

Slide 31

Slide 31 text

Polymorphism with Functor

Slide 32

Slide 32 text

Applicative Allow you to apply a wrapped function to a wrapped value using apply.

Slide 33

Slide 33 text

How Applicative works? (+3) <*> 2 5 Function wrapped in a Context Value in a Context (+3) 2 Unwrap both and apply the function to the value New value in a context

Slide 34

Slide 34 text

Applying Applicative to an array of values Source: https://hackernoon.com/kotlin-functors-applicatives-and- monads-in-pictures-part-2-3-f99a09efd1ec

Slide 35

Slide 35 text

Applicative Builder Preserves Type Information when computing over different Option types. Callback is invoked when all values are completed, if all values are Some.

Slide 36

Slide 36 text

Monads Monads apply a wrapped value to a function that returns a wrapped value using flatMap.

Slide 37

Slide 37 text

How does Monad work? (>>=) :: ma -> (a -> mb) -> mb >>= takes a Monad And a function that returns a Monad And it returns a Monad

Slide 38

Slide 38 text

Monad Chaining

Slide 39

Slide 39 text

Monad Binding

Slide 40

Slide 40 text

An interesting relation Functor Applicative Monad

Slide 41

Slide 41 text

More Functional Programming Concepts  Function composition  Memoization  Pipes  Logical Complement

Slide 42

Slide 42 text

Function Composition  Process of building a function using existing functions.  Compose is an infix function.  Compose takes the value from the function on the right, applies it to the function on the left and continues to pass it to the left.

Slide 43

Slide 43 text

Piping  Reverse of ‘compose’.  Takes a value from the function on the left and sequentially applies it to the function on the right.

Slide 44

Slide 44 text

Logical Component  Takes any function that returns a Boolean type and negates it.

Slide 45

Slide 45 text

Memoization  A technique to cache results of pure functions.  A function that is memorized behaves as a normal function, but stores the result of previous computations associated with the parameters supplied to produce that result.

Slide 46

Slide 46 text

Integration of Arrow with Android Studio project Add dependencies

Slide 47

Slide 47 text

Calculator app using Arrows FP constructs

Slide 48

Slide 48 text

Calculator app using Arrows FP constructs

Slide 49

Slide 49 text

Modelling division as Try When Divisor is zero When Divisor is non-zero

Slide 50

Slide 50 text

Form validation using Arrow

Slide 51

Slide 51 text

Modelling data with Either When no input is provided When input is provided

Slide 52

Slide 52 text

Best practices when using Arrow for Android  Push side effects to a single point on your system to keep the rest of the architecture completely pure.  Be clear on the concepts of different data types and their differences.  Be clear on the concepts of different type classes and incorporate them in your computations accordingly.

Slide 53

Slide 53 text

Projects using Arrow  raulh82vlc/FunctionalKotlin  dcampogiani/AndroidFunctionalValidation  47deg/helios You can check the projects that are being done/have being done using Arrow here: https://arrow-kt.io/docs/quickstart/projects/

Slide 54

Slide 54 text

Are you READY to try out Arrow? THANK YOU For queries, please reach out to me at [email protected]