Functional Concepts for OOP Developers

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    6 Favorites

    Functional Concepts for OOP Developers - Presentation Transcript

    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
    10. Please don’t leave.. 10
    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
    14. Haskell 14
    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 difficult 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_five =proc{|x,y,z|x+y+z }.curry.call(2).call(3) plus_five[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 = \"have a nice day\", Str2 = \"have another nice day\", 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(\"A:~p B:~p C:~p ~n\",[A,B,C]). pmatch() -> Str1 = \"la la la\", case Str1 of \"foo\" -> io:format(\"uh oh ~n\"); 3 -> io:format(\"wrong type ~n\"); \"la la la\" -> io:format(\"We have a match ~n\"); _ -> io:format(\"like an else ~n\") end. 30
    31. Built in Assertions / Single Assignment invalid() -> Str1 = \"have a nice day\", Str2 = \"have another nice day\", 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(\"A:~p B:~p C:~p ~n\",[A,B,C]). 32
    33. Switch Statement Str1 = \"la la la\", case Str1 of \"foo\" -> io:format(\"uh oh ~n\"); 3 -> io:format(\"wrong type ~n\"); \"la la la\" -> io:format(\"We have a match ~n\"); _ -> io:format(\"like an else ~n\") 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 \"hello\" \"there\" \"world\")) (println \"list is: [\" (apply str (interpose \", \" y)) \"]\") ) 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 $ \"Before: \" ++ 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 $ \"After: \" ++ 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(\"You said: ~p~n\",[Message]) end. run() -> Pid = spawn(fun act/0), Pid ! \"Foobarbaz\". 63
    64. Immutability Concurrency Side Effects 64
    65. Purity 65 65
    66. Side effects 66 66
    67. Your program Your API Code You Black Box 67 67
    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
    74. Monads! 74
    75. What is a Monad? 75
    76. Monadic Laws 1) Left identify 2) Right identity 3) Associativity 76
    77. Monad example 77
    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
    104. The Web http://www.erlang.org http://www.haskell.org http://www.clojure.org http://www.trapexit.org http://www.scala-lang.org http://lambda-the-ultimate.org http://pragmaticstudio.com/erlang http://en.wikibooks.org/wiki/Haskell http://book.realworldhaskell.org/read http://www.lisperati.com/fringedc.html http://research.microsoft.com/fsharp/fsharp.aspx 104
    105. IRC chat.freenode.net #haskell #scala #erlang #clojure #fsharp 105
    106. References 1) Example shamelessly stolen from page 42 of “Programming Erlang” by Joe Armstrong. 2) Shamelessly stolen from Clojure doc 3) From Erlang stdlib documentation 4) Shamelessly stolen from http://www.haskell.org/haskellwiki/Simple_STM_example 5) Example from “Beautiful Code” by Oram and Wilson 106
    107. Questions? 107

    + brweber2brweber2, 10 months ago

    custom

    2637 views, 6 favs, 1 embeds more stats

    Functional Concepts for OOP Developers given at Cod more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2637
      • 2636 on SlideShare
      • 1 from embeds
    • Comments 0
    • Favorites 6
    • Downloads 236
    Most viewed embeds
    • 1 views on http://192.168.10.100

    more

    All embeds
    • 1 views on http://192.168.10.100

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories