Slide 1

Slide 1 text

OOP is dead 
 only if FP is dead @andrestaltz

Slide 2

Slide 2 text

Is object-oriented programming fundamentally different or superficially different to functional programming?

Slide 3

Slide 3 text

OOP FP

Slide 4

Slide 4 text

Closures Objects Immutability Mutation Assignment
 syntax Assignment
 semantics Typed Dynamic Evaluate Execute

Slide 5

Slide 5 text

Evaluate Execute

Slide 6

Slide 6 text

Evaluate Execute Calculate the value of an expression Perform a sequence of get/set actions

Slide 7

Slide 7 text

Evaluate Execute Calculate the value of an expression Perform a sequence of get/set actions Imperative Functional

Slide 8

Slide 8 text

Evaluate Execute (weight => height => weight / 
 (height * height * 0.01 * 0.01) )(70)(177) var weight = 70; var height = 177; var bmi = weight; bmi /= height; bmi /= height; bmi /= 0.01; bmi /= 0.01;

Slide 9

Slide 9 text

Evaluate Execute Foundation is Lambda calculus Foundation is
 Random access machine

Slide 10

Slide 10 text

Closures Objects

Slide 11

Slide 11 text

Closures Objects Put data in an object, attach methods to the object Put data in a closure, functions refer to the closure

Slide 12

Slide 12 text

Closures Objects const greeter = { isFemale: false, greet(name) { return this.isFemale ? "Hi Ms. " + name : "Hi Mr. " + name; } }; const greet = (isFemale => name => isFemale ? "Hi Ms. " + name : "Hi Mr. " + name )(false);

Slide 13

Slide 13 text

Closures Objects const greeter = { // ... }; greeter.greet.bind({isFemale: true})('Lisa'); Advantage: allows changing the this object for a method.

Slide 14

Slide 14 text

Closures Objects const greeter = { // ... }; greeter.greet.bind({isFemale: true})('Lisa'); Advantage: allows changing the this object for a method. Disadvantage: allows changing the this object for a method.

Slide 15

Slide 15 text

Immutability Mutation

Slide 16

Slide 16 text

Immutability Mutation

Slide 17

Slide 17 text

Immutability Mutation final String s = "ABC"; s.toLowerCase(); Java

Slide 18

Slide 18 text

Immutability Mutation Haskell import Data.Char str1 = "ABC" str2 = map toLower str1

Slide 19

Slide 19 text

Immutability Mutation Haskell import Data.IORef f :: Num a => a -> IO a f x = do y <- newIORef 5 -- :: IORef Int writeIORef y 10 val <- readIORef y -- :: Int return 2 * val

Slide 20

Slide 20 text

Immutability Mutation Haskell import Data.STRef import Control.Monad.ST f :: Num a => a -> a f x = runST $ do y <- newSTRef 5 -- :: STRef Int writeSTRef y 10 val <- readSTRef y -- :: Int return 2 * val

Slide 21

Slide 21 text

Assignment
 syntax Assignment
 semantics

Slide 22

Slide 22 text

Assignment
 syntax Assignment
 semantics Haskell do text <- readFile "foo" writeFile "bar" text readFile "foo" >>= writeFile "bar"

Slide 23

Slide 23 text

Assignment
 syntax Assignment
 semantics Haskell do text <- readFile "foo" writeFile "bar" text readFile "foo" >>= writeFile "bar"

Slide 24

Slide 24 text

Assignment
 syntax Assignment
 semantics JavaScript var text = fs.readFileSync('foo'); fs.writeFileSync('bar', text);

Slide 25

Slide 25 text

Assignment
 syntax Assignment
 semantics strongConnect v graph index stack indices lowlinks output = do i <- readSTRef index modifySTRef' indices (I.insert v i) modifySTRef' lowlinks (I.insert v i) modifySTRef' index (+1) push stack v forM_ (graph A.! v) $ \w -> do wIndex <- I.lookup w <$> readSTRef indices if isNothing wIndex then do strongConnect w graph index stack indices lowlinks output vLowLink <- fromJust . I.lookup v <$> readSTRef lowlinks wLowLink <- fromJust . I.lookup w <$> readSTRef lowlinks modifySTRef' lowlinks (I.insert v $ min vLowLink wLowLink) else do wOnStack <- elem w <$> readSTRef stack when wOnStack $ do vLowLink <- fromJust . I.lookup v <$> readSTRef lowlinks modifySTRef' lowlinks (I.insert v $ min vLowLink (fromJust wIndex)) vLowLink <- fromJust . I.lookup v <$> readSTRef lowlinks vIndex <- fromJust . I.lookup v <$> readSTRef indices when (vLowLink == vIndex) $ do scc <- addSCC v [] stack modifySTRef' output (scc:) http://vaibhavsagar.com/blog/2017/05/29/imperative-haskell/

Slide 26

Slide 26 text

Typed Dynamic

Slide 27

Slide 27 text

Typed Dynamic Clojure (FP) Python (OOP) Haskell (FP) Java (OOP)

Slide 28

Slide 28 text

Typed Dynamic Is x => 2 * x pure?

Slide 29

Slide 29 text

Typed Dynamic Is x => 2 * x pure? const double = x => 2 * x; const lol = {}; lol.valueOf = Math.random; double(lol); // 0.21666564647375441 double(lol); // 1.4260443726695509

Slide 30

Slide 30 text

Typed Dynamic Is x => 2 * x pure? const double: (x: number) => number = x => 2 * x;

Slide 31

Slide 31 text

Typed Dynamic Abstractions: solving problems on the level of ideas

Slide 32

Slide 32 text

Typed Dynamic

Slide 33

Slide 33 text

Typed Dynamic

Slide 34

Slide 34 text

Typed Dynamic Abstraction = flexibility. integers = [1, 2..] list1 = take 5 integers -- [1,2,3,4,5] list2 = take 10 integers -- [1,2,3,4,5,6,7,8,9,10] Haskell

Slide 35

Slide 35 text

Typed Dynamic In Java... Objects are concrete. Interfaces are abstract. Classes are a bit of both.

Slide 36

Slide 36 text

Typed Dynamic In Haskell... Basic types are concrete. Type classes are abstract. Abstract Data Types too.

Slide 37

Slide 37 text

Typed Dynamic Inheritance In OOP: reusability of classes (= data + methods) In FP: reusability of type classes (= functions)

Slide 38

Slide 38 text

Typed Dynamic Inheritance In OOP: reusability of classes (= data + methods) In FP: reusability of type classes (= functions)

Slide 39

Slide 39 text

Typed Dynamic Inheritance In OOP: reusability of classes (= data + methods) In FP: reusability of type classes (= functions)

Slide 40

Slide 40 text

Typed Dynamic Inheritance In OOP: reusability of classes (= data + methods) In FP: reusability of type classes (= functions)

Slide 41

Slide 41 text

Typed Dynamic Inheritance In OOP: reusability of classes (= data + methods) In FP: reusability of type classes (= functions)

Slide 42

Slide 42 text

Typed Dynamic Inheritance In OOP: reusability of classes (= data + methods) In FP: reusability of type classes (= functions)

Slide 43

Slide 43 text

Typed Dynamic Diamond problem in typed OOP Object equals() Rectangle equals() Clickable equals() Button

Slide 44

Slide 44 text

The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; 
 premature optimization is the root of all evil (or at least most of it) in programming. – Donald Knuth

Slide 45

Slide 45 text

Premature concretization is the root of all complexity (or at least most of it) in programming. – André Staltz

Slide 46

Slide 46 text

Typed Dynamic Haskell can reliably do multiple inheritance.

Slide 47

Slide 47 text

Typed Dynamic Haskell can reliably do multiple inheritance.

Slide 48

Slide 48 text

Typed Dynamic JavaScript can unreliably do multiple inheritance.

Slide 49

Slide 49 text

Typed Dynamic JavaScript can unreliably do multiple inheritance. // Creating objects var o1, o2, o3, obj = multiInherit(o1={a:1}, o2={b:2}, o3={a:3, b:3}); // Setting properties obj.c = 3; // Reading properties obj.a; // 1 (inherited from o1) obj.b; // 2 (inherited from o2) obj.c; // 3 (own property) obj.d; // undefined (not found) https://stackoverflow.com/questions/9163341/multiple-inheritance-prototypes-in-javascript/31236132#31236132

Slide 50

Slide 50 text

Typed Dynamic JavaScript can unreliably do multiple inheritance. function multiInherit (...protos) { return Object.create(new Proxy(Object.create(null), { get (target, prop, receiver) { var obj = protos.find(obj => prop in obj); return obj ? Reflect.get(obj, prop, receiver) : void 0; }, set (target, prop, value, receiver) { var obj = protos.find(obj => prop in obj); return Reflect.set(obj || Object.create(null), prop, value, receiver); }, has: (target, prop) => protos.some(obj => prop in obj), ownKeys(target) { // ... }, getOwnPropertyDescriptor(target, prop) { // ... }, *enumerate (target) { yield* this.ownKeys(target); }, preventExtensions: (target) => false, defineProperty: (target, prop, desc) => false, })); }

Slide 51

Slide 51 text

Typed Dynamic Strongly typed FP Haskell

Slide 52

Slide 52 text

Haskell v1.3 Report

Slide 53

Slide 53 text

Haskell v1.3 Report

Slide 54

Slide 54 text

Evaluate Execute Calculate the value of an expression Perform a sequence of get/set actions Imperative Functional

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

OOP FP Should we mix everything?

Slide 58

Slide 58 text

OOP FP Should we mix everything? Languages should be coherent

Slide 59

Slide 59 text

Koka Typed Evaluate Immutability Assignment syntax Closures Object syntax

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Closures Objects Immutability Mutation Assignment
 syntax Assignment
 semantics Typed Dynamic Evaluate Execute Thanks : )