Intro to Functional Programming

581 views

Published on

associated Clojure code
https://github.com/firthh/intro-fp

Published in: Software, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
581
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Intro to Functional Programming

  1. 1. FUNCTIONAL PROGRAMMING Hugo Firth @hugo_firth 1
  2. 2. WHAT I’LL BE TALKING ABOUT 2 ▫︎Comparison of different paradigms ▫︎What is functional programming? ▫︎Purity ▫︎Laziness ▫︎Concurrency
  3. 3. WHAT I WON’T BE TALKING ABOUT 3 ▫︎Functors ▫︎Monads ▫︎Category theory ▫︎Type theory
  4. 4. Clojure
  5. 5. CLOJURE 5 ▫︎Lisp (funny syntax) ▫︎Runs on the JVM ▫︎No classes, just functions ▫︎Supports the functional paradigm
  6. 6. DIFFERENT PARADIGMS 6 ▫︎Machine code ▫︎Procedural ▫︎Object Oriented ▫︎Functional ▫︎Multi-paradigm Many others such as logic and symbolic programming
  7. 7. DIFFERENT PARADIGMS 7 ▫︎Machine code ▫︎Procedural ▫︎Object Oriented ▫︎Functional ▫︎Multi-paradigm Many others such as logic and symbolic programming
  8. 8. PROCEDURAL BASIC and C are the most notable languages here Gave us basic looping constructs and subroutines Why? To abstract away from GOTO statements 8
  9. 9. OBJECT ORIENTED Many languages here Java, C#, C++, etc. 
 Gave us Classes and Objects 
 Why? To abstract away from global state 9
  10. 10. FUNCTIONAL Increasingly large number of languages including Clojure, Scala and Haskell Gave us Functions 
 Why? Remove all state 10
  11. 11. What is FP
  12. 12. DEFINITION A style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data 12http://en.wikipedia.org/wiki/Functional_programming
  13. 13. PURITY
  14. 14. PURITY? 14 Same arguments in == same result No side effects! Ever!
  15. 15. WHY? 15 Easier to design, test and understand Referential transparency 
 -> optimisations 
 -> laziness
  16. 16. WHAT DOESN’T THIS LOOK LIKE
  17. 17. IMPURE 17 function calculateTax(amount) { var taxRate = db.getTaxRate(); return taxRate / amount * 100; }
  18. 18. IMPURE 18 function calculateTax(amount, taxRate) { db.saveTaxRate(taxRate); return taxRate / amount * 100; }
  19. 19. PURE 19 function calculateTax(amount, taxRate) { return taxRate / amount * 100; }
  20. 20. PURE 20 var tax = calculateTax(100, 10); // tax = 10
  21. 21. WHAT DOESN’T THIS LOOK LIKE? 21 for (int i = 0; i < count; i++) { //do something }
  22. 22. WHAT DOESN’T THIS LOOK LIKE? 22 while(someCondition) { //do something }
  23. 23. WHAT DOESN’T THIS LOOK LIKE? 23 foreach(var something in somethings) { //do something }
  24. 24. HOW DO YOU DO ANYTHING?
  25. 25. HIGHER ORDER FUNCTIONS A higher order function is one that takes one or more functions as input, or returns a function. 25
  26. 26. IMPURE 26 var numbers = [1, 2, 3, 4, 5]; for(var i = 0; i < numbers.length; i++) { numbers[i] = numbers[i] * 2; } //numbers => [2, 4, 6, 8, 10]
  27. 27. IMPURE 27 var numbers = [1, 2, 3, 4, 5]; var destination = []; for(var i = 0; i < numbers.length; i++) { var result = numbers[i] * 2; destination.push(result); } //destination => [2, 4, 6, 8, 10]
  28. 28. MAP (HIGHER ORDER FUNCTION) Map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results 28
  29. 29. FILTER (HIGHER ORDER FUNCTION) Filter is a higher-order function that processes a collection to produce a new collection containing exactly those elements of the original collection for which a given predicate returns true 29
  30. 30. FOLD / REDUCE / AGGREGATE A family of higher-order functions that analyse a recursive data structure and recombine through use of a given combining operation the results of recursively processing its constituent parts, building up a return value 30
  31. 31. HIGHER ORDER FUNCTIONS 31 Recursion and higher order functions are the two constructs that allow us to do anything in a functional language
  32. 32. DECLARATIVE PROGRAMMING
  33. 33. WHAT IS THIS DOING!?! 33 var n = 1; var num_elements = 0; var sum_of_first_10 = 0; while (num_elements < 10) { if (n^2 % 5 == 0) { sum_of_first_10 += n; num_elements += 1; } n += 1; } //sum_of_first_10 => 225
  34. 34. Clojure example 34
  35. 35. WHY? 35 Readability - describe what you want! Not how to get it
  36. 36. WHY? 36 Abstract away concepts of iteration, transformation, filtering, and accumulation. Write functions to deal with elements not a sequence.
  37. 37. WHY? 37 Maintainability - Small independently testable functions
  38. 38. LAZINESS
  39. 39. RANGE Return infinite sequence of numbers 39
  40. 40. REPEAT Returns a lazy infinite sequence of supplied items 40
  41. 41. CYCLE Returns a lazy infinite sequence of items in a collection 41
  42. 42. Clojure example 42
  43. 43. WHY? 43 Code is not evaluated until you need it. Code that doesn’t need to be evaluated won’t be.
  44. 44. IMMUTABILITY
  45. 45. IMMUTABLE CLASS 45 public class Dollars { private double value; public Dollars(double value) { this.value = value; } public double getValue() { return this.value; } }
  46. 46. 46 public class Dollars { private double value; public Dollars(double value) { this.value = value; } public double getValue() { return this.value; } public void add(double value) { this.value += value; } } MUTABLE CLASS
  47. 47. 47 IMMUTABLE CLASS public class Dollars { private double value; public Dollars(double value) { this.value = value; } public double getValue() { return this.value; } public Dollars add(double value) { return new Dollars(this.value + value); } }
  48. 48. 48 IMMUTABLE COLLECTIONS (THEY’RE HARD)
  49. 49. 49 IMMUTABLE COLLECTIONS Empty collection: [ ] //or null
  50. 50. 50 IMMUTABLE COLLECTIONS 1 element collection: var element = x; //and var restOfCollection = EMPTY_COLLECTION;
  51. 51. 51 IMMUTABLE COLLECTIONS 2 element collection: var element = y; //and var restOfCollection = collectionX;
  52. 52. 52 IMMUTABLE COLLECTIONS 3 element collection: var element = z; //and var restOfCollection = collectionY;
  53. 53. 53 IMMUTABLE COLLECTIONS IN PICTURES
  54. 54. 54 IMMUTABLE COLLECTIONS EMPTY COLLECTION [ ]
  55. 55. 55 IMMUTABLE COLLECTIONS EMPTY COLLECTION X [ X ]
  56. 56. 56 IMMUTABLE COLLECTIONS EMPTY COLLECTION X Y [ X, Y ]
  57. 57. 57 IMMUTABLE COLLECTIONS EMPTY COLLECTION X Y Z [ X, Y, Z ]
  58. 58. 58 IMMUTABLE COLLECTIONS LOOKS FAMILIAR RIGHT?
  59. 59. 59 IMMUTABLE COLLECTIONS EMPTY COLLECTION X Y Z Z2 [ X, Y, Z ] [ X, Y, Z2 ]
  60. 60. Clojure example 60
  61. 61. WHY? 61 Pass objects and collections to other functions with out fear of them changing
  62. 62. CONCURRENCY
  63. 63. Clojure example 63
  64. 64. WHY? 64 Concurrency is hugely important with multi- core processors Simplicity - data sharing across threads and processes is hard if it’s mutable
  65. 65. YOU CAN DO THIS IN ANY LANGUAGE!!!! ▫︎Try to write pure small pure methods/functions ▫︎Avoid state wherever you can ▫︎Separate impurities when they can’t be removed ▫︎Create immutable classes (value objects) ▫︎Look at simpler methods for concurrency 65

×