Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
It’s the end of 
Design Patterns 
as we know it 
(and I feel fine) 
Today: 
Introducing Monads 
Luiz Borba 
http://borba.b...
Part 1 
Deconstructing 
Design Patterns
1994
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
" 
I always knew that one day 
Smalltalk 
would replace Java. 
I just didn’t know 
it would be called 
Ruby 
" 
(Kent Beck...
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
It’s the 
Mainstream, 
stupid!
Always 
Controversial
Ralph Johnson and Singleton
Are 
Design Patterns 
missing languages features?
Are 
Design Patterns 
how languages evolve? 
(Jeff Atwood)
COBOL (61) 
FORTRAN (57) 
CLIPPER (85) 
C (72) 
SMALLTALK (72) 
C++ (83) 
PASCAL (86) 
LISP (58) 
PERL (87) 
MS BASIC (75)...
2010
imperative languages 
Java 
C 
Fortran 
C++ 
add 
abstractions
subtract 
abstractions 
Lisp 
ML 
Haskell 
functional languages 
imperative languages 
Java 
C 
Fortran 
C++ 
add 
abstrac...
subtract 
abstractions 
Lisp 
ML 
Haskell 
functional languages 
imperative languages 
Java 
C 
Fortran 
C++ 
add 
abstrac...
Haskell: 
[x | x <- xs, x < 10]
Haskell: 
[x | x <- xs, x < 10] 
C#: 
from x in xs where x < 10 select x;
Haskell: 
[x | x <- xs, x < 10] 
C#: 
from x in xs where x < 10 select x; 
Java 8: 
xs.stream().filter(x -> x < 10)
2014
Erlang 
Javascript 
Java 8 
Swift 
Objective-C 
C# 
C 
PHP 
Python 
Ruby 
Dart 
Elixir 
Coffeescript 
Scala 
Go 
Clojure 
...
Erlang 
Javascript 
Java 8 
Swift 
Objective-C 
C# 
C 
PHP 
Python 
Ruby 
Dart 
Elixir 
Coffeescript 
Scala 
Go 
Clojure 
...
We need to rewrite the 
design patterns book
Part 2 
Introducing Monads
Once you understand 
Monadsfor yourself 
you lose the ability 
to explain them to others 
(Douglas Crockford) 
" 
"
A Monad is just 
a monoid in the 
category of 
endofunctors
WTF?
WARM UP
What’s a 
Stack?
Value
Operations
Rules 
+ = 
stack.push(value).top == value 
stack.isEmpty == true 
stack.push(value).isEmpty == false
and.. 
We can implement them 
however we like 
We can use them without 
knowing their implementation
What’s a 
Queue?
What are 
Stack & 
Queue?
ABSTRACT 
TYPES 
DATA
Javascript 
Promises
Asynchronous 
programming is 
becoming the norm in 
modern, 
connected applications" 
(Anders Hejlsberg) 
"
var FS = require('fs'), 
request = require('request'); 
function getResults(pathToFile, callback) { 
FS.readFile(pathToFil...
asyncCall(function(err, data1){ 
if(err) return callback(err); 
anotherAsyncCall(function(err2, data2){ 
if(err2) return c...
asyncCall() 
.then(function(data1){ 
// do something... 
return anotherAsyncCall(); 
}) 
.then(function(data2){ 
// do som...
var promise = new Promise(initialFunction) 
var anotherPromise = promise.then(functionB) 
var yetAnotherPromise = anotherP...
var promise = new Promise(initialFunction) 
var anotherPromise = promise.then(functionB) 
var yetAnotherPromise = anotherP...
var promise = new Promise(initialFunction) 
var anotherPromise = promise.then(functionB) 
var yetAnotherPromise = anotherP...
Java 8 
Optional
String version = computer 
.getSoundcard() 
.getUSB() 
.getVersion();
String version = "UNKNOWN"; 
if(computer != null){ 
Soundcard soundcard = computer.getSoundcard(); 
if(soundcard != null){...
String name = Optional.of(computer) 
.map(Computer::getSoundcard) 
.map(Soundcard::getUSB) 
.map(USB::getVersion) 
.orElse...
String name = Optional.of(computer) 
.map(Computer::getSoundcard) 
.map(Soundcard::getUSB) 
.map(USB::getVersion) 
.orElse...
String name = Optional.of(computer) 
.map(Computer::getSoundcard) 
.map(Soundcard::getUSB) 
.map(USB::getVersion) 
.orElse...
Promises and Optional 
are Monads
A Monad is a structure 
that puts a value in a 
computational context
Monad<A> unit(A a)
Monad<A> unit(A a)
Monad<A> unit(A a)
Monad<B> bind(M<A> ma, 
Function<A, M<B>> f)
Monad<B> bind(M<A> ma, 
Function<A, M<B>> f)
Monad<B> bind(M<A> ma, 
Function<A, M<B>> f)
Monad: 
Value, Operations and Rules 
unit(x) = Monad x 
unit(x).bind(f) = Monad f(x) 
unit(x).bind(f).bind(g) = Monad g(f(...
Haskell 
I/O
Unit & Bind in Haskell 
return :: Monad m => a -> m a 
(>>=) :: Monad m => m a -> (a -> m b) -> m b 
(>>) :: Monad m => m ...
I/O Example 
main :: IO () 
main = 
putStrLn "What is your name?" >> 
getLine >>= 
name -> putStrLn ("Hello, " ++ name)
Sequence 
main :: IO () 
main = do 
putStrLn "What is your name?" 
name <- getLine 
putStrLn ("Hello, " ++ name)
What is that? 
main :: IO () 
main = do 
putStrLn "What is your name?" 
name <- getLine 
putStrLn ("Hello, " ++ name)
Imperative code 
in functional style
PARADIGM MIX, 
stupid! 
It’s the
One more thing…
Accidental Monad 
BuilderResult<Group> groupBuilder = new Builder<Group>(Group.class) 
.daoContext(daoContext) 
.user(logi...
"Six of one or half 
dozen of the other"
Set<PersistenceEventListener> result = new HashSet<>(); 
for (String channel: channels) { 
List<PersistenceEventListener> ...
Moral of the Story
Languages are evolving
Programmers must evolve 
along with the languages and 
enjoy learning new things
It’s the end of Design Patterns 
as we know it (and I feel fine) 
Thank you! 
Luiz Borba 
http://borba.blog.br
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Upcoming SlideShare
Loading in …5
×

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

2,385 views

Published on

The Design Patterns Book, released in 1994 is even today very influential in how we designed our apps. However, I think the influence is about to end In the last 20 years the mainstream programming languages are object oriented and imperative and right now, they are oo, imperative and functional. The functional paradigm suggest different patterns for the problems stated in the book. In the second part of presentation, I try to explain Monads, as an example of a functional pattern.

Published in: Technology
  • Be the first to comment

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

  1. 1. It’s the end of Design Patterns as we know it (and I feel fine) Today: Introducing Monads Luiz Borba http://borba.blog.br
  2. 2. Part 1 Deconstructing Design Patterns
  3. 3. 1994
  4. 4. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88) PHP (95)
  5. 5. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88) PHP (95)
  6. 6. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88) DESKTOP PHP (95)
  7. 7. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88) WEB PHP (95)
  8. 8. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88) PHP (95)
  9. 9. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88) PHP (95)
  10. 10. " I always knew that one day Smalltalk would replace Java. I just didn’t know it would be called Ruby " (Kent Beck)
  11. 11. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88)
  12. 12. It’s the Mainstream, stupid!
  13. 13. Always Controversial
  14. 14. Ralph Johnson and Singleton
  15. 15. Are Design Patterns missing languages features?
  16. 16. Are Design Patterns how languages evolve? (Jeff Atwood)
  17. 17. COBOL (61) FORTRAN (57) CLIPPER (85) C (72) SMALLTALK (72) C++ (83) PASCAL (86) LISP (58) PERL (87) MS BASIC (75) OBJECT PASCAL (95) ML (73) HASKELL (90) JAVA (95) RUBY (95) PYTHON (91) ALGOL (58) POWERBUILDER (92) BASIC (64) VISUAL BASIC (91) SQLWINDOWS (88)
  18. 18. 2010
  19. 19. imperative languages Java C Fortran C++ add abstractions
  20. 20. subtract abstractions Lisp ML Haskell functional languages imperative languages Java C Fortran C++ add abstractions
  21. 21. subtract abstractions Lisp ML Haskell functional languages imperative languages Java C Fortran C++ add abstractions Scala Java 8 C# F# hybrid languages
  22. 22. Haskell: [x | x <- xs, x < 10]
  23. 23. Haskell: [x | x <- xs, x < 10] C#: from x in xs where x < 10 select x;
  24. 24. Haskell: [x | x <- xs, x < 10] C#: from x in xs where x < 10 select x; Java 8: xs.stream().filter(x -> x < 10)
  25. 25. 2014
  26. 26. Erlang Javascript Java 8 Swift Objective-C C# C PHP Python Ruby Dart Elixir Coffeescript Scala Go Clojure Rust Groovy C++ Haskell F#
  27. 27. Erlang Javascript Java 8 Swift Objective-C C# C PHP Python Ruby Dart Elixir Coffeescript Scala Go Clojure Rust Groovy C++ Haskell F#
  28. 28. We need to rewrite the design patterns book
  29. 29. Part 2 Introducing Monads
  30. 30. Once you understand Monadsfor yourself you lose the ability to explain them to others (Douglas Crockford) " "
  31. 31. A Monad is just a monoid in the category of endofunctors
  32. 32. WTF?
  33. 33. WARM UP
  34. 34. What’s a Stack?
  35. 35. Value
  36. 36. Operations
  37. 37. Rules + = stack.push(value).top == value stack.isEmpty == true stack.push(value).isEmpty == false
  38. 38. and.. We can implement them however we like We can use them without knowing their implementation
  39. 39. What’s a Queue?
  40. 40. What are Stack & Queue?
  41. 41. ABSTRACT TYPES DATA
  42. 42. Javascript Promises
  43. 43. Asynchronous programming is becoming the norm in modern, connected applications" (Anders Hejlsberg) "
  44. 44. 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]); } } }); }
  45. 45. 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? }); }); });
  46. 46. 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();
  47. 47. var promise = new Promise(initialFunction) var anotherPromise = promise.then(functionB) var yetAnotherPromise = anotherPromise.then(functionC) ...
  48. 48. var promise = new Promise(initialFunction) var anotherPromise = promise.then(functionB) var yetAnotherPromise = anotherPromise.then(functionC) ...
  49. 49. var promise = new Promise(initialFunction) var anotherPromise = promise.then(functionB) var yetAnotherPromise = anotherPromise.then(functionC) ...
  50. 50. Java 8 Optional
  51. 51. String version = computer .getSoundcard() .getUSB() .getVersion();
  52. 52. String version = "UNKNOWN"; if(computer != null){ Soundcard soundcard = computer.getSoundcard(); if(soundcard != null){ USB usb = soundcard.getUSB(); if(usb != null){ version = usb.getVersion(); } } }
  53. 53. String name = Optional.of(computer) .map(Computer::getSoundcard) .map(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN");
  54. 54. String name = Optional.of(computer) .map(Computer::getSoundcard) .map(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN");
  55. 55. String name = Optional.of(computer) .map(Computer::getSoundcard) .map(Soundcard::getUSB) .map(USB::getVersion) .orElse("UNKNOWN");
  56. 56. Promises and Optional are Monads
  57. 57. A Monad is a structure that puts a value in a computational context
  58. 58. Monad<A> unit(A a)
  59. 59. Monad<A> unit(A a)
  60. 60. Monad<A> unit(A a)
  61. 61. Monad<B> bind(M<A> ma, Function<A, M<B>> f)
  62. 62. Monad<B> bind(M<A> ma, Function<A, M<B>> f)
  63. 63. Monad<B> bind(M<A> ma, Function<A, M<B>> f)
  64. 64. Monad: Value, Operations and Rules unit(x) = Monad x unit(x).bind(f) = Monad f(x) unit(x).bind(f).bind(g) = Monad g(f(x))
  65. 65. Haskell I/O
  66. 66. Unit & Bind in Haskell return :: Monad m => a -> m a (>>=) :: Monad m => m a -> (a -> m b) -> m b (>>) :: Monad m => m a -> m b -> m b
  67. 67. I/O Example main :: IO () main = putStrLn "What is your name?" >> getLine >>= name -> putStrLn ("Hello, " ++ name)
  68. 68. Sequence main :: IO () main = do putStrLn "What is your name?" name <- getLine putStrLn ("Hello, " ++ name)
  69. 69. What is that? main :: IO () main = do putStrLn "What is your name?" name <- getLine putStrLn ("Hello, " ++ name)
  70. 70. Imperative code in functional style
  71. 71. PARADIGM MIX, stupid! It’s the
  72. 72. One more thing…
  73. 73. Accidental Monad 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();
  74. 74. "Six of one or half dozen of the other"
  75. 75. Set<PersistenceEventListener> result = new HashSet<>(); for (String channel: channels) { List<PersistenceEventListener> channelListeners = listenersPerChannel.get(channel); if (channelListeners != null) { result.addAll(channelListeners); } } return result; X return listenersPerChannel.entrySet().stream() .filter(e -> Arrays.asList(channels).contains(e.getKey())) .map(e -> e.getValue()) .flatMap(l -> l.stream()) .collect(Collectors.toSet());
  76. 76. Moral of the Story
  77. 77. Languages are evolving
  78. 78. Programmers must evolve along with the languages and enjoy learning new things
  79. 79. It’s the end of Design Patterns as we know it (and I feel fine) Thank you! Luiz Borba http://borba.blog.br

×