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

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.

Gurpreet Singh

October 20, 2018
Tweet

More Decks by Gurpreet Singh

Other Decks in Programming

Transcript

  1. @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.
  2. @imGurpreetSK It’s a way of writing declarative functions, inspired from

    mathematical constructs • No state changes • No explicit* mutation
  3. @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
  4. @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
  5. @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
  6. @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 }
  7. @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 }
  8. @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 }
  9. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } .also { println(it) } }
  10. @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
  11. @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
  12. @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
  13. @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
  14. @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
  15. @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
  16. @imGurpreetSK fun squareEvenNumbers( input: ArrayList<Int> ): List<Int> { return input

    .filter { it % 2 == 0 } .map { it * it } } Functional 1. Pure; No side effects
  17. @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
  18. @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
  19. @imGurpreetSK a = a + 1 Is this thread safe?

    It has a temporal dependency
  20. @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
  21. @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
  22. @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
  23. @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
  24. @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
  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 Why should I use it?
  26. @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…
  27. @imGurpreetSK 1. Functional programs are simpler to understand => Cleaner,

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

    concise code => More efficient code reviews => Cleaner git history => Better dependency management
  29. @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
  30. @imGurpreetSK 1. Functional programs are simpler to understand 2. and

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

    easier to write 3. Order of execution doesn’t matter 4. Fewer concurrency issues
  32. @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
  33. @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?”
  34. @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