An Intro to Functional Programming

An Intro to Functional Programming

Abstract:
Functional Programming advocates a declarative paradigm of writing consistent, explicit & concurrent programs. Functions are treated as their mathematical counterparts, while avoiding side-effects and mutation of state.
We aim at discussing the need of building solutions inspired from the functional paradigm, writing better code & boosting performance of an application by efficiently using computing resources.

80f9272a9ef400fb40f56fcd2e41b3fb?s=128

Gurpreet Singh

October 20, 2018
Tweet

Transcript

  1. @imGurpreetSK An Introduction to Functional Programming Why your functions should

    tell the true story
  2. @imGurpreetSK GurpreetSK.com Gurpreet Singh Android Engineer at Kite ~ 3

    years of Android ~ 1.5 years of Functional & Reactive programming Paradigms, TDD and well-named variables. Learning to write cleaner code and designing better systems.
  3. @imGurpreetSK

  4. @imGurpreetSK What is Functional Programming?

  5. @imGurpreetSK Programming with functions

  6. @imGurpreetSK What a Terrible Failure

  7. @imGurpreetSK Really!*

  8. @imGurpreetSK Really!* *conditions apply

  9. @imGurpreetSK It’s a paradigm

  10. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs
  11. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs • No state changes
  12. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs • No state changes • No explicit* mutation
  13. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs • No state changes • No explicit* mutation • Operation at a higher level of abstraction: What > How
  14. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs • No state changes • No explicit* mutation • Operation at a higher level of abstraction: What > How • Data & computation > actions to be performed
  15. @imGurpreetSK

  16. @imGurpreetSK Let’s break it down

  17. @imGurpreetSK • No state changes • No explicit* mutation •

    Operation at a higher level of abstraction: What > How • Data & computation > actions to be performed It’s a way of writing declarative functions, inspired from mathematical constructs
  18. @imGurpreetSK

  19. @imGurpreetSK fb.com/garbagebin

  20. @imGurpreetSK

  21. @imGurpreetSK fun doubleEvenNumbers( input: ArrayList<Int> ): ArrayList<Int> { var squares

    = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares }
  22. @imGurpreetSK fun doubleEvenNumbers( input: ArrayList<Int> ): ArrayList<Int> { var squares

    = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares }
  23. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): ArrayList<Int> { var squares

    = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares }
  24. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } .also { println(it) } }
  25. @imGurpreetSK return input .filter { it % 2 == 0

    } .map { it * it } .also { println(it) } var squares = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares Imperative Functional
  26. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs • No state changes • No explicit* mutation • Operation at a higher level of abstraction: What > How • Data & computation > actions to be performed
  27. @imGurpreetSK What’s a function?

  28. @imGurpreetSK

  29. @imGurpreetSK y = f(x)

  30. @imGurpreetSK y = f(x) Function

  31. @imGurpreetSK y = f(x) Input Function

  32. @imGurpreetSK y = f(x) Output Value Function Input

  33. @imGurpreetSK y = f(x) Initialisation Function Output Value Input

  34. @imGurpreetSK Remember the two solutions before?

  35. @imGurpreetSK return input .filter { it % 2 == 0

    } .map { it * it } .also { println(it) } var squares = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares Imperative Functional
  36. @imGurpreetSK return input .filter { it % 2 == 0

    } .map { it * it } .also { println(it) } var squares = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares Imperative Functional
  37. @imGurpreetSK return input .filter { it % 2 == 0

    } .map { it * it } .also { println(it) } var squares = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares Functional Mutable Side-effect Imperative
  38. @imGurpreetSK return input .filter { it % 2 == 0

    } .map { it * it } .also { println(it) } var squares = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares Imperative Functional Side-effect
  39. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional
  40. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 1. Pure; No side effects
  41. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 1. Pure; No side effects 2. No state manipulation
  42. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 2. No state manipulation 1. Pure; No side effects
  43. @imGurpreetSK What is State?

  44. @imGurpreetSK a ++

  45. @imGurpreetSK a = a + 1 Yours truly

  46. @imGurpreetSK a = a + 1 Operations?

  47. @imGurpreetSK a = a + 1 Operations? 1. Read

  48. @imGurpreetSK a = a + 1 Operations? 1. Read 2.

    Modify
  49. @imGurpreetSK a = a + 1 Operations? 1. Read 2.

    Modify 3. Write
  50. @imGurpreetSK a = a + 1 Is this thread safe?

  51. @imGurpreetSK a = a + 1 Is this thread safe?

  52. @imGurpreetSK a = a + 1 Is this thread safe?

    It has a temporal dependency
  53. @imGurpreetSK a = a + 1 System is now time

    dependent
  54. @imGurpreetSK State is the current value of a variable in

    the system
  55. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 1. Pure, No side effects 2. No state manipulation
  56. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 2. No state manipulation 3. No explicit mutation 1. Pure, No side effects
  57. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 4. Emphasis on flow of data 2. No state manipulation 3. No explicit mutation 1. Pure, No side effects
  58. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 5. Higher level of abstraction 4. Emphasis on flow of data 2. No state manipulation 3. No explicit mutation 1. Pure, No side effects
  59. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs • No state changes • No explicit* mutation • Operation at a higher level of abstraction: What > How • Data & computation > actions to be performed
  60. @imGurpreetSK return input .filter { it % 2 == 0

    } .map { it * it } .also { println(it) } var squares = ArrayList<Int>() for (i in 0 until input.size) { if (input[i] % 2 == 0) { squares.add(i * i) } } println(squares) return squares Imperative Functional Why should I use it?
  61. @imGurpreetSK

  62. @imGurpreetSK Some facts

  63. @imGurpreetSK #1 Writing readable code is hard

  64. @imGurpreetSK #2 Readable code is maintainable code

  65. @imGurpreetSK #3 Systems get tightly coupled over time, making it

    harder to make changes
  66. @imGurpreetSK

  67. @imGurpreetSK “Hot-Fixes”

  68. @imGurpreetSK #5 impacting testability, durability & reviews of code

  69. @imGurpreetSK Of-course there are things we might not *really* think

    about at first: • Multi-threaded environments • Time dependency of code • Modularisation • Dependency management • A stable architecture • Testing (all kinds of it), and more…
  70. @imGurpreetSK We should change because

  71. @imGurpreetSK 1. Functional programs are simpler to understand

  72. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } }
  73. @imGurpreetSK 1. Functional programs are simpler to understand => Cleaner,

    concise code
  74. @imGurpreetSK 1. Functional programs are simpler to understand => Cleaner,

    concise code => More efficient code reviews
  75. @imGurpreetSK 1. Functional programs are simpler to understand => Cleaner,

    concise code => More efficient code reviews => Cleaner git history
  76. @imGurpreetSK 1. Functional programs are simpler to understand => Cleaner,

    concise code => More efficient code reviews => Cleaner git history => Better dependency management
  77. @imGurpreetSK 1. Functional programs are simpler to understand => Cleaner,

    concise code => More efficient code reviews => Cleaner git history => Better dependency management => Happier you, happier coworkers
  78. @imGurpreetSK 1. Functional programs are simpler to understand 2. and

    easier to write
  79. @imGurpreetSK 1. Functional programs are simpler to understand 2. and

    easier to write 3. Order of execution doesn’t matter
  80. @imGurpreetSK 1. Functional programs are simpler to understand 2. and

    easier to write 3. Order of execution doesn’t matter 4. Fewer concurrency issues
  81. @imGurpreetSK 1. Functional programs are simpler to understand 2. and

    easier to write 3. Order of execution doesn’t matter 4. Fewer concurrency issues 5. More scalable solutions
  82. @imGurpreetSK 1. Functional programs are simpler to understand 2. and

    easier to write 3. Order of execution doesn’t matter 4. Fewer concurrency issues 5. More scalable solutions assertEquals(f(x), f(x)) gives no surprises => Freedom from “What’s the state?”
  83. @imGurpreetSK but above all

  84. @imGurpreetSK More time to sleep

  85. @imGurpreetSK Resources/People • Erik Meijer • Rich Hickey • Uncle

    Bob • Venkat Subramaniam • Functional Geekery (Podcast) • This video -> https: //goo.gl/y9JM7Y • https: //wiki.haskell.org/Introduction
  86. @imGurpreetSK Thank you! www.GurpreetSK.com