-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
700 lines (507 loc) · 10.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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>The Past, Present and Future of Functional JavaScript</title>
<style>
@import url(http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(http://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(http://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body {
font-family: 'Droid Serif';
}
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.title {
background-color: #272822;
color: white;
}
.business-card {
background-color: #272822;
background-position: left;
display: block;
margin-top: 50px;
}
.pics {
display: block;
margin-top: 50px;
}
.pics > span {
display: inline-block;
height: 180px;
width: 180px;
}
.pics .gamer {
background-image: url(nes-controller.png);
}
.pics .kids {
background-image: url(alex-and-aidan.png);
}
.pics .js-logo {
background-image: url(javascript-logo.png);
background-size: contain;
}
.obiwan {
background-image: url(obiwan.jpg);
display: block;
height: 277px;
margin: 0 auto;
width: 490px;
}
.year-19100 {
background-image: url(year-19100-1.jpg);
display: block;
height: 273px;
margin: 0 auto;
width: 490px;
}
.quote-container {
display: inline-block;
font-style: italic;
position: relative;
}
.quote {
display: inline-block;
margin: 0 0;
padding: 0 40px;
}
.quote:before {
color: #7a7a7a;
content: "\201C";
display: block;
font-size: 80px;
left: -20px;
position: absolute;
top: -20px;
}
.quote .cite {
display: block;
font-size: 13px;
margin-top: 10px;
}
.quote .cite:before {
content: "\2014 \2009";
}
.strikethrough {
text-decoration: line-through;
}
.hljs-default .hljs {
box-shadow: inset 2px 2px 4px rgba(0, 0, 0, 0.2);
}
.remark-code,
.remark-inline-code {
font-family: 'Ubuntu Mono';
}
.remark-slide li {
margin-bottom: 0.5em;
}
.remark-slide ol ol {
list-style-type: lower-alpha;
}
.remark-slide ol ol ol {
list-style-type: lower-roman;
}
.remark-slide-number {
display: none;
}
</style>
</head>
<body>
<textarea id="source">
class: center, middle, title
# Functional JavaScript:<br/>The Past, Present & Future
---
class: middle
```javascript
var me = {
name: 'Nathaniel Lee',
job: {
employer: 'AppNexus',
title: 'User Interface Engineer'
},
favorites: [
'my family',
'digital entertainment',
'JavaScript'
]
};
```
---
class: center, middle, title
# Nathaniel Lee: User Interface Engineer
.pics[.kids[] .gamer[] .js-logo[]]
.business-card[]
---
class: middle
```javascript
var targetClass = 'summary';
var summaries = [];
for (var i = 0; i < document.body.childNodes.length; i++) {
var currentNode = document.body.childNodes[i];
var classes = currentNode.className.split(' ');
if (classes.indexOf(targetClass) !== -1) {
summaries.push(currentNode);
}
/** Continue with this node's children and so on... */
}
```
---
class: middle
## Imperative programming
**The focus is on how we accomplish our goal.** We tell the environment *how* to do something, and it does *what* we want it to do.
---
class: middle
```javascript
var summaries = $('.summary');
```
---
class: middle
## Declarative programming
**The focus is on what we want to accomplish.** We tell the environment *what* we want to happen, and it figures out *how* to do it.
---
class: center, middle
## Libraries are declarative tools
---
class: middle
Because jQuery gave us this:
```javascript
var summaries = $('.summary');
```
We now have this:
```javascript
var summaries = document.querySelectorAll('.summary');
```
---
class: center, middle
## Thanks, John Resig!
---
class: middle
.quote-container[.quote[Any fool can write code that a computer can understand.<br/>Good programmers write code that humans can understand..cite[Martin Fowler]]]
---
class: middle
## Functional programming
Aims to increase not just readability and understandability but also predictability:
* avoid changes in program state that make it more difficult to reason about the result of an operation
* abstract simple, reusable actions into functions that act similarly to traditional mathematical functions
* use those functions to build more complex operations
---
class: center, middle
## Programming with functions?
---
class: center, middle
## "Scheme in the browser"
---
class: middle
```javascript
Scheme.prototype = new Lisp;
```
---
class: middle
.quote-container[.quote[JavaScript's C-like syntax, including curly braces and the clunky for statement, makes it appear to be an ordinary procedural language. This is misleading because JavaScript has more in common with functional languages like Lisp or Scheme than with C or Java..cite[Douglas Crockford]]]
---
class: middle
```javascript
meme.setUpperText("LET'S LOOK TO THE FUTURE");
meme.setLowerText("ALL THE WAY TO THE YEAR " +
new String("19" + (new Date).getYear()));
```
---
class: center, middle
.year-19100[]
---
class: middle
```javascript
Self.prototype = new Smalltalk;
Self.prototype.constructor = Self;
Self.prototype.getInheritanceSystem = function () {
return "Prototypes FTW!!!";
};
JavaScript.extend({
getInheritanceSystem: Self.getInheritanceSystem,
functions: Scheme.firstClassFunctions
});
```
---
class: middle
### We can assign a function to a variable
```javascript
var multiply = function (x, y) {
return x * y;
};
```
---
class: middle
### We can store a function in a data structure
```javascript
var calculator = {
multiply: multiply
};
```
---
class: middle
### We can pass a function into another function
```javascript
calculator.calculate = function (fn, x, y) {
return fn(x, y);
};
```
---
class: middle
### We can return a function from another function
```javascript
calculator.double = function () {
return function (x) {
return multiply(x, 2);
};
}(multiply);
```
---
class: center, middle
## Functions are first class citizens in JavaScript
---
class: center, middle
## Higher order functions
---
class: middle
```javascript
var companies = [
{
name: "Buy 'N Large",
donations: [35, 25, 50, 80, 50, 45, 15, 20, 10, 20, 30, 80, 75, 55, 60]
},
{
name: "Weyland-Yutani",
donations: [40, 40, 25, 20, 60, 30, 30, 45, 25, 50, 25, 25, 30]
},
{
name: "Omni Consumer Products",
donations: [50, 55, 65, 85, 100, 30, 65]
},
{
name: "Cyberdyne Systems",
donations: [10, 20, 40, 30, 35, 50, 45, 50, 55]
},
{
name: "Umbrella Corporation",
donations: [45, 70, 35, 65, 75, 40, 55, 65, 85, 70, 45, 50]
}
];
```
---
class: middle
```javascript
var data = [];
var company;
var averageDonation;
var totalDonation;
var i;
var j;
for (i = 0; i < companies.length; i++) {
company = companies[i];
totalDonation = 0;
for (j = 0; j < company.donations.length; j++) {
totalDonation += company.donations[j];
}
averageDonation = totalDonation / company.donations.length;
data.push([
company.name,
averageDonation,
totalDonation
]);
}
```
---
class: center, middle
## .strikethrough[Iteration] Recursion
---
class: middle
```javascript
function forEach(arr, callback) {
callback(arr.shift());
if (arr.length > 0) {
forEach(arr, callback);
}
}
```
---
class: middle
```javascript
function forEach(arr, callback) {
callback(arr[0]);
if (arr.length > 1) {
forEach(arr.slice(1), callback);
}
}
```
---
class: middle
```javascript
var data = [];
var averageDonation;
var totalDonation;
forEach(companies, function (company) {
totalDonation = 0;
forEach(company.donations, function (donation) {
totalDonation += donation;
});
averageDonation = totalDonation / company.donations.length;
data.push([
company.name,
averageDonation,
totalDonation
]);
});
```
---
class: middle
```javascript
var data = [];
var averageDonation;
var totalDonation;
companies.forEach(function (company) {
totalDonation = 0;
company.donations.forEach(function (donation) {
totalDonation += donation;
});
averageDonation = totalDonation / company.donations.length;
data.push([
company.name,
averageDonation,
totalDonation
]);
});
```
---
class: middle
```javascript
function add(a, b) {
return a + b;
}
function total(numbers) {
return numbers.reduce(add);
}
```
---
class: middle
```javascript
var data = [];
var averageDonation;
var totalDonation;
companies.forEach(function (company) {
totalDonation = total(company.donations);
averageDonation = totalDonation / company.donations.length;
data.push([
company.name,
averageDonation,
totalDonation
]);
});
```
---
class: middle
```javascript
function average(numbers) {
return total(numbers) / numbers.length;
}
```
---
class: middle
```javascript
var data = [];
var averageDonation;
var totalDonation;
companies.forEach(function (company) {
totalDonation = total(company.donations);
averageDonation = average(company.donations);
data.push([
company.name,
averageDonation,
totalDonation
]);
});
```
---
class: middle
```javascript
var data = [];
companies.forEach(function (company) {
data.push([
company.name,
average(company.donations),
total(company.donations)
]);
});
```
---
class: middle
```javascript
var data = companies.map(function (company) {
return [
company.name,
average(company.donations),
total(company.donations)
];
});
```
---
class: center, middle
## Arrow functions
---
class: middle
```javascript
x => x * 2;
```
---
class: middle
```javascript
var add = (x, y) => x + y;
```
---
class: middle
```javascript
function total(numbers) {
return numbers.reduce((x, y) => x + y);
}
function average(numbers) {
return numbers.reduce((x, y) => x + y) / numbers.length;
}
```
---
class: middle
```javascript
var data = companies.map(company => [
company.name,
company.donations.reduce((x, y) => x + y) / company.donations.length,
comapny.donations.reduce((x, y) => x + y)
]);
```
---
class: middle
```javascript
var data = companies.map(company => [
company.name,
average(company.donations),
total(company.donations)
]);
```
---
class: middle
.quote-container[.quote[Good code is its own best documentation. As you’re about to add a comment, ask yourself, ‘How can I improve the code so that this comment isn’t needed?'.cite[Steve McConnell]]]
---
class: middle
.quote-container[.quote[Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live..cite[Martin Golding]]]
---
class: center, middle
.obiwan[]
---
class: center, middle
# Thanks!
</textarea>
<script src="remark.min.js"></script>
<script>
var slideshow = remark.create();
</script>
</body>
</html>