-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
388 lines (369 loc) · 13.6 KB
/
index.html
File metadata and controls
388 lines (369 loc) · 13.6 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<!doctype html>
<html lang="es">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=1024" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<title>Lodash FP</title>
<meta name="description" content="Lodash for FP" />
<meta name="author" content="Sebastián Schuchhardt" />
<link href="http://fonts.googleapis.com/css?family=Open+Sans:regular,semibold,italic,italicsemibold|PT+Sans:400,700,400italic,700italic|PT+Serif:400,700,400italic,700italic" rel="stylesheet" />
<link href="css/impress-demo.css" rel="stylesheet" />
<link rel="shortcut icon" href="favicon.png" />
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
<link href='http://fonts.googleapis.com/css?family=Oxygen+Mono' rel='stylesheet' type='text/css'>
</head>
<body class="impress-not-supported">
<div class="fallback-message">
<p>Your browser <b>doesn't support the features required</b> by impress.js, so you are presented with a simplified version of this presentation.</p>
<p>For the best experience please use the latest <b>Chrome</b>, <b>Safari</b> or <b>Firefox</b> browser.</p>
</div>
<div id="impress">
<!-- <article> -->
<section id="main" class="step" data-x="0" data-y="0">
<header>
<h1>Programacion Funcional en Javascript</h1>
<h2>con Lodash.js</h2>
</header>
<pre class="prettyprint">
<code class="language-javascript">
import _ from 'lodash';
// OR
import find from 'lodash/find';
</code>
</pre>
</section>
<section id="values" class="step" data-x="0" data-y="1000">
<header>
<h1>Las Funciones son Valores</h1>
</header>
<p>En un lenguaje donde las funciones pueden ser valores son llamadas <em>first class functions</em></p>
<p>Funciones pueden ser asignadas a variables y pasadas como argumentos como cualquier otro valor primitivo</p>
<pre class="prettyprint">
<code class="language-javascript">
const add = (x, y) => x + y;
</code>
</pre>
</section>
<section id="pure" class="step" data-x="0" data-y="2000">
<header>
<h1>Funciones puras</h1>
<h3><em>pure functions</em></h3>
</header>
<p>Una funcion pura tiene solo entradas y salidas</p>
<p>Una funcion pura no muta/cambia a ningun estado</p>
</section>
<section id="pure-code1" class="step" data-x="-500" data-y="2500" data-z="-500">
<pre class="prettyprint">
<code class="language-javascript">
const someArray = [1, 2, 3];
someArray.push(4);</code>
</pre>
<p>
This mutates <code class="language-javascript inline-snippet">someArray</code>.
</p>
</section>
<section id="pure-code2" class="step" data-x="500" data-y="2500" data-z="-500">
<pre class="prettyprint">
<code class="language-javascript">
const purePush = (item, array) => {
const newArray = array.slice();
newArray.push(item);
return newArray;
};
purePush(4, someArray);</code>
</pre>
<p>
Esto retorna un <em>nuevo</em> array y deja
<code class="language-javascript inline-snippet">someArray</code> sin modificacion
</p>
</section>
<section id="higher-order" class="step" data-x="0" data-y="3300">
<header>
<h1>Higher Order Functions</h1>
</header>
<p>Una funcion de Alto Orden toma una función como parametro </p>
<pre class="prettyprint">
<code class="language-javascript">
const add1 = (x) => x + 1
const arr = [1, 2, 3]
arr.map(add1)
[2, 3, 4]</code>
</pre>
</section>
<section id="currying" class="step" data-x="0" data-y="4000">
<header>
<h1>Currying</h1>
<h4><em>Llamado parcial de funciones</em></h4>
</header>
<p>Cuando no se pasan todos los argumentos de la función:</p>
<ul>
<li>Una funcion normal en Javascript sustituye por undefined el resto</li>
<li>Una curried function returns a <em>partially applied function</em> that accepts the rest</li>
<li>A curried function returns a <em>partially applied function</em> that accepts the rest</li>
</ul>
</section>
<section id="currying-code" class="step" data-x="0" data-y="4500" data-z="-500">
<pre class="prettyprint">
<code class="language-javascript">
> const add = (x) => {
return (y) => {
return x + y
};
};
> const add2 = add(2);
> add2(5)
7</code>
</pre>
</section>
<section id="order" class="step" data-x="0" data-y="5100">
<header>
<h1>Argument Order</h1>
</header>
<p>Los argumentos iniciales de una funcion son como una configuracion</p>
<p>El ultimo es usualmente la data con la cual operar</p>
<p>Esto hace mas facil crear funciones especializadas con currying</p>
</section>
<section id="order-example1" class="step" data-x="-1200" data-y="5000" data-z="-500">
<pre class="prettyprint">
<code class="language-javascript">
var getNamed = R.filter(R.has("name"));
> getNamed([
{id: 0, name: "foo"},
{id: 1},
{id: 2, name: "bar"}
]);
[{id: 0, name: "foo"},
{id: 2, name: "bar"}]</code>
</pre>
</section>
<section id="order-example2" class="step" data-x="-600" data-y="5700" data-z="-500">
<pre class="prettyprint">
<code class="language-javascript">
var firstNamed = R.find(R.has("name"));
> firstNamed([
{id: 0, name: "foo"},
{id: 1},
{id: 2, name: "bar"}
]);
{id: 0, name: "foo"}
</code>
</pre>
</section>
<section id="order-example3" class="step" data-x="600" data-y="5700" data-z="-500">
<pre class="prettyprint">
<code class="language-javascript">
var isEven = function(x) {
return x % 2 === 0;
};
var evensOnly = R.filter(isEven);
> evensOnly([1, 2, 3, 4, 5]);
[2, 4]</code>
</pre>
</section>
<section id="order-example4" class="step" data-x="1200" data-y="5000" data-z="-500">
<pre class="prettyprint">
<code class="language-javascript">
var duplicate = function(x) {
return [x, x];
};
var duplicateEach = R.chain(duplicate);
> duplicateEach([1, 2, 3]);
[1, 1, 2, 2, 3, 3]</code>
</pre>
</section>
<section id="breather" class="step" data-x="0" data-y="6500">
<header>
<h1>Thus Ends Section One</h1>
</header>
<p>Let's take a quick break now</p>
</section>
<section id="composition-section" class="step" data-x="0" data-y="7000">
<header>
<h1>Function Composition</h2>
<h3><em>putting it all together</em></h3>
</header>
</section>
<section id="composition" class="step" data-x="0" data-y="7700">
<header>
<h1>Mathematical Functions</h1>
</header>
<p>
A function of one argument <code class="language-javascript inline-snippet">f(x)</code>
and another function of one argument <code class="language-javascript inline-snippet">g(x)</code>
can be <em>composed together</em> to make the function <code class="language-javascript inline-snippet">f(g(x))</code>
</p>
</section>
<section id="composition-example" class="step" data-x="0" data-y="8500">
<pre class="prettyprint">
<code class="language-javascript">
> duplicateEach([1, 2, 3]);
[1, 1, 2, 2, 3, 3]
> evensOnly([1, 2, 3, 4, 5]);
[2, 4]
var duplicateEvens = R.compose(
duplicateEach,
evensOnly
);
> duplicateEvens([1, 2, 3, 4, 5]);
[2, 2, 4, 4]</code>
</pre>
</section>
<section id="exercises-main" class="step" data-x="0" data-y="9300">
<header>
<h1>Exercises!</h1>
<h4><em>not the sweaty kind</em><h4>
</header>
<p>Let's tackle some exercises and apply these concepts</p>
<p>Suppose we're working with a JSON API that provides a giant catalog of users.</p>
<p>
Each user has an <code class="language-javascript inline-snippet">id</code>,
a <code class="language-javascript inline-snippet">name</code>, and
an <code class="language-javascript inline-snippet">age</code>
</p>
</section>
<section id="exercises-catalog" class="step" data-x="0" data-y="10000">
<pre class="prettyprint">
<code class="language-javascript">
{
users: [
{id: 3, name: "Thad", age: 36},
{id: 5, name: "Lucian", age: 23},
{id: 2, name: "Justine", age: 29},
{id: 4, name: "Katie", age: 26},
{id: 0, name: "Jerold", age: 52},
{id: 1, name: "Nona", age: 33}
]
}</code>
</pre>
<p>
You can find this catalog in the "exercises" folder of the
class GitHub repository.
</p>
</section>
<section id="exercise1" class="step" data-x="-600" data-y="10800">
<header>
<h1>Sorting by ID</h1>
<h3><code class="language-javascript inline-snippet">npm run-script ex1</code><h3>
</header>
<p>It would be useful to see a list of users sorted by ID</p>
<p>
Ramda provides a <code class="language-javascript inline-snippet">sortBy</code>
function to make this easier.
</p>
<p>Note: it's a higher order function!</p>
</section>
<section id="exercise1-answer" class="step" data-x="600" data-y="10800">
<pre class="prettyprint">
<code class="language-javascript">
var sortCatalogUsersById = R.compose(
R.sortBy(R.prop("id")),
R.prop("users")
);</code>
</pre>
</section>
<section id="exercise2" class="step" data-x="600" data-y="11600">
<header>
<h1>Users With an Even Age</h1>
<h3><code class="language-javascript inline-snippet">npm run-script ex2</code><h3>
</header>
<p>Now for some filtering</p>
<p>Let's say we want only the users in the catalog with an even age</p>
<p>
Tip - if you've got <code class="language-javascript inline-snippet">isEven</code>,
check out Ramda's <code class="language-javascript inline-snippet">propSatisfies</code>.
</p>
</section>
<section id="exercise2-answer" class="step" data-x="-600" data-y="11600">
<pre class="prettyprint">
<code class="language-javascript">
var isEven = function(x) {
return x % 2 === 0
};
var hasEvenAge = R.propSatisfies(
isEven,
"age"
);
var getUsersWithEvenAge = R.compose(
R.filter(hasEvenAge),
R.prop("users")
);</code>
</pre>
</section>
<section id="exercise3" class="step" data-x="-600" data-y="12400">
<header>
<h1>Names of the Three Youngest Users</h1>
<h3><code class="language-javascript inline-snippet">npm run-script ex3</code><h3>
</header>
<p>Instead of sorting by ID, let's sort by age</p>
<p>Additionally, let's get the 3 youngest users instead of all of them</p>
<p>As a final twist - let's only get their names, not the whole user objects</p>
</section>
<section id="exercise3-answer" class="step" data-x="600" data-y="12400">
<pre class="prettyprint">
<code class="language-javascript">
var getNamesOfThreeYoungestUsers =
R.compose(
R.map(R.prop("name")),
R.take(3),
R.sortBy(R.prop("age")),
R.prop("users")
);</code>
</pre>
</section>
<section id="exercise4" class="step" data-x="600" data-y="13200">
<header>
<h1>Users in their Thirties</h1>
<h3><code class="language-javascript inline-snippet">npm run-script ex4</code><h3>
</header>
<p>
Now a somewhat more complex query - let's get all
the users with age 30-39
</p>
<p>
Ramda provides <code class="language-javascript inline-snippet">R.lt</code>,
and <code class="language-javascript inline-snippet">R.gt</code>
for comparing, but their argument order makes things tricky
</p>
<p>
Maybe try
<code class="language-javascript inline-snippet">R.flip</code>
or <code class="language-javascript inline-snippet">R.__</code>
</p>
</section>
<section id="exercise4-answer" class="step" data-x="-600" data-y="13200">
<pre class="prettyprint">
<code class="language-javascript">
var isInThirties = R.both(
R.gte(R.__, 30),
R.lt(R.__, 40)
);
var userIsInThirties = R.propSatisfies(
isInThirties,
"age"
);
var getUsersInTheirThirties = R.compose(
R.filter(userIsInThirties),
R.prop("users")
);</code>
</pre>
</section>
<section id="final" class="step" data-x="0" data-y="14000">
<header>
<h1>Congratulations!</h1>
<h3><em>you survived!</em></h3>
</header>
<p>Now you know the main techniques of functional programming in Javascript</p>
</section>
<!-- </article> -->
</div>
<script>
if ("ontouchstart" in document.documentElement) {
document.querySelector(".hint").innerHTML = "<p>Tap on the left or right to navigate</p>";
}
</script>
<script src="js/impress.js"></script>
<script src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
<script>impress().init();</script>
</body>
</html>