Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1.  
  2. # functional programming
  3.  
  4. Endre Galaczi
  5.  
  6. 🐦 @galacziendre
  7. βœ‰οΈ galacziendre@gmail.com
  8.  
  9. # functional programming - intro
  10.  
  11. functional programming style
  12. evaluation of mathematical functions
  13. avoid changing-state and mutable data
  14.  
  15. # functional programming - first-class functions
  16.  
  17. var triple = function(n) {
  18. return n * 3;
  19. }
  20.  
  21. functions can be passed as arguments
  22.  
  23. # functional programming - higher-order functions
  24.  
  25. map, filter, reduce
  26.  
  27. var numbers = [1,2,3].map(triple)
  28.  
  29. instead of:
  30.  
  31. input = [1,2,3]
  32. numbers = []
  33. for(var i = 0; i<=numbers.length; i++){
  34. numbers.push(input[i])
  35. }
  36.  
  37. # functional programming - pure functions - no mutation, no assignment
  38.  
  39. var a = 5;
  40. a = a + 1
  41.  
  42. no mutating global state
  43.  
  44. function πŸ’© triple(n){
  45.  
  46. times_called = times_called + 1;
  47.  
  48. return n * 3;
  49. }
  50.  
  51. # functional programming - recursion
  52.  
  53. no mutation -> no loops
  54. no while
  55. no for
  56.  
  57. ✨ recursion ✨
  58.  
  59. function count_to_a_chicken(n){
  60. if(n==0) return "πŸ” ";
  61. else return count_to_chicken(n-1);
  62. }
  63.  
  64. # tail call optimisation
  65.  
  66. stack overflow
  67.  
  68. factorial 0 = 1
  69. factorial n = n * factorial n-1
  70.  
  71. factorial(10) =
  72. 10 * factorial(9) =
  73. 10 * (9 * factorial(8)) =
  74. 10 * (9 * (8 * factorial(7))) =
  75. 10 * (9 * (8 * (7 * factorial(6))) =
  76. ...
  77. 10 * (9 * (8 * (7 * (6 * (5 * (4 * (3 * (2 * 1))))))))
  78.  
  79.  
  80. # tail call optimisation
  81.  
  82. factorial 0 i = i
  83. factorial n i = factorial (n-1) (i*n)
  84.  
  85. factorial(4,1)=
  86. (factorial(3,4))=
  87. ((factorial(2,12)))=
  88. (((factorial(1,24))))=
  89. ((((factorial(0,24)))))=
  90. ((((24))))=
  91.  
  92. es6 tco
  93.  
  94. # functional programming - expressions over statements
  95.  
  96. encourages writing code using
  97. expressions rather than statements
  98.  
  99. int total = 0;
  100. for(int i = 1; i<=10; i++){
  101. total = total + 1;
  102. }
  103.  
  104. sum [1..10]
  105.  
  106. # functional programming - composition
  107.  
  108. hatch = 🍳 -> πŸ”
  109. clone = πŸ” -> πŸ” πŸ”
  110.  
  111. superhatcher x = clone(clone(hatch(x)))
  112. superhatcher = clone.clone.hatch
  113.  
  114. superhatcher 🍳 -> πŸ” πŸ” πŸ” πŸ”
  115.  
  116. # functional programming - composition
  117.  
  118. var triple = function(n) {
  119. return n * 3;
  120. }
  121. var numbers = [1,2,3].map(triple)
  122.  
  123.  
  124. input = [1,2,3]
  125. numbers = []
  126. for(var i = 0; i<=numbers.length; i++){
  127. numbers.push(input[i])
  128. }
  129.  
  130.  
  131. # programming is about making things simpler
  132.  
  133. "There are only two hard things in Computer Science:
  134. cache invalidation and naming things."
  135.  
  136. -- Phil Karlton
  137.  
  138.  
  139. # why is it suddenly so popular?
  140.  
  141. memory is cheap
  142.  
  143. cpu is growing in # cores not in GHz
  144.  
  145. functionally pure code is easier to understand
  146.  
  147. pure functions are easier to unit test
  148.  
  149. # unit testing pure functions
  150.  
  151. var mistery_func = function(a, b) {
  152.  
  153. //lol I don't know why we need this
  154. //but it breaks otherwise
  155.  
  156. write_to_file(random_filename(), "πŸ’© ");
  157. return a * 2 + b * 5;
  158. }
  159.  
  160. side effects need to be cleaned up
  161. or need to be mocked
  162.  
  163. # do you unit test?
  164.  
  165. learn by example or from your own mistakes
  166.  
  167. tests as communication
  168.  
  169. # Joel Test
  170.  
  171. Do you use source control?
  172. Can you make a build in one step?
  173. Do you make daily builds?
  174. Do you have a bug database?
  175. Do you fix bugs before writing new code?
  176. Do you have an up-to-date schedule?
  177. Do you have a spec?
  178. Do programmers have quiet working conditions?
  179. Do you use the best tools money can buy?
  180. Do you have testers?
  181. Do new candidates write code during their interview?
  182. Do you do hallway usability testing?
  183.  
  184.  
  185. - do you always write tests?
  186. - do you have mandatory code review?
  187.  
  188.  
  189. # Getting practical
  190.  
  191. Islands of functional purity
  192.  
  193. You will probably never end up programming in a purely functional language
  194.  
  195. Like in javascript you are not going to start using recursion everywhere
  196.  
  197. Even if you never work in a functional language professionally,
  198. understanding functional programming will make you a better developer.
  199.  
  200. It will give you a new perspective on your code and programming in general.
  201.  
  202. # A little bit of haskell
  203.  
  204. sum [1..12]
  205.  
  206. factorial 0 = 1
  207. factorial n = n * factorial (n - 1)
  208.  
  209. let right_triangles = [(x, y, z) |
  210. x**2 + y**2 = z**2,
  211. x <- [1..20],
  212. y <- [1..20],
  213. z <- [1..20] ]
  214.  
  215.  
  216. # Thanks for listening
  217.  
  218. Endre Galaczi
  219.  
  220. 🐦 @galacziendre
  221. βœ‰οΈ galacziendre@gmail.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement