Share
Embed
Transcript
54 slides
-
Leveraging the power of Arrow to build Android(Kotlin) apps using
Functional Programming MONIKA KUMAR JETHANI -
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 -
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 -
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. -
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. -
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. -
Functional Constructs of Kotlin Immutability Higher-Order Functions
Lambdas Lazy Evaluation Tail Call Recursion Extension Functions Null Safety Pattern Matching -
Limitations of Kotlin w.r.t. FP Doesn’t support typed functional
programming. Doesn’t enforce FP. -
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 -
Option Option<A> is a container for an optional value of
type A. If the value of type A is present, the Option<A> is an instance of Some<A>, containing the present value of type A. If the value is absent, the Option<A> is the object None. -
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. -
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. -
Using getOrElse with Either getOrElse allows us to provide a
default value for the left-side. -
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. -
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> a Try instance where the computation has failed is represented with a Throwable, which represents Failure. -
Extracting value of Try We can extract Try inner values
using ‘when’ clause. -
Using recover Recover allows us to handle the exception if
it’s a Failure. -
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. -
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 -
How Functor works? fmap :: (a -> b) -> f(a)
-> f(b) fmap takes a Function And a Functor And returns a new Functor -
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. -
Functor applied to Array (+3) (+3) (+3) 2 4 6
5 7 9 Apply the function to each value. -
Applicative Allow you to apply a wrapped function to a
wrapped value using apply. -
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 -
Applicative Builder Preserves Type Information when computing over different Option
types. Callback is invoked when all values are completed, if all values are Some. -
Monads Monads apply a wrapped value to a function that
returns a wrapped value using flatMap. -
How does Monad work? (>>=) :: ma -> (a ->
mb) -> mb >>= takes a Monad And a function that returns a Monad And it returns a Monad -
More Functional Programming Concepts Function composition Memoization
Pipes Logical Complement -
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. -
Piping Reverse of ‘compose’. Takes a value from
the function on the left and sequentially applies it to the function on the right. -
Logical Component Takes any function that returns a Boolean
type and negates it. -
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. -
Modelling data with Option When no input is provided When
input is provided -
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. -
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/ -
Are you READY to try out Arrow? THANK YOU For
queries, please reach out to me at [email protected]