-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
517 lines (463 loc) · 15.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Functional JavaScript</title>
<meta name="description" content="A lightning talk about how Sessions, how to use them with Rails and how they are implemented.">
<meta name="author" content="Ryan McGowan">
<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">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/solariced.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/solarized_dark.css">
<!-- If the query includes 'print-pdf', use the PDF print sheet -->
<script>
document.write( '<link rel="stylesheet" href="css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-markdown>
# An Introduction to Functional JavaScript
</section>
<section data-markdown>
## This talk is *NOT* about
* The DOM
* jQuery
* Inheritance
</section>
<section data-markdown>
## This talk *IS* about
* Types
* Syntax
* Functional programming (mostly this)
</section>
<section data-markdown>
## Let's Jump into it
</section>
<section>
<section data-markdown>
# Types & Truthiness
</section>
<section data-markdown>
## Objects
They are everywhere
</section>
<section data-markdown>
## Pick the objects
true
false
undefined
null
"Hello World"
1
new Integer(1)
new Object()
{}
new Array()
[]
function (x) { return x; }
</section>
<section data-markdown>
# Classfree
</section>
<section data-markdown>
## Comparisons and Truthiness
undefined == null // → true
undefined === null // → false
0 == false // → true
1 == true // → true
'0' == false // → true
'1' == true // → true
'' == false // → true
'hello world' == false // → false
'hello world' == true // → false
if conditions are like `!= false`
</section>
</section>
<section>
<section data-markdown>
# Syntax
</section>
<section data-markdown>
## if-else
C-style syntax
<pre class="compact"><code class="javascript">if (x == y) {
// do something
} else {
// do something else
}
</code></pre>
</section>
<section data-markdown>
## switch-case
<pre class="compact"><code class="javascript">var returnValue;
switch(val) {
case 2:
returnValue = 'A'
break;
case 6:
returnValue = 'B'
break;
case 9:
returnValue = 'B'
break;
default:
returnValue = null;
}
return returnValue;
</code></pre>
</section>
<section data-markdown>
## switch-case
beware of breaks
</section>
<section data-markdown>
## while
while (condition) {
// do something that eventually
// manipulates condition
}
</section>
<section data-markdown>
## Classic for
<pre class="compact"><code class="javascript">for (var i = 0; i < arr.length; i++) {
// Do something arr.length times
}
</code></pre>
</section>
<section data-markdown>
## for *(each)*
for (var index in obj) {
// what do you think index is?
}
</section>
<section data-markdown>
# ;
</section>
<section data-markdown>
## var
var x;
x // → undefined
var y = 10;
</section>
<section data-markdown>
## typeof
typeof x // → "undefined"
x // throws a reference error
var x;
</section>
<section data-markdown>
## Why should I use var?
</section>
</section>
<section>
<section data-markdown>
# Scope
</section>
<section data-markdown>
## Where does it end?
with functions
</section>
<section data-markdown>
## Do I need scope?
* It's a tool, not a restriction
* closures are cool
</section>
</section>
<section data-markdown>
#### **DETOUR TO CLOSURES →**
</section>
<section>
<section data-markdown>
# Functions
</section>
<section data-markdown>
## Pssh, I already know that
</section>
<section data-markdown>
# Shut up
</section>
<section data-markdown>
## First class
<blockquote>
Functions can be assigned, passed in, returned and stored in data
structures like other objects.
</blockquote>
*- Ryan McGowan*
</section>
<section data-markdown>
## Quick Example
// Assignment
var coolFunc1 = function () { return "Sooo coool"; };
// Pass in as argument(s)
var coolFunc2 = function (func) {
// do some stuff and then finally …
func();
};
</section>
<section data-markdown>
## What the func?!
<pre class="compact"><code class="javascript">var foo = function (bar) {
return function (baz) {
return function (derp) {
return bar.callback(baz, derp, bar.value);
}:
};
};
var data = {
callback: function (x, y, z) { return [x, y, z].join(' '); },
value: 'func?!'
};
foo(data)("What")("the"); // → "What the func?!"
</code></pre>
</section>
</section>
<section>
<section data-markdown>
# Closures
</section>
<section data-markdown>
### What's that?
</section>
<section data-markdown>
## Example
</section>
<section data-markdown>
### Implementation Detail Extravaganza
* Reference To Function
* Referencing Environment
* Table of references to free variables
</section>
</section>
<section>
<section data-markdown>
# Underscore.js
[http://underscorejs.org/](http://underscorejs.org/)
</section>
<section data-markdown>
## map
##### Clojure
<pre><code class="clojure">(map (parital + 1) [1 2 3])
</code></pre>
##### JavaScript
<pre><code class="javascript">_.map([1, 2, 3], function (num) {
return num + 1;
});
</code></pre>
</section>
<section data-markdown>
## reduce
##### Clojure
<pre><code class="clojure">(reduce * [2 3 4])
</code></pre>
##### JavaScript
<pre><code class="javascript">_.reduce([2, 3, 4], function (product, num) {
return product * num;
});
</code></pre>
</section>
<section data-markdown>
## chain
##### Clojure
<pre><code class="clojure">(->>
[1 2 3]
(map (partial + 1))
(reduce *))
</code></pre>
##### JavaScript
<pre><code class="javascript">_.chain([2, 3, 4]).map(function (num) {
return num + 1;
}).reduce(function (product, num) {
return product * num;
}).value();
</code></pre>
</section>
<section data-markdown>
## It's your turn
Let's make lambda.js
</section>
</section>
<section>
<section data-markdown>
# Replacing partial
</section>
<section data-markdown>
## Partials are cool
<pre><code class="clojure">(def plus-six (partial + 6))
(plus-six 10) ; → 16
</code></pre>
</section>
<section data-markdown>
## Partials are cool
<pre><code class="clojure">(defn all-less-than-ten-lame [coll]
(every? (fn [item] (< item 10)) coll))
; Cooler
(def all-less-than-ten (partial every? (partial > 10)))
(all-less-than-ten [1 2 3 4 5 8 2 2 7]) ; → true
(all-less-than-ten [1 2 3 4 5 8 11]) ; → false
</code></pre>
</section>
<section data-markdown>
## Rember this?
<pre><code class="clojure">(defn two-numbers [x]
(partial + (* 10 x)))
((two-numbers 6) 1) ; → 61
((two-numbers 10) ((two-numbers 1) 6) 4) ; → 120
</code></pre>
</section>
</section>
<section data-markdown>
# Challenge Time
</section>
<section>
<section data-markdown>
## Replace if-else
Use an Object, true, false and function(s)
</section>
<section data-markdown>
### Original
if (a == 'blarg') {
console.log('Hello');
} else {
console.log('Goodbye');
}
</section>
<section data-markdown>
### Answer
<pre><code class="javascript">console.log({
true: 'Hello',
false: 'Goodbye'
}[a == 'blarg']);
</code></pre>
</section>
<section data-markdown>
### Original
var x, y;
if (a == 'blarg') {
x = 2;
y = [z * 5];
} else {
y = 3;
x = [z / 2];
}
</section>
<section data-markdown>
### Answer
{
true: function () {
x = 2;
y = [z * 5];
},
false: function () {
y = 3;
x = [z / 2];
}
}[a == 'blarg']();
</section>
</section>
<section>
<section data-markdown>
## Replace switch-case
What do you need?
</section>
<section data-markdown>
### Original
<pre class="compact"><code class="javascript">var returnValue;
switch(val) {
case 2:
returnValue = 'A'
break;
case 6:
returnValue = 'B'
break;
case 9:
returnValue = 'B'
break;
default:
returnValue = null;
}
return returnValue;
</code></pre>
</section>
<section data-markdown>
### Answer
<pre class="compact"><code class="javascript">{2: 'A', 6: 'B', 9: 'C'}[val]
</code></pre>
Well, almost...
</section>
</section>
<section>
<section data-markdown>
## Function definition crap
var y = function () { ... };
// What's the difference?
function y() { ... }
</section>
<section data-markdown>
### Answer
[Hoisting](http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html).
<pre class="compact"><code class="javascript">foo(); // TypeError "foo is not a function"
bar(); // valid
baz(); // TypeError "baz is not a function"
spam(); // ReferenceError "spam is not defined"
var foo = function () {}; // anonymous function expression ('foo' gets hoisted)
function bar() {}; // function declaration ('bar' and the function body get hoisted)
var baz = function spam() {}; // named function expression (only 'baz' gets hoisted)
foo(); // valid
bar(); // valid
baz(); // valid
spam(); // ReferenceError "spam is not defined"
</code></pre>
</section>
</section>
<section data-markdown>
## That's All Folks
### Books/References
* [JavaScript: The Good Parts](http://shop.oreilly.com/product/9780596517748.do)
* [fun.js](http://blog.fogus.me/2013/03/20/fun-js/)
* [Some Stuff about Prototypes](http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/)
<br />
### Me
Twitter: [Ryan_VM](https://twitter.com/Ryan_VM)
Github: [RyanMcG](https://github.com/RyanMcG)
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script src="js/underscore.js"></script>
<script src="js/partial.js"></script>
<script src="js/lambda.js"></script>
<script src="js/scratch-pad.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/showdown.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, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
// { src: 'plugin/remotes/remotes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>