-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
executable file
·616 lines (517 loc) · 15.8 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Functional Programming Fundamentals</title>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<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>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h3>Functional Programming Fundamentals</h3>
<p>Quinton Aiken, Laurel & Wolf, @quintonaiken</p>
</section>
<!-- OOP VS FP -->
<section>
<section>
<h3>Agenda</h3>
<h4 class="fragment">OOP VS FP</h4>
<h4 class="fragment">Algebraic Side Tangent</h4>
<h4 class="fragment">Benefits of FP</h4>
<h4 class="fragment">Declarative VS Imperative Code</h4>
<h4 class="fragment">Point-Free Programming</h4>
<h4 class="fragment">Functional Composition</h4>
<h4 class="fragment">Functors and Monads (you're already using them)</h4>
</section>
<section>
<h4>OOP VS FP</h4>
<p class="fragment">NOT mutually exclusive.</p>
<p class="fragment">NOT about objects vs functions.</p>
<p class="fragment">
The principles of software design still apply,
regardless of your programming style, like the Single Responsibility Principle.
</p>
</section>
<section>
<p>
Every functional program ever written is composed of a set of functions that operate on data.
Every OO program ever written is composed of a set of functions that operate on data. - Robert Cecil Martin
</p>
<pre class="fragment">
<code>f(o) VS o.f()</code>
</pre>
<p class="fragment">
This syntax difference alone is not meaningful.
</p>
</section>
<section>
<h3>OOP</h3>
<p class="fragment">
OO imposes discipline about where functions are defined.
In OOP, functions are object methods that can be inherited (polymorphism).
</p>
<p class="fragment">
Data and behavior in a single location - classes with properties (data) and methods (functions).
</p>
<small>http://blog.cleancoder.com/uncle-bob/2014/11/24/FPvsOO.html</small>
</section>
<section>
<h3>FP</h3>
<p class="fragment">
FP imposes discipline upon assignment (do not mutate state).
</p>
<p class="fragment">
Functions are "first class". We can treat functions like any other data type.
They can be stored in arrays, passed around as function parameters,
assigned to variables, etc.
</p>
<small>http://blog.cleancoder.com/uncle-bob/2014/11/24/FPvsOO.html</small>
<small>https://drboolean.gitbooks.io/mostly-adequate-guide</small>
</section>
<section>
<h4>OOP Example</h4>
<pre class="fragment">
<code>var Flock = function(n) {
this.seagulls = n;
};
Flock.prototype.conjoin = function(other) {
this.seagulls += other.seagulls;
return this;
};
Flock.prototype.breed = function(other) {
this.seagulls = this.seagulls * other.seagulls;
return this;
};
var flock_a = new Flock(4);
var flock_b = new Flock(2);
var flock_c = new Flock(0);
var result = flock_a // 4
.conjoin(flock_c) // 4
.breed(flock_b) // 8
.conjoin(flock_a.breed(flock_b)) // (16 + (8*2 = 16))
.seagulls; // 32
</code>
</pre>
<p class="fragment">
32!??
</p>
<small>https://drboolean.gitbooks.io/mostly-adequate-guide</small>
</section>
<section>
<h4>FP Example</h4>
<pre class="fragment">
<code>var add = function(x, y) { return x + y; };
var multiply = function(x, y) { return x * y; };
var flock_a = 4;
var flock_b = 2;
var flock_c = 0;
var result = add(
multiply(
flock_b,
add(flock_a, flock_c)
),
multiply(flock_a, flock_b)
); // 16
</code>
</pre>
<small>https://drboolean.gitbooks.io/mostly-adequate-guide</small>
</section>
<section>
<h4>Algebraic Side Tangent</h4>
<pre class="fragment">associative property - grouping of addends doesn't matter
<code>add(add(x, y), z) === add(x, add(y, z));</code>
</pre>
<pre class="fragment">commutative - order of addends doesn't matter
<code>add(x, y) === add(y, x);</code>
</pre>
<pre class="fragment">identity - adding zero doesn't do anything
<code>add(x, 0) === x;</code>
</pre>
<pre class="fragment">distributive - you can multiply a sum by multiplying each
addend separately and then add the products.
<code>add(multiply(x, y), multiply(x, z)) === multiply(x, add(y,z));</code>
</pre>
</section>
<section>
<h4>Identity?</h4>
<pre>
<code>var flock_a = 4;
var flock_b = 2;
var flock_c = 0;
var result = add(
multiply(
flock_b,
add(flock_a, flock_c)
),
multiply(flock_a, flock_b)
); // 16
</code>
</pre>
</section>
<section>
<h4>Distributive?</h4>
<pre>
<code>var flock_a = 4;
var flock_b = 2;
var flock_c = 0;
var result = add(
multiply(flock_b, flock_a),
multiply(flock_a, flock_b)
); // 16
</code>
</pre>
</section>
<section>
<h4>Simplified</h4>
<pre>
<code>var flock_a = 4;
var flock_b = 2;
var flock_c = 0;
var result = multiply(
flock_b,
add(flock_a, flock_a)
); // 16
</code>
</pre>
</section>
</section>
<section>
<section>
<h4>Benefits of FP</h4>
<p class="fragment">
Deeply rooted in tried and true mathematical concepts
</p>
<p class="fragment">
Enforces immutability which allows us to see our application's history.
</p>
</section>
<section>
<img width="960" src="redux-logger.png" />
</section>
<section>
<h4>Benefits of FP</h4>
<p>
Deeply rooted in tried and true mathematical concepts
</p>
<p>
Enforces immutability which allows us to see our application's history.
</p>
<p class="fragment">
Minimal Side Effects - functions called with same input, give you the same output.
Which leads to...
</p>
<p class="fragment">
Cacheable Functions / Memoization
</p>
</section>
<section>
<pre>
<code>var squareNumber = memoize(function(x) {
return x * x;
});
squareNumber(4); // 16
squareNumber(4); // returns cache for input 4 (16)
</code>
</pre>
<small>https://drboolean.gitbooks.io/mostly-adequate-guide</small>
</section>
<section>
<h4>Benefits of FP</h4>
<p>
Deeply rooted in tried and true mathematical concepts.
</p>
<p>
Enforces immutability which allows us to see our application's history.
</p>
<p>
Minimal Side Effects - functions called with same input, give you the same output.
Which leads to...
</p>
<p>
Cacheable Functions / Memoization
</p>
<p class="fragment">
Predictable and Testable Code - I/O
Which leads to...
</p>
<p class="fragment">
More readable and declarative code
</p>
</section>
</section>
<section>
<section>
<h4>Declarative Vs Imperative</h4>
<p class="fragment">Imperative: "Loop thru collection, check each item, if has email, add it to a results var"</p>
<p class="fragment">Declarative: "Give me emails from a collection."</p>
</section>
<section>
<h4>Imperative</h4>
<pre class="fragment">
<code>function double (arr) {
let results = []
for (let i = 0; i < arr.length; i++){
results.push(arr[i] * 2)
}
return results
}
</code>
</pre>
<small>https://tylermcginnis.com/imperative-vs-declarative-programming/</small>
</section>
<section>
<h4>Declarative</h4>
<pre class="fragment">
<code>function double (arr) {
return arr.map((item) => item * 2)
}
</code>
</pre>
<small>https://tylermcginnis.com/imperative-vs-declarative-programming/</small>
</section>
<section>
<h4>Declarative Benefits</h4>
<p class="fragment">
Reads better, easier to see what is happening.
</p>
<p class="fragment">
Less typing, less chance for mistakes/typos/bugs.
</p>
<p class="fragment">
Remove needless variables / state (results var) Which leads to...
</p>
<p class="fragment">
Point Free Programming!
</p>
</section>
<section>
<h4>Point Free Programming</h4>
<pre class="fragment">
<code>var getServerStuff = function(callback) {
return ajaxCall(function(json) {
return callback(json);
});
};
</code>
</pre>
<pre class="fragment">
<code>var getServerStuff = function(callback) {
return ajaxCall(callback);
};
</code>
</pre>
<pre class="fragment">
<code>var getServerStuff = ajaxCall
</code>
</pre>
<small>https://drboolean.gitbooks.io/mostly-adequate-guide</small>
</section>
<section>
<h4>Point Free Programming</h4>
<pre class="fragment">
<code>httpGet('/post/2', function(json) {
return renderPost(json);
});
</code>
</pre>
<pre class="fragment">
<code>httpGet('/post/2', renderPost);
</code>
</pre>
<pre class="fragment">
<code>httpGet('/post/2', function(json, err) {
return renderPost(json, err);
});
</code>
</pre>
<small>https://drboolean.gitbooks.io/mostly-adequate-guide</small>
</section>
<section>
<h4>Benefits of Point Free Programming</h4>
<p class="fragment">
Reduces "the amount of redundant code to maintain and search through."
</p>
<p class="fragment">
Keep us concise and generic. "Pointfree is a good litmus test for functional code as it lets us know we've got small functions that take input to output."
Which leads to...
</p>
<p class="fragment">
Functional Composition!
</p>
<small>(Quotes by Dr Boolean) https://drboolean.gitbooks.io/mostly-adequate-guide</small>
</section>
<section>
<h4>Functional Composition</h4>
<pre class="fragment">
<code>function getUserNames (res) {
var users = res.data.users || []
var userNames = []
for (var i = 0; i < users.length; i++) {
var userName = users[i].name
userNames.push(userName)
}
return userNames
}
</code>
</pre>
<pre class="fragment">
<code>const userNames = pipe(
path(['data','users']),
defaultTo([]),
pluck('name')
)(res)
</code>
</pre>
</section>
<section>
<h4>Benefits of Functional Composition</h4>
<p class="fragment">linearity</p>
<p class="fragment">less assignment, variables and state</p>
<p class="fragment">easily extentable</p>
<p class="fragment">Build new functions from other functions</p>
<p class="fragment">Help build generic programs w/o data arguments</p>
</section>
</section>
<section>
<section>
<h4>Functors / Monads</h4>
<p class="fragment">Functor - thing with a map method (arrays too!)</p>
<p class="fragment">Monad - thing with a flatmap/chain/bind method</p>
</section>
<section>
<h4>Functors</h4>
<img width="650" src="functor.png" />
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Functors</h4>
<p class="fragment">Map - less about iteration over something and more to do with composition in a context.</p>
<p class="fragment">.map.map.map is the composition. the thing that is being mapped is the context (LazyBox(' 64 '))</p>
<p class="fragment">Actual value is "wrapped" in our context.</p>
<p class="fragment">LazyBox - no impure side-effects until the very end.<p>
<p class="fragment">if LazyBox().map.map.map was a library, the maintainers could keep extending it and the consumer's application would remain pure until the consumer calls .fold!</p>
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Functors</h4>
<img width="650" src="functor.png" />
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Functors</h4>
<p>In our LazyBox mapping, we always returned the actual value of interest.</p>
<p class="fragment">What if map returned a functor?</p>
<p class="fragment">Intead of LazyBox(5) we might get LazyBox(LazyBox(5))</p>
<p class="fragment">hmm...</p>
<p class="fragment">When would we do this though?</p>
</section>
<section>
<p>Uncaught TypeError: Cannot read property 'x' of undefined</p>
<img class="fragment" width="650" src="monad-1.5.png" />
<p class="fragment">Monads to the rescue!</p>
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Monads</h4>
<img width="380" src="monad.png" />
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Monads</h4>
<p class="fragment">
Must have a bind/flatMap/chain function which is able to pull the value (x) out from the the monad
and put that value into another function (f) as an argument.
(f should return a monad).
</p>
<small>https://hackernoon.com/functional-computational-thinking-what-is-a-monad-2adea91154e</small>
</section>
<section>
<h4>Monads</h4>
<img width="600" src="monad-2.png" />
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Monads</h4>
<img src="monad-3.png" />
<p class="fragment">Imperative</p>
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Monads</h4>
<img src="monad-4.png" />
<p class="fragment">Declarative</p>
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Monads</h4>
<p class="fragment">where have we seen this before?</p>
<p class="fragment">Promises!</p>
<pre class="fragment">
<code>Promise.resolve(x)
// Promise(x)
.then(x => someAsyncFunc(x))
// Promise(Promise(y)) then === flatMap!
.then(y => console.log(y))
</code>
</pre>
<small>https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript</small>
</section>
<section>
<h4>Monads</h4>
<p class="fragment">
Allow us to use function composition and encapsulation to solve many programming problems like null checking and async flow.
</p>
<p class="fragment">
Further Reading:
<ul>
<li class="fragment">https://drboolean.gitbooks.io/mostly-adequate-guide/</li>
<li class="fragment">https://github.com/fantasyland/fantasy-land</li>
</ul>
</p>
<h2 class="fragment">The End</h2>
</section>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>