This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
300 lines (255 loc) · 8.49 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>How to JavaScript - the functional way</title>
<meta name="description" content="Basics of functional js">
<meta name="author" content="Hakim El Hattab">
<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">
<!-- Title -->
<section>
<h1>How to JavaScript</h1>
<h3>The Functional Way</h3>
<p>
<small>By <a href="https://github.com/aulme">Aleksandrs Ulme</a> </small>
</p>
</section>
<!-- What is Functional? -->
<section>
<h2>What is Functional?</h2>
<ul>
<li class="fragment">Math</li>
<li class="fragment">Lambda calculus</li>
<li class="fragment">Functions in math definition of the word</li>
</ul>
</section>
<!-- OMG WTF -->
<section>
<img src="http://catmacros.files.wordpress.com/2009/12/omgwtf.jpg"
class="stretch"
alt="OMG WTF cat was supposed to be here, but sadly it's not :( " />
</section>
<section>
<h2>It's just this</h2>
<img src="http://upload.wikimedia.org/wikipedia/commons/3/3b/Function_machine2.svg"
class="stretch"
style="background-color:white;"
alt="Representation of function as a black box. So sorry you can't see it, it's a nice box">
</section>
<section>
<h2>Reminds you of something?</h2>
<ul>
<li class="fragment">JSON -> f -> some other JSON</li>
<li class="fragment">Risk data -> f -> risk</li>
<li class="fragment">API, user input -> f -> DOM state</li>
</ul>
</section>
<!-- Main concepts -->
<section>
<section>
<h2>What do we need?</h2>
<ul>
<li>Purity</li>
<li>Immutability</li>
<li>Functional composition</li>
</ul>
</section>
<section>
<h2>Purity</h2>
<small>Avoid side effects</small>
<pre>
<code data-noescape contenteditable data-trim>
var c = 0;
const addTwoNumbers= (a, b) => {
alert('explosions!');
return a + b + c++;
}
addTwoNumbers(2, 2); // explosions!, 4, c <- 1
addTwoNumbers(2, 2); // explosions!, 5, c <- 2
</code>
</pre>
<pre class="fragment">
<code data-noescape contenteditable data-trim>
const addThreeNumbers= (a, b, c) => {
return a + b + c;
}
addThreeNumbers(2, 2, 0); // 4
addThreeNumbers(2, 2, 1); // 5
</code>
</pre>
</section>
<section>
<h2>Immutability</h2>
<small>Avoid mutations</small>
<pre>
<code data-noescape contenteditable data-trim>
const accounts = [100, 200, 300];
const addInterest = accounts => {
for (i = 0; i < accounts.length; i++) {
accounts[i] *= 1.03;
}
}
addInterest(accounts); // accounts <- [103, 206, 309]
</code>
</pre>
<pre class="fragment">
<code data-noescape contenteditable data-trim>
const accounts = [100, 200, 300];
const addInterest = accounts => accounts.map(acc => acc * 1.03);
const updatedAccounts = addInterest(accounts); // [103, 206, 309]
</code>
</pre>
</section>
<section>
<h2>Functional composition</h2>
<small>Use Functions as an abstraction</small>
<pre>
<code data-noescape contenteditable data-trim>
const accountsHKD = [100, 200, 300];
const withInterest = R.multiply(1.03);
const afterFees = x => x - 0.05 * x;
const settled = R.pipe(withInterest, afterFees);
const newAccountsHKD = R.map(settled, accountsHKD);
// [97.85, 195.7, 293.55]
</code>
</pre>
</section>
</section>
<!-- Already in JS -->
<section>
<section>
<h2>We're already there, sorta</h2>
<br />
<img src="http://i.imgur.com/QEiJZyt.png"
class="stretch"
alt="dr Evil tells you how to functional JavaScript" />
</section>
<section>
<h2>Promisses are pipes </h2>
<small>expressed over time</small>
<pre>
<code data-noescape contenteditable>
const user = { accountsUSD: [10, 5, 20] };
const getAccounts = R.prop('accountsUSD');
const USDToHKD = R.multiply(7.77);
const accountsInHKD = Promise.resolve(user)
.then(getAccounts)
.then(R.map(USDToHKD));
const totalHKD = accountsInHKD.then(R.sum); // ~272
</code>
</pre>
</section>
<section>
<h2>And streams are lists</h2>
<small>also over time</small>
<pre>
<code data-noescape contenteditable>
const prices = highland([10, 11, 15, 12, 13, 11]);
const mean = prices => R.sum(prices) / R.length(prices);
prices
.batch(3)
.map(mean)
.pipe(writeToSomeDb);
</code>
</pre>
</section>
<section>
<h2>Two of Three Principles of Redux</h2>
<ul>
<li><q cite="http://redux.js.org/docs/introduction/ThreePrinciples.html">
“ State is read-only”
</q></li>
<li><q cite="http://redux.js.org/docs/introduction/ThreePrinciples.html">
“Changes are made with pure functions”
</q></li>
</ul>
</section>
</section>
<section>
<section>
<h2>Why bother?</h2>
</section>
<section>
<h2>Pure is awesome</h2>
<ul>
<li>Truly self-contained</li>
<li>Easy to reason about</li>
<li>Very unit testable</li>
</ul>
</section>
<section>
<h2>Promisses</h2>
<pre>
<code data-noescape contenteditable>
stockDataPromise
.then(R.splitEvery(3))
.then(R.map(mean))
.then(R.map(toHKD))
.then(persist);
</code>
</pre>
</section>
<section>
<h2>Synergy</h2>
<img src="https://45.media.tumblr.com/d8efd92df00f4217f26a90a896c01be8/tumblr_ngfhvu9COA1r72ht7o1_500.gif"
class="stretch"
alt="Fusion!" />
</section>
</section>
<section>
<h1>Questions?</h1>
</section>
<!-- End -->
<section style="text-align: left;">
<h1>THE END</h1>
<p>
Now go and make stuff functional
</p>
</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, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>