Contenu connexe Similaire à It's the end of design patterns as we know it (and i feel fine) (20) It's the end of design patterns as we know it (and i feel fine)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
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. 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. 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. 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. 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. 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. "
I always knew that one day
Smalltalk
would replace Java.
I just didn’t know
it would be called
Ruby
"
(Kent Beck)
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)
22. 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)
36. subtract
abstractions
Lisp
ML
Haskell
functional languages
imperative languages
Java
C
Fortran
C++
add
abstractions
Scala
Java 8
C#
F#
hybrid languages
40. Haskell:
[x | x <- xs, x < 10]
C#:
from x in xs where x < 10 select x;
41. Haskell:
[x | x <- xs, x < 10]
C#:
from x in xs where x < 10 select x;
Java 8:
xs.stream().filter(x -> x < 10)
43. Erlang
Javascript
Java 8
Swift
Objective-C
C#
C
PHP
Python
Ruby
Dart
Elixir
Coffeescript
Scala
Go
Clojure
Rust
Groovy
C++
Haskell
F#
44. Erlang
Javascript
Java 8
Swift
Objective-C
C#
C
PHP
Python
Ruby
Dart
Elixir
Coffeescript
Scala
Go
Clojure
Rust
Groovy
C++
Haskell
F#
49. Once you understand
Monadsfor yourself
you lose the ability
to explain them to others
(Douglas Crockford)
"
"
50. A Monad is just
a monoid in the
category of
endofunctors
57. Rules
+ =
stack.push(value).top == value
stack.isEmpty == true
stack.push(value).isEmpty == false
58. and..
We can implement them
however we like
We can use them without
knowing their implementation
64. 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]);
}
}
});
}
65. 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?
});
});
});
66. 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();
67. var promise = new Promise(initialFunction)
var anotherPromise = promise.then(functionB)
var yetAnotherPromise = anotherPromise.then(functionC)
...
68. var promise = new Promise(initialFunction)
var anotherPromise = promise.then(functionB)
var yetAnotherPromise = anotherPromise.then(functionC)
...
69. var promise = new Promise(initialFunction)
var anotherPromise = promise.then(functionB)
var yetAnotherPromise = anotherPromise.then(functionC)
...
72. String version = "UNKNOWN";
if(computer != null){
Soundcard soundcard = computer.getSoundcard();
if(soundcard != null){
USB usb = soundcard.getUSB();
if(usb != null){
version = usb.getVersion();
}
}
}
73. String name = Optional.of(computer)
.map(Computer::getSoundcard)
.map(Soundcard::getUSB)
.map(USB::getVersion)
.orElse("UNKNOWN");
74. String name = Optional.of(computer)
.map(Computer::getSoundcard)
.map(Soundcard::getUSB)
.map(USB::getVersion)
.orElse("UNKNOWN");
75. String name = Optional.of(computer)
.map(Computer::getSoundcard)
.map(Soundcard::getUSB)
.map(USB::getVersion)
.orElse("UNKNOWN");
77. A Monad is a structure
that puts a value in a
computational context
84. 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))
86. 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
87. I/O Example
main :: IO ()
main =
putStrLn "What is your name?" >>
getLine >>=
name -> putStrLn ("Hello, " ++ name)
88. Sequence
main :: IO ()
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Hello, " ++ name)
89. What is that?
main :: IO ()
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Hello, " ++ name)
95. 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();
97. 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());
101. It’s the end of Design Patterns
as we know it (and I feel fine)
Thank you!
Luiz Borba
http://borba.blog.br