Upcoming SlideShare
×

# Functional Concepts for OOP Developers

19,306 views

Published on

Functional Concepts for OOP Developers given at CodeMash 2009

Published in: Technology
1 Comment
15 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• good

Are you sure you want to  Yes  No
Views
Total views
19,306
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
417
1
Likes
15
Embeds 0
No embeds

No notes for slide

### Functional Concepts for OOP Developers

1. Functional Programming λ Bryan Weber Near Infinity Corporation January 2009 1
2. About the Speaker • Let’s be honest • You don’t know • You don’t care • Let’s move on... 2 2
3. Things should not change and they should not affect others 3 3
4. and it would be good if they could do both at the same time 4 4
5. What’s the point? 5 5
6. What not how not when 6 6
7. Before we go any further.. 7 7
8. Mathematics! 8
9. Uh oh! 9
11. Good book 11
12. Functional Languages Haskell Scala Clean Clojure F# XSLT ML / OCaml Erlang Lisp / Scheme 12 12
13. Pure Functional Languages Haskell Scala Clean Clojure F# XSLT ML / OCaml Erlang Lisp / Scheme 13 13
15. Introductory? 15
16. Programming? 16
17. Is it too hard? 17
18. Again? 18 18
19. The foundation 19
20. What is a function? y = f(x) 20
21. Lambda Calculus 21
22. Lambda expression λ x. x + 2 22
23. How do we achieve functions like.. f(x,y) -> z 23
24. Function Currying Example foo(x,y) -> z bar(x) -> baz baz(y) -> z 24
25. “It's not difﬁcult at all, proc {|x, y, z| x + y + z }.curry returns the proc object equivalent to proc {|x| proc {|y| proc {|z| x + y + z } } } See?” matz. 25
26. Why do this in Ruby? plus_ﬁve =proc{|x,y,z|x+y+z }.curry.call(2).call(3) plus_ﬁve[10] #=> 15 26
27. Ruby, really? C# too! LINQ 27
28. Arity example (1) %% sum/1 sum(L) -> sum(L,0). %% sum/2 sum([], N) -> N; sum([H|T],N) -> sum(T, H+N). 28
29. Pattern Matching example 29
30. -module(patternmatch). -export([run/0]). run() -> tupleassign(), pmatch(), invalid(). invalid() -> Str1 = quot;have a nice dayquot;, Str2 = quot;have another nice dayquot;, Str1 = Str2. %% this is an error! tupleassign() -> MyTuple = {1,2,3}, {A,B,C} = MyTuple, %% this assigns values to unbound A, B and C io:format(quot;A:~p B:~p C:~p ~nquot;,[A,B,C]). pmatch() -> Str1 = quot;la la laquot;, case Str1 of quot;fooquot; -> io:format(quot;uh oh ~nquot;); 3 -> io:format(quot;wrong type ~nquot;); quot;la la laquot; -> io:format(quot;We have a match ~nquot;); _ -> io:format(quot;like an else ~nquot;) end. 30
31. Built in Assertions / Single Assignment invalid() -> Str1 = quot;have a nice dayquot;, Str2 = quot;have another nice dayquot;, Str1 = Str2. %% this is an error! 31
32. Multiple Assignment tupleassign() -> %% create a tuple MyTuple = {1,2,3}, %% assign values to unbound A, B and C {A,B,C} = MyTuple, %% print the result io:format(quot;A:~p B:~p C:~p ~nquot;,[A,B,C]). 32
33. Switch Statement Str1 = quot;la la laquot;, case Str1 of quot;fooquot; -> io:format(quot;uh oh ~nquot;); 3 -> io:format(quot;wrong type ~nquot;); quot;la la laquot; -> io:format(quot;We have a match ~nquot;); _ -> io:format(quot;like an else ~nquot;) end. 33
34. Call by Future 34
35. Lazy Evaluation 35
36. Control Structure Example 36
37. DEFINE: newOr a b = if a then a else b EXAMPLE: newOr (4==4) (length [1..] > 0) 37
38. And now, for our featured presentation 38
39. Immutability Concurrency Side Effects 39
40. Functional Object Oriented 40
41. Immutability Concurrency Side Effects 41
42. Object Oriented Functional 42
43. Enlightenment? 43
44. Immutability Concurrency Side Effects 44
45. Immutability Concurrency Side Effects 45
46. Can Things Change? 46
47. Immutable Collections example Lists (IPersistentList) Vectors (IPersistentVector) Maps (IPersistentMap) StructMaps ArrayMaps* Sets 47
48. PROGRAM: (2) (in-ns 'main) (clojure/refer 'clojure) (defn main [args] (def x (list 1 2 3)) (def y (conj x quot;helloquot; quot;therequot; quot;worldquot;)) (println quot;list is: [quot; (apply str (interpose quot;, quot; y)) quot;]quot;) ) PRODUCES: list is: [ world, there, hello, 1, 2, 3 ] 48
49. Erlang Lists (3) append(List1, List2) -> List3 Types: List1 = List2 = List3 = [term()] Returns a new list List3 which is made from the elements of List1 followed by the elements of List2. 49
50. Immutability Concurrency Side Effects 50
51. Complexity 51
52. No shared state 52
53. STM Example 53
54. Operation Type Signature atomically STM a -> IO a retry STM a orElse STM a -> STM a -> STM a newTVar a -> STM (TVar a) readTVar TVar a -> STM a writeTVar TVar a -> a -> STM () 54
55. (4) module Main where << ... imports omitted ... >> main = do shared <- atomically \$ newTVar 0 before <- atomRead shared putStrLn \$ quot;Before: quot; ++ show before forkIO \$ 25 `timesDo` (dispVar shared >> milliSleep 20) forkIO \$ 10 `timesDo` (appV ((+) 2) shared >> milliSleep 50) forkIO \$ 20 `timesDo` (appV pred shared >> milliSleep 25) milliSleep 800 after <- atomRead shared putStrLn \$ quot;After: quot; ++ show after where timesDo = replicateM_ milliSleep = threadDelay . (*) 1000 atomRead = atomically . readTVar dispVar x = atomRead x >>= print appV fn x = atomically \$ readTVar x >>= writeTVar x . fn 55
56. Message Passing 56
57. Actor Primitives spawn send (!) receive 57
58. Actor actor 58
59. Actor Actor Spawn 59
60. Actor Actor Send 60
61. Actor Actor Receive (pull model) 61
62. Actor Example 62
63. -module(actor). -export([run/0]). act() -> receive Message -> io:format(quot;You said: ~p~nquot;,[Message]) end. run() -> Pid = spawn(fun act/0), Pid ! quot;Foobarbazquot;. 63
64. Immutability Concurrency Side Effects 64
65. Purity 65 65
66. Side effects 66 66
68. Your program State State Your API Code You Black Box 68 68
69. Your program State Database (Memory) Your API File System Code You Black Box Network 69 69
70. Launch Missiles 70 70
71. ALSO IO Exceptions Network Randomness Database Memory File System Order Console 71
72. Side effects are bad, mkay? 72
73. What can I do then? 73
75. What is a Monad? 75
76. Monadic Laws 1) Left identify 2) Right identity 3) Associativity 76
78. (5) hPutStr :: Handle -> String -> IO () hGetLine :: Handle -> IO String hEchoLine :: Handle -> IO String hEchoLine h = do { s <- hGetLine h ; hPutStr h (“I read: ” ++ s ++ “n”) ; return s } 78
79. Does order matter? 79
80. Order is a side effect as well.. 80
81. Order example 81
82. func(x,y,z) -> r zr xa yb 82
83. func(x,y,z) -> r zr xa yb 83
84. Data dependency example 84
85. func(x,y) -> z bz xa y,a b 85
86. func(x,y) -> z bz xa y,a b 86
87. func(x,y) -> z bz xa y,a b 87
88. func(x,y) -> z bz xa y,a b 88
89. Function Optimization Example f = g(x) + g(x) 89
90. The Real WOrld 90
91. No time for: Polymorphism Lists Tuples Guards Accumulators Continuations Comprehensions Fault Tolerance Type Systems Process Linking Recursion (TCO) Higher Order Functions 91
92. Summary 92
93. Functions (Math) 93
94. Functions (Math) Immutability 94
95. Functions (Math) Immutability Concurrency 95
96. Functions (Math) Immutability Concurrency Developer Optimizations 96
97. Functions (Math) Immutability Side Effects Concurrency Developer Optimizations 97
98. Functions (Math) Immutability Side Effects Concurrency Monads Developer Optimizations 98
99. Functions (Math) Immutability Side Effects Concurrency Monads Developer Automated Optimizations Optimizations 99
100. Things should not change (but state is great!) and they should not affect others (without notifying us) 100 100
101. and it would be good if they could do both at the same time (all the time) 101 101
102. Resources 102
103. Books Beautiful Code (Oram and Wilson) Real World Haskell (O’Sullivan, Stewart and Goerzen) Programming Erlang (Armstrong) Foundations of F# (Pickering) Expert F# (Syme, Granicz and Cisternino) F# for Scientists (Harrop) Programming in Haskell (Hutton) Coming Soon Programming Scala (Subramaniam) Programming Clojure (Halloway) 103