Successfully reported this slideshow.
Upcoming SlideShare
×

# Dont Drive on the Railroad Tracks

1,312 views

Published on

Published in: Technology
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

### Dont Drive on the Railroad Tracks

1. 1. Don’t Drive on the Railroad Tracks Eugene Wallingford University of Northern Iowa November 17, 2010 Thursday, November 18, 2010
2. 2. In the small, you know this. It is no big deal. In the large, this is different. It changes how you think about problems and data. Two Claims Thursday, November 18, 2010
3. 3. you know this Thursday, November 18, 2010
4. 4. def addSalesTax( price ) price * 1.07 end Thursday, November 18, 2010
5. 5. def addSalesTax( price ) price = price * 1.07 end Thursday, November 18, 2010
6. 6. def addSalesTax( price ) price = price * 1.07 end X Thursday, November 18, 2010
7. 7. def addSalesTax( price ) tax = price * 0.07 price = price + tax end Thursday, November 18, 2010
8. 8. def addSalesTax( price ) tax = price * 0.07 price = price + tax end X Thursday, November 18, 2010
9. 9. side effects Thursday, November 18, 2010
10. 10. side effectsX Thursday, November 18, 2010
11. 11. Thursday, November 18, 2010
12. 12. Thursday, November 18, 2010
13. 13. Thursday, November 18, 2010
14. 14. def addSalesTax( price ) price * 1.07 end Thursday, November 18, 2010
15. 15. sort -m access01-ips access02-ips | uniq -d | wc -l Thursday, November 18, 2010
16. 16. wc(“-l”, uniq(“-d”, sort(“-m”, access01-ips, access02-ips))) Thursday, November 18, 2010
17. 17. [ 1 , 2 , 3 , 4 ] ["a","b","c","d"] Thursday, November 18, 2010
18. 18. [ 1 , 2 , 3 , 4 ] ["a","b","c","d"] Thursday, November 18, 2010
19. 19. [ 1 , 2 , 3 , 4 ]. zip( ["a","b","c","d"] ) Thursday, November 18, 2010
20. 20. [[1, "a"], [2, "b"], [3, "c"], [4, "d"]] Thursday, November 18, 2010
21. 21. [ 1 , 2 , 3 , 4 ] { |x| x.odd? } Thursday, November 18, 2010
22. 22. [ 1 , 2 , 3 , 4 ] [ 1 , 3 ] Thursday, November 18, 2010
23. 23. [ 1 , 2 , 3 , 4 ]. select { |x| x.odd? } Thursday, November 18, 2010
24. 24. [ 1, 3 ] Thursday, November 18, 2010
25. 25. [ 1 , 2 , 3 , 4 ] { |x| x.odd? } Thursday, November 18, 2010
26. 26. [ 1 , 2 , 3 , 4 ] [ 1 , 2 , 3 , 4 ] Thursday, November 18, 2010
27. 27. [ 1 , 2 , 3 , 4 ]. partition { |x| x.odd? } Thursday, November 18, 2010
28. 28. [ [ 1, 3 ], [ 2, 4 ] ] Thursday, November 18, 2010
29. 29. [ 1 , 2 , 3 , 4 ] { |x| x * x } Thursday, November 18, 2010
30. 30. [ 1 , 2 , 3 , 4 ] [ 1 , 4 , 9 , 16 ] Thursday, November 18, 2010
31. 31. [ 1 , 2 , 3 , 4 ]. map { |x| x * x } Thursday, November 18, 2010
32. 32. [ 1 , 2 , 3 , 4 ] [ 1 , 4 , 9 , 16 ] ^2^2^2^2 Thursday, November 18, 2010
33. 33. [ 1 , 2 , 3 , 4 ] 1 + 2 + 3 + 4 => 10 Thursday, November 18, 2010
34. 34. 1 + 2 + 3 + 4 == ((1 + 2) + 3) + 4 Thursday, November 18, 2010
35. 35. [ 1 , 2 , 3 , 4 ]. inject { |x,y| x + y } Thursday, November 18, 2010
36. 36. [ 1 , 2 , 3 , 4 ]. inject { |x,y| x + y } fold the list with + Thursday, November 18, 2010
37. 37. { |x| x.odd? } { |x| x * x } { |x,y| x + y } Thursday, November 18, 2010
38. 38. functions are ﬁrst-class values Thursday, November 18, 2010
39. 39. # Python for item in iterable_collection: # do something with item # Ruby set.each do |item| # do something with item end Thursday, November 18, 2010
40. 40. next steps Thursday, November 18, 2010
41. 41. implies recursion over persistent data structures Thursday, November 18, 2010
42. 42. number ::= 0 | 1 + number Thursday, November 18, 2010
43. 43. list ::= empty | item + list Thursday, November 18, 2010
44. 44. tree ::= empty | item + tree + tree Thursday, November 18, 2010
45. 45. induction implies recursion Thursday, November 18, 2010
46. 46. what versus how Thursday, November 18, 2010
47. 47. number ::= 0 | 1 + number if n = 0 do something else solve for 1 solve for n-1 combine Thursday, November 18, 2010
48. 48. number ::= 0 | 1 + number Thursday, November 18, 2010
49. 49. number ::= 0 | 1 + number decrease and conquer Thursday, November 18, 2010
50. 50. number ::= 0 | 1 + number sequential Thursday, November 18, 2010
51. 51. number ::= 0 | number/2 + number/2 Thursday, November 18, 2010
52. 52. number ::= 0 | number/2 + number/2 divide and conquer Thursday, November 18, 2010
53. 53. number ::= 0 | number/2 + number/2 parallel Thursday, November 18, 2010
54. 54. tree ::= empty | item + tree + tree divide and conquer parallel Thursday, November 18, 2010
55. 55. MapReduce map an operator over each item reduce (fold) the resulting list Thursday, November 18, 2010
56. 56. [ 8, 4, 1, 6, 7, 2, 5, 3 ] [ 1, 2, 3, 4, 5, 6, 7, 8 ] Thursday, November 18, 2010
57. 57. [ 8, 4, 1, 6, 7, 2, 5, 3 ]. map { |x| [x] } [[8], [4], [1], [6], [7], [2], [5], [3]] make a list of each item Thursday, November 18, 2010
58. 58. [[8], [4], [1], [6], [7], [2], [5], [3]]. inject { |x,y| merge(x,y) } merge the sorted lists, pairwise [ 1, 2, 3, 4, 5, 6, 7, 8 ] Thursday, November 18, 2010
59. 59. [ 8, 4, 1, 6, 7, 2, 5, 3 ] .map { |x| [x] } .inject { |x,y| merge(x,y) } map/reduce [ 1, 2, 3, 4, 5, 6, 7, 8 ] Thursday, November 18, 2010
60. 60. Implications for Parallelism merge(a,b) == merge(b,a) && merge(a, merge(b,c)) == merge(merge(a, b),c) merges can be done independently Thursday, November 18, 2010
61. 61. Thursday, November 18, 2010
62. 62. really getting it Thursday, November 18, 2010
63. 63. class Proc def self.compose(f, g) lambda { |*args| f[g[*args]] } end end Thursday, November 18, 2010
64. 64. class Proc def self.compose(f, g) lambda { |*args| f[g[*args]] } end end Thursday, November 18, 2010
65. 65. class Proc def self.compose(f, g) lambda { |*args| f[g[*args]] } end end Thursday, November 18, 2010
66. 66. class Proc def self.compose(f, g) lambda { |*args| f[g[*args]] } end end combinator Thursday, November 18, 2010
67. 67. A combinator is a function that takes functions as input and computes its result by composing those functions. * * and nothing else. There are no free variables. Thursday, November 18, 2010
68. 68. combinator is to functional programming as framework is to object-oriented programming Thursday, November 18, 2010
69. 69. combinator is to functional programming as framework is to object-oriented programming the next level of abstraction Thursday, November 18, 2010
70. 70. A Common Pattern... widget.collection .select { |a_table| a_table.widgets_column_name =~ regex } .map { |a_table| widget.attribute_present?(a_table.widgets_column_name) && { a_table.label => widget.send(a_table.widgets_column_name) } || {} } .inject(&:merge) Thursday, November 18, 2010
71. 71. Combinators in Action suppose we want to ﬁnd the square of the sum of all the odd numbers between 1 and 100 Thursday, November 18, 2010
72. 72. (1..100) Thursday, November 18, 2010
73. 73. (1..100).select(&:odd?) Thursday, November 18, 2010
74. 74. (1..100).select(&:odd?).inject(&:+) Thursday, November 18, 2010
75. 75. lambda { |x| x * x }.call( (1..100).select(&:odd?).inject(&:+)) Thursday, November 18, 2010
76. 76. lambda { |x| x * x }.call( (1..100).select(&:odd?).inject(&:+)) Thursday, November 18, 2010
77. 77. A permuting combinator composes two functions in reverse order. Instead of f(g(x), we want g(f(x)). Thursday, November 18, 2010
78. 78. (1..100).select(&:odd?).inject(&:+) .callWithSelf(lambda { |x| x * x }) Thursday, November 18, 2010
79. 79. (1..100).select(&:odd?).inject(&:+) .into (lambda { |x| x * x }) Thursday, November 18, 2010
80. 80. (1..100) .select(&:odd?) .inject(&:+) .into(lambda { |x| x * x }) Thursday, November 18, 2010
81. 81. class Object def into expr = nil expr.nil? ? yield(self) : expr.to_proc.call(self) end end Thursday, November 18, 2010
82. 82. Um, what about Scala? Thursday, November 18, 2010
83. 83. case class Thrush[A](x: A) { def into[B](g: A => B): B = { g(x) } } Thursday, November 18, 2010
84. 84. Thrush((1 to 100) .filter(_ % 2 != 0) .foldLeft(0)(_ + _)) .into((x: Int) => x * x) Thursday, November 18, 2010
85. 85. accounts .filter(_ belongsTo "John S.") .map(_.calculateInterest) .filter(_ > threshold) .foldLeft(0)(_ + _) .into {x: Int => updateBooks journalize (Ledger.INTEREST, x) } Thursday, November 18, 2010
86. 86. Thursday, November 18, 2010
87. 87. more? Thursday, November 18, 2010
88. 88. functional design patterns Structural Recursion Thursday, November 18, 2010
89. 89. functional design patterns Structural Recursion Interface Procedure Thursday, November 18, 2010
90. 90. functional design patterns Structural Recursion Interface Procedure Mutual Recursion Thursday, November 18, 2010
91. 91. functional design patterns Structural Recursion Interface Procedure Mutual Recursion Accumulator Passing Thursday, November 18, 2010
92. 92. functional design patterns Structural Recursion Interface Procedure Mutual Recursion Accumulator Passing Local Procedure Thursday, November 18, 2010
93. 93. functional design patterns Structural Recursion Interface Procedure Mutual Recursion Accumulator Passing Local Procedure Program Derivation Thursday, November 18, 2010
94. 94. functional design patterns Structural Recursion Interface Procedure Mutual Recursion Accumulator Passing Local Procedure Program Derivation Tail-Recursive State Machine Continuation Passing Control Abstraction Thursday, November 18, 2010
95. 95. Isn’t all this recursion so inefﬁcient as to be impractical? Thursday, November 18, 2010
96. 96. This is the 21st century. Thursday, November 18, 2010
97. 97. garbage collection Thursday, November 18, 2010
98. 98. tail-call elimination Thursday, November 18, 2010
99. 99. def foo(...) = { if (n is base case) return some value else foo(...) } Thursday, November 18, 2010
100. 100. <Scheme indulgence> Thursday, November 18, 2010
101. 101. def factorial(n: Int) = { def loop(n: Int, acc: Int): Int = if (n <= 0) acc else loop(n - 1, acc * n) loop(n, 1) } Thursday, November 18, 2010
102. 102. return ’done Thursday, November 18, 2010
103. 103. If I had asked people what they wanted, they would have said 'faster horses'. Henry Ford Thursday, November 18, 2010
104. 104. An invention has to make sense in the world in which it is ﬁnished, not the world in which it was started. Ray Kurzweil Thursday, November 18, 2010
105. 105. http://www.youtube.com/watch?v=c_5GpBgsang http://weblog.raganwald.com/2008/01/no-detail-too-small.html http://debasishg.blogspot.com/2009/09/thrush-combinator-in-scala.html http://fupeg.blogspot.com/2009/04/tail-recursion-in-scala.html http://www.cs.uni.edu/~wallingf/patterns/recursion.html http://www.cs.uni.edu/~wallingf/patterns/envoy.pdf http://mitpress.mit.edu/sicp/ http://sicpinclojure.com/ resources to study Thursday, November 18, 2010