-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
307 lines (282 loc) · 8.97 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Functional Async</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<script src="https://use.fontawesome.com/761b045573.js"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>FRP</h1>
<h3>o Como aprendí a dejar de preocuparme y amar las mónadas 🐒</h3>
</section>
<section>
<h2>Composición y dependencias de tareas</h2>
<li class="fragment">Tarea A: Llamada a un servicio rest A <br>
<div style ="text-align: center">
<i class="fa fa-arrow-down" aria-hidden="true"></i>
</div>
</li>
<li class="fragment">Tarea B: Llamada a un servicio rest B <br>
<div style ="text-align: center">
<i class="fa fa-arrow-down" aria-hidden="true"></i>
</div>
</li>
<li class="fragment">Tarea C: Llamada a un servicio rest C</li>
</ul>
</section>
<section>
<h2>Ejecución bloqueante</h2>
<pre><code class="scala hljs" data-trim>
val state = restApi.pay(sale)
val result = crm.register(state, sale)
val sent = smtp.send(sale)
//…
println(state + result + sent)
</code></pre>
</section>
<section>
<h2>Welcome to Callback Hell</h2>
<pre><code class="java hljs" data-trim>
// get f4/f5 after dependency f2 completes
executor.execute(new CallToRemoteServiceB(new Callback<Integer>() {
@Override
public void call(Integer f2) {
executor.execute(new CallToRemoteServiceD(new Callback<Integer>() {
@Override
public void call(Integer f4) {
f4Value.set(f4);
latch.countDown();
}
}, f2));
executor.execute(new CallToRemoteServiceE(new Callback<Integer>() {
@Override
public void call(Integer f5) {
f5Value.set(f5);
latch.countDown();
}
}, f2));
}
}));
</code></pre>
</section>
<section>
<p>Existe una manera mas fácil?</p>
<p class="fragment">Ya que estamos, una manera mas funcional<br>
<img style="background: none !important; border: none; box-shadow: none; width:20%; height:auto;" src="lambda_logo.svg" alt="lambda question">
</p>
</section>
<section>
<h2>Mónadas!🙉</h2>
<p class="fragment fade-out">Un endofunctor con dos transformaciones naturales</p>
<ul class="fragment">
<li>Un "Constructor" (unit)</li>
<li>Una función FlatMap (bind)</li>
</ul>
<p class="fragment">Representan un contexto</p>
</section>
<section>
<section>
<h2>Mónadas que ya usamos</h2>
<table>
<thead>
<tr>
<th></th>
<th></th>
<th>unit</th>
<th>bind</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="2">Java</td>
<td style="font-weight: bold;">Optional<T></td>
<td>Optional.of</td>
<td>opt.flatMap</td>
</tr>
<tr>
<td style="font-weight: bold;">Stream<T></td>
<td>Stream.of</td>
<td>strm.flatMap</td>
</tr>
<tr>
<td rowspan="2">Scala</td>
<td style="font-weight: bold;">Either[A,B]</td>
<td>Right() o Left()</td>
<td>either.flatMap</td>
</tr>
<tr>
<td style="font-weight: bold;">Try[A]</td>
<td>Try{}</td>
<td>try.flatMap</td>
</tr>
<tr>
<td>C#</td>
<td style="font-weight: bold;">List<T></td>
<td>new List()</td>
<td>list.SelectMany</td>
</tr>
</tbody>
</table>
</section>
<section>
<h2>Mónada Option</h2>
<pre><code class="scala hljs" data-trim>
val v = map.get("key")
var res: String = _
if(v != null && v == "value" && map.containsKey(value)){
res = map.get(v)
}else{
res = "not found"
}</code></pre>
<div class="fragment">
<i class="fa fa-angle-double-down" aria-hidden="true"></i>
<pre><code class="scala hljs" data-trim>
val res = map.get("key")
.filter( _ == "value" )
.flatMap( s => map.get(s) )
.getOrElse("not found")</code></pre>
</div>
</section>
</section>
<section>
<ul>
<h3>Monada para tareas asincronas</h3>
<li class="fragment">El contexto es el tiempo</li>
<li class="fragment">Componibles para nunca 'bloquear'</li>
</ul>
</section>
<section>
<section>
<h3>Ejemplos</h3>
<ul style="float:left;">
<li class="fragment">
Java: CompletableFuture<T>
<ul>
<li>supplyAsync</li>
<li>thenComposeAsync</li>
</ul>
</li>
<li class="fragment">JS Promise*
<ul>
<li>new Promise()</li>
<li>then</li>
</ul>
</li>
</ul>
<ul>
<li class="fragment">Scala: Future[T]
<ul>
<li>Future{}</li>
<li>flatMap</li>
</ul>
</li>
<li class="fragment">C#: Task<T>
<ul>
<li>FromResult</li>
<li>Then**</li>
</ul>
</li>
</ul>
</section>
<section>
<p>Task de C# no provee un method bind pero se puede realizar facilmente</p>
<pre><code class="cs hljs">public static async Task<V> Bind<U,V>(
this Task<U> m, Func<U,Task<V>> f)
{
return await f(await m);
}
</code></pre>
<small>Link: <a href="https://blogs.msdn.microsoft.com/pfxteam/2013/04/03/tasks-monads-and-linq/"><i>Tasks, Monads, and LINQ</i></a></small>
</section>
</section>
<section>
<h2>Implementación</h2>
<pre><code class="java hljs" data-trim>
CompletableFuture<String> posibleResult = asyncRestApi.pay(sale)
.thenComposeAsync(res -> asyncCrm.register(res, sale))
.thenComposeAsync(res -> asyncSmtp.send(sale));
posibleResult.thenAcceptAsync(System.out::println);
</code></pre>
</section>
<section>
<h2>JS</h2>
<pre><code class="javascript hljs" data-trim>
"use strict";
const rp = require('request-promise')
const user = 'ignaciopl'
class MyUrl {
constructor (url) {
this.url = url;
}
toOption(){
return {
uri: this.url,
headers:{'User-Agent': 'Request-Promise'},
json: true
}
}
}
let options = new MyUrl(`https://api.github.com/users/${user}`)
console.log('Start')
rp(options.toOption()).then( s => s.repos_url )
.then( url => new MyUrl(url) )
.then( url => rp(url.toOption()) )
.then( repos => console.log(`user ${user} has ${repos.length} repos`) )
console.log('Finish')
</code></pre>
</section>
<section>
<h2>Scala</h2>
<pre><code class="scala hljs" data-trim>
val res = for{
res1 <- asyncRestApi.pay(sale)
res2 <- asyncCrm.register(res1,sale)
res3 <- asyncSmtp.send(sale)
}yield{
res3
}
res.forEach(println)
</code></pre>
</section>
<section>
<h2>Muchas Gracias!!</h2>
</section>
<section>
<h2>¿Preguntas?</h2>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>