Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

This presentation introduces the viewers to Functional Programming, concept of Pure function, Functional Programming Language, Functional Constructs of Kotlin, Limitations of Kotlin, Introduction to Arrow, Deep-dive into Arrow's data types-Option, Either, Try[with examples and use cases], Deep-dive into Arrow's Type classes - Functor, Applicative, Monad[with examples and how they work], Relation between Functor, Applicative and Monad, More FP concepts- Function composition, Piping, Logical complement, Memoization, Integration of Arrow with Android Studio project, Sample Arrow apps and Best practices when trying to build with Arrow.

Monika Kumar Jethani

October 14, 2018
Tweet

More Decks by Monika Kumar Jethani

Other Decks in Technology

Transcript

  1. Leveraging the power of Arrow to build Android(Kotlin) apps using

    Functional Programming MONIKA KUMAR JETHANI
  2. 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
  3. 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
  4. 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.
  5. 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.
  6. 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.
  7. Functional Constructs of Kotlin  Immutability  Higher-Order Functions 

    Lambdas  Lazy Evaluation  Tail Call Recursion  Extension Functions  Null Safety  Pattern Matching
  8. 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
  9. 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.
  10. 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.
  11. 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.
  12. 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.
  13. 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.
  14. 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.
  15. 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
  16. How Functor works? fmap :: (a -> b) -> f(a)

    -> f(b) fmap takes a Function And a Functor And returns a new Functor
  17. 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.
  18. Functor applied to Array (+3) (+3) (+3) 2 4 6

    5 7 9 Apply the function to each value.
  19. 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
  20. Applicative Builder Preserves Type Information when computing over different Option

    types. Callback is invoked when all values are completed, if all values are Some.
  21. Monads Monads apply a wrapped value to a function that

    returns a wrapped value using flatMap.
  22. How does Monad work? (>>=) :: ma -> (a ->

    mb) -> mb >>= takes a Monad And a function that returns a Monad And it returns a Monad
  23. 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.
  24. Piping  Reverse of ‘compose’.  Takes a value from

    the function on the left and sequentially applies it to the function on the right.
  25. 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.
  26. 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.
  27. 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/