-
Notifications
You must be signed in to change notification settings - Fork 30
/
ptc.html
395 lines (341 loc) · 9.96 KB
/
ptc.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
<!--
Google IO 2012/2013 HTML5 Slide Template
Authors: Eric Bidelman <ebidel@gmail.com>
Luke Mahé <lukem@google.com>
URL: https://code.google.com/p/io-2012-slides
-->
<!DOCTYPE html>
<html>
<head>
<title>PTC</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">-->
<!--<meta name="viewport" content="width=device-width, initial-scale=1.0">-->
<!--This one seems to work all the time, but really small on ipad-->
<!--<meta name="viewport" content="initial-scale=0.4">-->
<meta name="apple-mobile-web-app-capable" content="yes">
<link rel="stylesheet" media="all" href="theme/css/default.css">
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="theme/css/phone.css">
<base target="_blank"> <!-- This amazingness opens all links in a new tab. -->
<script data-main="js/slides" src="js/require-1.0.8.min.js"></script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<slide class="title-slide segue nobackground">
<aside class="gdbar"><img src="images/es6.png"></aside>
<!-- The content of this hgroup is replaced programmatically through the slide_config.json. -->
<hgroup class="auto-fadein">
<h1 data-config-title style="display: none;"><!-- populated from slide_config.json --></h1>
<h2 data-config-subtitle style="display: none;"><!-- populated from slide_config.json --></h2>
<p data-config-presenter style="display: none;"><!-- populated from slide_config.json --></p>
<h1>Proper Tail Calls</h1>
<p data-config-presenter><!-- populated from slide_config.json --></p>
</hgroup>
</slide>
<slide class="segue dark quote nobackground">
<aside class="gdbar right bottom"><img src="images/es6.png"></aside>
<article class="flexbox vleft auto-fadein">
<q>
Proper tail recursion is a property of the asymptotic space complexity of a
language's runtime behavior. That is, in improperly tail recursive languages,
...
</q>
<div class="author">
Dave Herman<br>
Principal Researcher w/ Mozilla<br>
@littlecalculist
</div>
</article>
</slide>
<slide class="segue dark quote nobackground">
<aside class="gdbar right bottom"><img src="images/es6.png"></aside>
<article class="flexbox vleft auto-fadein">
<q>
...
control can consume unbounded amounts of space for programs that, when run
in properly tail recursive languages, only require a constant amount of space.
</q>
<div class="author">
Dave Herman<br>
Principal Researcher w/ Mozilla<br>
@littlecalculist
</div>
</article>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Call</h2>
</hgroup>
<article class="flexbox vcenter">
<img src="images/ptcimproper.png" class="" alt="" style="width: 80%;" />
</article>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Call</h2>
</hgroup>
<article class="flexbox vcenter">
<img src="images/ptcproper.png" class="" alt="" style="width: 80%;" />
</article>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Calls: Example</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
<script type='text/javascript'>
//resursive method
<b>function foo(num) {</b>
try{
<b>return foo( (num||0) +1);</b>
}catch(e){return num;}
}
console.log(foo());
</script>
</pre>
<ul class="build fade">
<li>Let's Run In Chrome, Firefox, & Nodejs</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Call</h2>
</hgroup>
<article class="flexbox ">
<img src="images/ptcfoo1.png" class="reflect" alt="" />
</article>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Call</h2>
</hgroup>
<article class="flexbox ">
<img src="images/ptcfoo2.png" class="reflect" alt="" />
</article>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Call</h2>
</hgroup>
<article class="flexbox ">
<img src="images/ptcfoo3.png" class="reflect" alt="" />
</article>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Call</h2>
</hgroup>
<article class="flexbox ">
<img src="images/ptcfoon.png" class="" alt="" />
</article>
<ul class="build fade">
<li>Callstacks are not released after each call</li>
</ul>
</slide>
<slide>
<hgroup>
<h2>Proper Tail Call</h2>
</hgroup>
<article>
<ul class="build fade">
<li>ES6 intoduces Proper Tail Calls into JavaScript</li>
<li>If used correctly, PTC will add recursion</li>
<li>Previous stacks are GC'd</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>PTC Vocab: Tail Position</h2>
</hgroup>
<article>
<ul class="build fade">
<li>The last instruction to fire before the <i>return</i> statement</li>
</ul>
<pre class="prettyprint" data-lang="javascript">
<script type='text/javascript'>
function greet(num) {
<b>return "Hello";</b>
}
</script>
</pre>
<ul class="build fade">
<li>Instruction "Hello" is in Tail Position</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>PTC Vocab: Tail Call</h2>
</hgroup>
<article>
<ul class="build fade">
<li>An instruction in Tail Position that is a method call</li>
<li>
<pre class="prettyprint" data-lang="javascript">
<script type='text/javascript'>
function greet(){
<b>return getSalutation(); //Tail Position && Tail Call</b>
}
function getSalutation(){
<b>return 'Hello'; //Tail Position, but not Tail Call</b>
}
</script>
</pre>
</li>
<li>Now we know Tail Position & Tail Call</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>PTC Vocab: Tail Call</h2>
</hgroup>
<article>
<ul class="build fade">
<li>An instruction in Tail Position that is a method call</li>
<li>
<pre class="prettyprint" data-lang="javascript">
<script type='text/javascript'>
function resultsHandler(err, results, callback){
if(err){
<b>return callback(true);</b>
}
<b>return callback(false, results);</b>
}
<b>//TWO TAIL POSITIONS, TWO TAIL CALLS</b>
</script>
</pre>
</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>PTC Vocab: Close Call</h2>
</hgroup>
<article>
<ul class="build fade">
<li>Close call looks like a tail call, but isn't</li>
<li>
<pre class="prettyprint" data-lang="javascript">
<script type='text/javascript'>
function foo(){
<b>return 1 + bar();</b>
}
<b>//It has to return the result of bar(), then add 1</b>
</script>
</pre>
</li>
<li>Because it does something after the call, it isn't a tail call</li>
</ul>
</article>
</slide>
<slide>
<hgroup>
<h2>PTC Vocab: Proper Tail Call PRE-ES6</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
<script type='text/javascript'>
function fibonacci(x, y, limit, index) {
if(arguments.length == 1){
if(x)
return fibonacci(0, 1, x, 1);
return 0;
}else{
if(index < limit)
return fibonacci(y, (x+y), limit, ++index);
return y;
}
}
console.log(fibonacci(3)); // 2
console.log(fibonacci(10)); // 55
console.log(fibonacci(12495)); //RangeError
</script>
</pre>
</article>
<aside class="note">
<section>
<ul class="build fade">
<li>Each Call Add Another Stack</li>
<li>Each Stack Needs More Memory</li>
<li>Memory Runs Out</li>
<li>#trycatch</li>
</ul>
</section>
</aside>
</slide>
<slide>
<hgroup>
<h2>PTC Vocab: Proper Tail Call w/ ES6</h2>
</hgroup>
<article>
<pre class="prettyprint" data-lang="javascript">
<script type='text/javascript'>
function fibonacci(x, y, limit, index) {
if(arguments.length == 1){
if(x)
return fibonacci(0, 1, x, 1);
return 0;
}else{
if(index < limit)
return fibonacci(y, (x+y), limit, ++index);
return y;
}
}
console.log(fibonacci(3)); // 2
console.log(fibonacci(10)); // 55
console.log(fibonacci(12495)); // BIG BLOODY NUMBER
</script>
</pre>
</article>
<aside class="note">
<section>
<ul class="build fade">
<li>In ES6, exact same code just works</li>
<!--<li>Each Stack Needs More Memory</li>-->
<!--<li>Memory Runs Out</li>-->
<!--<li>#trycatch</li>-->
</ul>
</section>
</aside>
</slide>
<slide>
<hgroup>
<h2>Final Thoughts</h2>
</hgroup>
<article>
<ul>
<li>Proper Tail Calls only work in Strict Mode</li>
<li>Optimize code so that Tail Position is a Tail Call</li>
</ul>
</article>
</slide>
<slide class="segue dark quote nobackground">
<aside class="gdbar right bottom"><img src="images/es6.png"></aside>
<article class="flexbox vleft auto-fadein">
<h2>Proper Tail Calls</h2>
<h3>Questions???</h3>
<div class="author">
#ES6 #frontendmasters @js_dev
</div>
</article>
</slide>
<slide class="backdrop"></slide>
</slides>
<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</body>
</html>