The Shape of Functional Programming

  • 1,699 views
Uploaded on

The slides created for the NationJS 2013 conference.

The slides created for the NationJS 2013 conference.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • I don't think that there will be a video for this talk, but I plan to give the same at a future venue.
    Are you sure you want to
    Your message goes here
  • Thanks a lot Mike, but is there a video to go alongside the slides ?
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
1,699
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
18
Comments
2
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • just kidding
  • 1979
  • boolean
  • data abstractions, composed from primitive types
  • data abstractions, composed from primitive types
  • show cell(table, r, c)

Transcript

  • 1. The Shape of Functional Programming Saturday, October 5, 13
  • 2. @fogus http://www.fogus.me http://github.com/documentcloud/underscore-contrib Saturday, October 5, 13
  • 3. @fogus “Functional JavaScript”Lemonad codd.js Saturday, October 5, 13
  • 4. A program written in an object-oriented style Saturday, October 5, 13
  • 5. Functional programming for the MEOW! Saturday, October 5, 13
  • 6. Saturday, October 5, 13
  • 7. Pattern 1 - Map(ish) var array = [0,1,2,3,4,5]; var transformed = [];   for (var i = 0; i < array.length; i++) { transformed.push(array[i] + 100); }   transformed; //=> [100, 101, 102, 103, 104, 105] Saturday, October 5, 13
  • 8. Pattern 1 - Map(ish) // Underscore   _.map(array, function(n) { return n + 100; });     // Lemonad   L.map(L.add(100), array); Saturday, October 5, 13
  • 9. Pattern 2 - Filter(ish) var array = [0,1,2,3,4,5]; var keepers = [];   for (var i = 0; i < array.length; i++) { if (array[i] % 2 === 0) { keepers.push(array[i]); } }   keepers; //=> [101, 102, 103, 104, 105] Saturday, October 5, 13
  • 10. Pattern 2 - Filter(ish) // Underscore   _.filter(array, function(n) { if (n % 2 === 0) { return true; } else { return false; } });   // Lemonad   L.filter(L.isEven, array); Saturday, October 5, 13
  • 11. Pattern 3 - Reduce(ish) var array = [0,1,2,3,4,5]; var sum = 0;   for (var i = 0; i < array.length; i++) { sum += array[i]; }   sum; //=> 15 Saturday, October 5, 13
  • 12. Pattern 3 - Reduce(ish) // Underscore   _.reduce(array, function(acc, n) { return acc + n; }, 0);     // Lemonad   L.reduce(L.uncurry(L.add), 0, array); Saturday, October 5, 13
  • 13. APL life←{↑1 ⍵∨.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵} 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 Saturday, October 5, 13
  • 14. Fun.js at a glance • Simple data • Many small functions • Larger abstractions built via composition • OO to supplement (no dogma) Saturday, October 5, 13
  • 15. Simple data Saturday, October 5, 13
  • 16. “ Saturday, October 5, 13
  • 17. # Saturday, October 5, 13
  • 18. 42 Saturday, October 5, 13
  • 19. ? Saturday, October 5, 13
  • 20. Saturday, October 5, 13
  • 21. [ ] Saturday, October 5, 13
  • 22. # Saturday, October 5, 13
  • 23. # [1, 2, 3, 4, 5] Saturday, October 5, 13
  • 24. Saturday, October 5, 13
  • 25. { } Saturday, October 5, 13
  • 26. #“ Saturday, October 5, 13
  • 27. #“ {x: 0, y: 1} Saturday, October 5, 13
  • 28. {x: 0, y: 1} # Saturday, October 5, 13
  • 29. Saturday, October 5, 13
  • 30. * Saturday, October 5, 13
  • 31. Small functions Saturday, October 5, 13
  • 32. Saturday, October 5, 13
  • 33. λ Saturday, October 5, 13
  • 34. λ Saturday, October 5, 13
  • 35. λ Saturday, October 5, 13
  • 36. Disclaimer Saturday, October 5, 13
  • 37. # Procedure Saturday, October 5, 13
  • 38. # Procedure Saturday, October 5, 13
  • 39. function logNth(array, index) { console.log(array[index]); } logNth([‘a’, ‘b’, ‘c’], 1); // (console) b # Saturday, October 5, 13
  • 40. Purity Saturday, October 5, 13
  • 41. Ostrich Purity Saturday, October 5, 13
  • 42. Ostrich Purity Saturday, October 5, 13
  • 43. # Function Kinda important Saturday, October 5, 13
  • 44. function nth(array, index) { return array[index]; } nth([‘a’, ‘b’, ‘c’], 1); //=> ‘b’ # Saturday, October 5, 13
  • 45. Saturday, October 5, 13
  • 46. Saturday, October 5, 13
  • 47. Saturday, October 5, 13
  • 48. id name age 555-55-5555 Moe 45 777-77-7777 Curly 47 0 1 Saturday, October 5, 13
  • 49. var table = [ {id: ‘555-55-5555’, name: ‘Moe’, age: 45}, {id: ‘777-77-7777’, name: ‘Curly’, age: 47} ]; Saturday, October 5, 13
  • 50. id name age 555-55-5555 Moe 45 777-77-7777 Curly 47 0 1 Saturday, October 5, 13
  • 51. #“ Saturday, October 5, 13
  • 52. function cell(table, col, row){ return table[row][col]; } cell(table, ‘name’, 0); //=> ‘Moe’ cell(table, ‘age’, 0); //=> 45 #“ Saturday, October 5, 13
  • 53. Larger abstractions built via composition Saturday, October 5, 13
  • 54. L Clojure, Haskell, ML, Joy, _ Saturday, October 5, 13
  • 55. Currying, partial application, fixation, oh my! Saturday, October 5, 13
  • 56. #“ Saturday, October 5, 13
  • 57. #“ L.fix Saturday, October 5, 13
  • 58. # L.fix(cell, L._, ‘name’, L._); ‘name’ Saturday, October 5, 13
  • 59. # L.fix(cell, L._, ‘name’, L._); var getName = L.fix(cell, L._, ‘name’, L._); getName(table, 0); //=> ‘Moe’ getName(table, 1); //=> ‘Curly’ ‘name’ Saturday, October 5, 13
  • 60. #“ L.rot Saturday, October 5, 13
  • 61. # L.rot(cell); “ Saturday, October 5, 13
  • 62. #“ L.partial Saturday, October 5, 13
  • 63. L.partial(L.rot(cell), ‘name’); #‘name’ Saturday, October 5, 13
  • 64. L.partial(L.rot(cell), ‘name’); var getName = L.partial(L.rot(cell), ‘name’); getName(0, table); //=> ‘Moe’ getName(1, table); //=> ‘Curly’ #‘name’ Saturday, October 5, 13
  • 65. L.rcurry “ Codd.col(table, ‘name’); //=> [‘Moe’, ‘Curly’] Saturday, October 5, 13
  • 66. “ “ L.rcurry2(Codd.col); Saturday, October 5, 13
  • 67. “ L.rcurry2(Codd.col); var valuesFor = L.rcurry2(Codd.col); var allNames = valuesFor(‘name’); allNames(table); //=> [‘Moe’, ‘Curly’] Saturday, October 5, 13
  • 68. Compositions and Pipelines Saturday, October 5, 13
  • 69. How to count all of the values in a column? Saturday, October 5, 13
  • 70. “ function countValues(table, tag) { return L.len(Codd.col(table, tag)); } countValues(table, ‘name’); //=> 2 # Saturday, October 5, 13
  • 71. L.comp return L.len(Codd.col(table, tag)); First thisThen this Saturday, October 5, 13
  • 72. “Codd.col # L.len Saturday, October 5, 13
  • 73. “Codd.col # L.len Saturday, October 5, 13
  • 74. “ var countValues = L.comp(L.len, Codd.col); countValues(table, ‘name’); //=> 2 # Saturday, October 5, 13
  • 75. L.pipeline L.pipeline(table, Codd.col(‘name’), L.len); //=> 2 First this Then this Saturday, October 5, 13
  • 76. codd.js (demo) Relational Algebra Saturday, October 5, 13
  • 77. Thanks • The NationJS team • O’Reilly • Alan Dipert • Paul Khuong • The Underscore community • The Clojure community Saturday, October 5, 13