it's the end of design patterns as we know it (and i feel fine)

101
It’s the end of Design Patterns as we know it (and I feel fine) Today: Introducing Monads Luiz Borba http://borba.blog.br

Upload: luiz-borba

Post on 22-Aug-2015

389 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: It's the end of design patterns as we know it (and i feel fine)

It’s the end of Design Patterns as we know it

(and I feel fine)Today:

Introducing Monads

Luiz Borba http://borba.blog.br

Page 2: It's the end of design patterns as we know it (and i feel fine)

Part 1 Deconstructing Design Patterns

Page 3: It's the end of design patterns as we know it (and i feel fine)

1994

Page 4: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

PHP (95)

Page 5: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

PHP (95)

Page 6: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

DESKTOP

PHP (95)

Page 7: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

WEB

PHP (95)

Page 8: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

PHP (95)

Page 9: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

PHP (95)

Page 10: It's the end of design patterns as we know it (and i feel fine)

I always knew that one day

Smalltalkwould replace Java.I just didn’t knowit would be called

Ruby

"

"(Kent Beck)

Page 11: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

Page 12: It's the end of design patterns as we know it (and i feel fine)

Mainstream,It’s the

stupid!

Page 13: It's the end of design patterns as we know it (and i feel fine)

AlwaysControversial

Page 14: It's the end of design patterns as we know it (and i feel fine)
Page 15: It's the end of design patterns as we know it (and i feel fine)
Page 16: It's the end of design patterns as we know it (and i feel fine)

Ralph Johnson and Singleton

Page 17: It's the end of design patterns as we know it (and i feel fine)

AreDesign Patternsmissing languages features?

Page 18: It's the end of design patterns as we know it (and i feel fine)

AreDesign Patternshow languages evolve?

(Jeff Atwood)

Page 19: It's the end of design patterns as we know it (and i feel fine)
Page 20: It's the end of design patterns as we know it (and i feel fine)
Page 21: It's the end of design patterns as we know it (and i feel fine)
Page 22: It's the end of design patterns as we know it (and i feel fine)

COBOL (61)

FORTRAN (57)

CLIPPER (85)

C (72)

C++ (83)

SMALLTALK (72)

PASCAL (86)PERL (87)

LISP (58)

OBJECT PASCAL (95)

HASKELL (90)

JAVA (95) RUBY (95)

PYTHON (91)

ML (73)

ALGOL (58)

POWERBUILDER (92)

BASIC (64)

VISUAL BASIC (91)

MS BASIC (75)

SQLWINDOWS (88)

Page 23: It's the end of design patterns as we know it (and i feel fine)

2010

Page 24: It's the end of design patterns as we know it (and i feel fine)
Page 25: It's the end of design patterns as we know it (and i feel fine)
Page 26: It's the end of design patterns as we know it (and i feel fine)
Page 27: It's the end of design patterns as we know it (and i feel fine)
Page 28: It's the end of design patterns as we know it (and i feel fine)
Page 29: It's the end of design patterns as we know it (and i feel fine)
Page 30: It's the end of design patterns as we know it (and i feel fine)
Page 31: It's the end of design patterns as we know it (and i feel fine)
Page 32: It's the end of design patterns as we know it (and i feel fine)
Page 33: It's the end of design patterns as we know it (and i feel fine)
Page 34: It's the end of design patterns as we know it (and i feel fine)

Java

imperative languages

C FortranC++

add abstractions

Page 35: It's the end of design patterns as we know it (and i feel fine)

subtract abstractions

LispML

Haskell

functional languages

Java

imperative languages

C FortranC++

add abstractions

Page 36: It's the end of design patterns as we know it (and i feel fine)

subtract abstractions

LispML

Haskell

functional languages

Java

imperative languages

C FortranC++

add abstractions

hybrid languages

ScalaJava 8

C#F#

Page 37: It's the end of design patterns as we know it (and i feel fine)
Page 38: It's the end of design patterns as we know it (and i feel fine)
Page 39: It's the end of design patterns as we know it (and i feel fine)

[x | x <- xs, x < 10]Haskell:

Page 40: It's the end of design patterns as we know it (and i feel fine)

[x | x <- xs, x < 10]

from x in xs where x < 10 select x;

Haskell:

C#:

Page 41: It's the end of design patterns as we know it (and i feel fine)

[x | x <- xs, x < 10]

from x in xs where x < 10 select x;

Haskell:

C#:

Java 8:xs.stream().filter(x -> x < 10)

Page 42: It's the end of design patterns as we know it (and i feel fine)

2014

Page 43: It's the end of design patterns as we know it (and i feel fine)

Java 8

C#

Swift

C

PHP

Python

Ruby

Javascript

Dart

Scala

Go

Clojure

Rust

Groovy

Erlang

Elixir

Coffeescript

C++

Objective-C

Haskell

F#

Page 44: It's the end of design patterns as we know it (and i feel fine)

Java 8

C#

Swift

C

PHP

Python

Ruby

Javascript

Dart

Scala

Go

Clojure

Rust

Groovy

Erlang

Elixir

Coffeescript

C++

Objective-C

Haskell

F#

Page 45: It's the end of design patterns as we know it (and i feel fine)
Page 46: It's the end of design patterns as we know it (and i feel fine)

We need to rewrite thedesign patterns book

Page 47: It's the end of design patterns as we know it (and i feel fine)

Part 2 Introducing Monads

Page 48: It's the end of design patterns as we know it (and i feel fine)
Page 49: It's the end of design patterns as we know it (and i feel fine)

(Douglas Crockford)

Once you understandMonadsfor yourself

you lose the ability

to explain them to others

"

"

Page 50: It's the end of design patterns as we know it (and i feel fine)

A Monad is justa monoid in thecategory ofendofunctors

Page 51: It's the end of design patterns as we know it (and i feel fine)

WTF?

Page 52: It's the end of design patterns as we know it (and i feel fine)
Page 53: It's the end of design patterns as we know it (and i feel fine)

WARM UP

Page 54: It's the end of design patterns as we know it (and i feel fine)

What’s aStack?

Page 55: It's the end of design patterns as we know it (and i feel fine)

Value

Page 56: It's the end of design patterns as we know it (and i feel fine)

Operations

Page 57: It's the end of design patterns as we know it (and i feel fine)

Rules

+ = stack.push(value).top == value stack.isEmpty == truestack.push(value).isEmpty == false

Page 58: It's the end of design patterns as we know it (and i feel fine)

We can implement them however we like

We can use them without knowing their implementation

and..

Page 59: It's the end of design patterns as we know it (and i feel fine)

What’s aQueue?

Page 60: It's the end of design patterns as we know it (and i feel fine)

What are

Queue?Stack &

Page 61: It's the end of design patterns as we know it (and i feel fine)

ABSTRACT

TYPESDATA

Page 62: It's the end of design patterns as we know it (and i feel fine)

JavascriptPromises

Page 63: It's the end of design patterns as we know it (and i feel fine)

modern,(Anders Hejlsberg)

Asynchronousprogramming is becoming the norm in

connected applications"

"

Page 64: It's the end of design patterns as we know it (and i feel fine)

var FS = require('fs'), request = require('request');

function getResults(pathToFile, callback) { FS.readFile(pathToFile, 'utf8', function(err, data) { if (err) return callback(err); var response1, response2;

request.post('http://service1.example.com?data=' + data), function(err, response, body) { if(err) return callback(err); response1 = response; next(); });

request.post('http://service2.example.com?data=' + data), function(err, response, body) { if(err) return callback(err); response2 = response; next(); });

function next(){ if(response1 && response2){ callback(null, [response1, response2]); } } }); }

Page 65: It's the end of design patterns as we know it (and i feel fine)

asyncCall(function(err, data1){ if(err) return callback(err); anotherAsyncCall(function(err2, data2){ if(err2) return calllback(err2); oneMoreAsyncCall(function(err3, data3){ if(err3) return callback(err3); // are we done yet? }); }); });

Page 66: It's the end of design patterns as we know it (and i feel fine)

asyncCall() .then(function(data1){ // do something... return anotherAsyncCall(); }) .then(function(data2){ // do something... return oneMoreAsyncCall(); }) .then(function(data3){ // the third and final async response }) .fail(function(err) { // handle any error resulting from any of the above calls }) .done();

Page 67: It's the end of design patterns as we know it (and i feel fine)

var promise = new Promise(initialFunction) var anotherPromise = promise.then(functionB) var yetAnotherPromise = anotherPromise.then(functionC) ...

Page 68: It's the end of design patterns as we know it (and i feel fine)

var promise = new Promise(initialFunction) var anotherPromise = promise.then(functionB) var yetAnotherPromise = anotherPromise.then(functionC) ...

Page 69: It's the end of design patterns as we know it (and i feel fine)

var promise = new Promise(initialFunction) var anotherPromise = promise.then(functionB) var yetAnotherPromise = anotherPromise.then(functionC) ...

Page 70: It's the end of design patterns as we know it (and i feel fine)

Java 8Optional

Page 71: It's the end of design patterns as we know it (and i feel fine)

String version = computer .getSoundcard() .getUSB() .getVersion();

Page 72: It's the end of design patterns as we know it (and i feel fine)

String version = "UNKNOWN"; if(computer != null){ Soundcard soundcard = computer.getSoundcard(); if(soundcard != null){ USB usb = soundcard.getUSB(); if(usb != null){ version = usb.getVersion(); } } }

Page 73: It's the end of design patterns as we know it (and i feel fine)

String name = Optional.of(computer) .map(Computer::getSoundcard)

.map(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN");

Page 74: It's the end of design patterns as we know it (and i feel fine)

String name = Optional.of(computer) .map(Computer::getSoundcard)

.map(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN");

Page 75: It's the end of design patterns as we know it (and i feel fine)

String name = Optional.of(computer) .map(Computer::getSoundcard)

.map(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN");

Page 76: It's the end of design patterns as we know it (and i feel fine)

Promises and Optional are Monads

Page 77: It's the end of design patterns as we know it (and i feel fine)

A Monad is a structure that puts a value in a

computational context

Page 78: It's the end of design patterns as we know it (and i feel fine)

Monad<A> unit(A a)

Page 79: It's the end of design patterns as we know it (and i feel fine)

Monad<A> unit(A a)

Page 80: It's the end of design patterns as we know it (and i feel fine)

Monad<A> unit(A a)

Page 81: It's the end of design patterns as we know it (and i feel fine)

Monad<B> bind(M<A> ma, Function<A, M<B>> f)

Page 82: It's the end of design patterns as we know it (and i feel fine)

Monad<B> bind(M<A> ma, Function<A, M<B>> f)

Page 83: It's the end of design patterns as we know it (and i feel fine)

Monad<B> bind(M<A> ma, Function<A, M<B>> f)

Page 84: It's the end of design patterns as we know it (and i feel fine)

unit(x) = Monad x unit(x).bind(f) = Monad f(x) unit(x).bind(f).bind(g) = Monad g(f(x))

Monad: Value, Operations and Rules

Page 85: It's the end of design patterns as we know it (and i feel fine)

HaskellI/O

Page 86: It's the end of design patterns as we know it (and i feel fine)

return :: Monad m => a -> m a (>>=) :: Monad m => m a -> (a -> m b) -> m b (>>) :: Monad m => m a -> m b -> m b

Unit & Bind in Haskell

Page 87: It's the end of design patterns as we know it (and i feel fine)

main :: IO () main = putStrLn "What is your name?" >> getLine >>= \name -> putStrLn ("Hello, " ++ name)

I/O Example

Page 88: It's the end of design patterns as we know it (and i feel fine)

main :: IO () main = do putStrLn "What is your name?" name <- getLine putStrLn ("Hello, " ++ name)

Sequence

Page 89: It's the end of design patterns as we know it (and i feel fine)

main :: IO () main = do putStrLn "What is your name?" name <- getLine putStrLn ("Hello, " ++ name)

What is that?

Page 90: It's the end of design patterns as we know it (and i feel fine)

Imperative codein functional style

Page 91: It's the end of design patterns as we know it (and i feel fine)

PARADIGM MIX,stupid!

It’s the

Page 92: It's the end of design patterns as we know it (and i feel fine)
Page 93: It's the end of design patterns as we know it (and i feel fine)
Page 94: It's the end of design patterns as we know it (and i feel fine)

One more thing…

Page 95: It's the end of design patterns as we know it (and i feel fine)

BuilderResult<Group> groupBuilder = new Builder<Group>(Group.class) .daoContext(daoContext) .user(loginUser) .comments(body.getCreateComments()) .set(Field.name).with(name).validations(required(), unique()) .set(Field.owners).with(ownerIds).converting(convertToOwner()) .validations(required()) .set(Field.description).with(body.getDesc()).withoutValidation() .build();

Accidental Monad

Page 96: It's the end of design patterns as we know it (and i feel fine)

"Six of one or half dozen of the other"

Page 97: It's the end of design patterns as we know it (and i feel fine)

Set<PersistenceEventListener> result = new HashSet<>(); for (String channel: channels) { List<PersistenceEventListener> channelListeners = listenersPerChannel.get(channel); if (channelListeners != null) { result.addAll(channelListeners); } } return result;

return listenersPerChannel.entrySet().stream() .filter(e -> Arrays.asList(channels).contains(e.getKey())) .map(e -> e.getValue()) .flatMap(l -> l.stream()) .collect(Collectors.toSet());

X

Page 98: It's the end of design patterns as we know it (and i feel fine)

Moral of the Story

Page 99: It's the end of design patterns as we know it (and i feel fine)

Languages are evolving

Page 100: It's the end of design patterns as we know it (and i feel fine)

Programmers must evolve along with the languages and

enjoy learning new things

Page 101: It's the end of design patterns as we know it (and i feel fine)

Luiz Borba http://borba.blog.br

It’s the end of Design Patterns as we know it (and I feel fine)

Thank you!