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

OOP To FP

OOP To FP

Slides for my talk at Droidcon Berlin 2019

Adnan A M

July 03, 2019
Tweet

More Decks by Adnan A M

Other Decks in Programming

Transcript

  1. What does it mean to be functional ? 4 Declarative/Explicit

    4 Immutability/Concurrency 4 Purity 4 Referential Transparency 4 State Isolation droidcon Berlin 2019 7
  2. Declarative vs Imperative 4 I see an empty table at

    the end of the room. I and my spouse are going to walk over there and sit down 4 Table for two please droidcon Berlin 2019 9
  3. Declarative vs Imperative Imperative focuses on the How val array

    = listOf(1,2,3,4) var sum = 0 var odds: MutableList<Int> = listOf() for (element in array) { sum += element if (element % 2 == 1) { odds.add(element) } } droidcon Berlin 2019 10
  4. Declarative vs Imperative Declarative focuses on the What val array

    = listOf(1,2,3,4) val sum = array.reduce { sum, i -> sum + i } val odds = array.filter { i -> i % 2 == 1 } droidcon Berlin 2019 11
  5. Explicit 4 A function must always have a well defined

    return type 4 Avoid void/Unit as return types droidcon Berlin 2019 12
  6. Explicit fun multiplyBy5(number: Int): Int = { /** Some complex

    Math **/ } fun asyncCall(callback: CustomListener) = { /** Some IO operation and return via callback **/ } droidcon Berlin 2019 13
  7. Pure Functions 4 Produce same output for same inputs 4

    Rely only on input parameters 4 Do not have any side effects droidcon Berlin 2019 17
  8. Side Effects Modifying anything outside the scope or requiring external

    context. Examples : 4 Writing to a file/database 4 Making a network request 4 Modifying variables/state droidcon Berlin 2019 18
  9. Pure Functions - Sample val z = 10; fun add(x:

    Int, y: Int): Int { return x + y; } droidcon Berlin 2019 19
  10. Pure Functions - Sample Most useful Pure Functions must take

    at least 1 parameter fun justConstant(): Int { return 1336; } droidcon Berlin 2019 20
  11. Pure Functions - Sample Most useful Pure Functions must return

    a value fun noReturn(x:Int, y:Int) { val z = x + y } droidcon Berlin 2019 21
  12. Benefits of Pure Functions 4 Unit Testing 4 Paralellization 4

    Ordering 4 Memoization droidcon Berlin 2019 24
  13. Parallelization Run operations over multiple threads.... Ex: A parallel map

    implementation can distribute values over multiple threads and collect the results droidcon Berlin 2019 25
  14. Memoization Since O/P's don't change for same I/P's we can

    cache and re-use their results droidcon Berlin 2019 27
  15. No Side Effects ? Functional Programming doesn't eliminate Side Effects,

    but contains them and separates them out. droidcon Berlin 2019 29
  16. User Profile public class User { private String Name; private

    String email; public String getName() { return Name; } public void setName(String name) { Name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } droidcon Berlin 2019 34
  17. FP is not opp of OOP ! Imperative shell on

    the outside with an FP core on the inside droidcon Berlin 2019 36
  18. The 30 Day Challenge No loops No var (Use with

    caution) No if droidcon Berlin 2019 42
  19. # 1 It isn't an all or nothing game !

    droidcon Berlin 2019 44
  20. Either Type // Old Exception Style fun parse(s: String): Int

    = if (s.matches(Regex("-?[0-9]+"))) s.toInt() else throw NumberFormatException("$s is not a valid integer.") // Either Style fun parse(s: String): Either<NumberFormatException, Int> = if (s.matches(Regex("-?[0-9]+"))) Either.Right(s.toInt()) else Either.Left(NumberFormatException("$s is not a valid integer.")) droidcon Berlin 2019 51
  21. Either Type val x = parse("2") val value = when(x)

    { is Either.Left -> when (x.a){ is NumberFormatException -> "Not a number!" else -> "Unknown error" } is Either.Right -> "Got Number: ${x.b}" } droidcon Berlin 2019 52
  22. Option Type // Some value contained val someValue: Option<String> =

    Some("I am wrapped in something") //Empty value val emptyValue: Option<String> = None // Call Site val someValue: Option<Double> = Some(20.0) val value = when(someValue) { is Some -> someValue.t is None -> 0.0 } val number: Option<Int> = Some(3) val noNumber: Option<Int> = None val mappedResult1 = number.map { it * 1.5 } val mappedResult2 = noNumber.map { it * 1.5 } droidcon Berlin 2019 53
  23. IO Computation fun getDataFromNetwork(): IO<Either<CustomError, List<User>>> { /** .... **/}

    IO computation which returns either an error or a list of users droidcon Berlin 2019 54
  24. References/Credits 4 47 Degrees - Arrow-kt 4 Charles Scalfani -

    So You Want To Be A Functional Programmer droidcon Berlin 2019 55
  25. May The Functions Be With You ! 4 Reach out

    to me at @AdnanM0123 4 Talk about OOP/FP or droidcon India or anything Android droidcon Berlin 2019 56