Fixed typo. Thanks Steffi.
[fp-lecture.git] / index.html
1 <html><head>
2 <meta charset="utf-8">
3 <link rel="stylesheet" href="css/csshake.min.css">
4 <link rel="stylesheet" href="css/animate.css">
5 <link rel="stylesheet" href="css/style.css">
6 <link href="https://fonts.googleapis.com/css?family=Press+Start+2P&amp;display=swap" rel="stylesheet">
7 <link rel="stylesheet"
8 href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/styles/atom-one-dark.min.css">
9 <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.1/highlight.min.js"></script>
10 <script>hljs.initHighlightingOnLoad();</script>
11 <script src="js/confetti.min.js"></script>
12 <title>Wide-Eyed Crazy Functional Programming</title>
13 </head>
14 <body>
15 <div id="slide-0">
16 <div id="cloudguy">
17 <div style="height: 20%; text-align: center;">
18 <img style="height: 100%; display: block; margin-left: auto; margin-right: auto; margin-bottom: 10px;" src="img/cloudguy.gif" />
19 <div id="cgquotes" style="width: 80%; margin: auto;">Welcome to the talk!</div>
20 </div>
21 </div>
22 <div id="commentary"><a href="notes.html" target="_blank"><img src="img/commentary.png" /><br />Click me!</a></div>
23 </div>
24 <div id="slide-1">
25 <div class="slide-title">=<< Welcome to the Talk Called >>=</div>
26 <div class="slide flexotron" style="flex-wrap: nowrap;">
27 <img class="cloudguypower" style="margin-left: 70px;" src="img/cloudguy-power.gif" />
28 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;">Wide-Eyed Crazy Functional Programming</div>
29 <img class="cloudguypower" style="margin-right: 70px; transform: scaleX(-1);" src="img/cloudguy-power.gif" />
30 </div>
31 <div class="notes">
32 Woo! Look at them go! If you throw on <a href="https://www.youtube.com/watch?v=vqz8c4ZP3Wg" target="_blank">this</a> then it's like a 2 hour dance party!
33 </div>
34 </div>
35 <div id="slide-2">
36 <div class="slide-title">=<< Just Wait a Sec... >>=</div>
37 <div class="slide flexotron" style="flex-wrap: nowrap;">
38 <div class="textbox flexotron" id="notes" style="position: relative; margin-right:20px; padding: 20px;">
39 This slide needs to be here to let the confetti settle otherwise the next slide lags even more lol... Just wait til the confetti goes away and continue.
40 </div>
41 </div>
42 </div>
43 <div id="slide-3">
44 <div class="slide-title">=<< What's up with that Title? >>=</div>
45 <div class="slide flexotron idk">
46 <div style="justify-content: right;">
47 <img class="shake-constant shake" src="img/wide-eyed-crazy.png" style="width: 70%; margin-right: 10%;">
48 </div>
49 <div style="justify-content: left">
50 <div style="display: flex; align-items: center; width: 70%">
51 <div id="rant" class="textbox" style="display: block;height: auto; width:100%; margin-left:10%;">
52 <span class="crazy">It’s functions, </span><span class="shake-constant shake-opacity">pure</span><span class="crazy"> functions! Pure functions and their values, PURE FUNCTIONS FOREVER AND FOREVER AND FOREVER A HUNDRED YEARS </span><span class="shake-constant shake-opacity">monad</span><span class="crazy">... some... things... Me and </span><span class="shake-constant shake-opacity">monads</span><span class="crazy"> runnin' around and...</span><span class="shake-constant shake-opacity">applicative functor</span><span class="crazy"> time... a- all day long forever... All a - a hundred days </span><span class="shake-constant shake-opacity">applicatives</span><span class="crazy"> and </span><span class="shake-constant shake-opacity">functors</span><span class="crazy">! Forever a hundred times.... OVER and over </span><span class="shake-constant shake-opacity">monad transformers</span><span class="crazy">... adventures dot com... W W W dot at </span><span class="shake-constant shake-opacity">lambda calculus</span><span class="crazy"> and </span><span class="shake-constant shake-opacity">category theory</span><span class="crazy"> dot com w..w..w... function composition adventures... Ah- hundred years… Every minute </span><span class="shake-constant shake-opacity">referential... transparency</span><span class="crazy"> dot com.... w w w a hundred times... </span><span class="shake-constant shake-opacity">composition</span><span class="crazy"> dot com...</span>
53 </div>
54 </div>
55 </div>
56 <div class="fig">Fig 1.1 What most people thing a typical functional programming advocate looks like</div>
57 </div>
58 <div class="notes">
59 <h1>Wide-eyed Crazy Functional Programmers</h1>
60 <ul>
61 <li>Some people were curious about the title, so I figured I'd clarify what it means - functional programmers have a reputation of...</li>
62 <ul>
63 <li>Being eccentric wide-eyed crazies with wild ideas</li>
64 <li>Spouting nonsense like "monads" and "functors" every 2 seconds</li>
65 <li>Being bald</li>
66 </ul>
67 </ul>
68 </div>
69 </div>
70 <div id="slide-4">
71 <div class="slide-title">=<< Obligatory Quote >>=</div>
72 <div class="slide flexotron">
73 <div class="textbox" style="font-size:30px;">If you wish to build a ship, do not divide the men into teams and send them to the forest to cut wood. Instead, teach them to long for the vast and endless sea.<br /><br /> - Some French Guy. Idk. I'm not cultured. I'm Australian.</div>
74 </div>
75 <div class="notes">
76 <h1>Some Framing</h1>
77 <ul>
78 <li>My goal is not to teach you functional programming, my goal is to spark curiosity</li>
79 <li>And I think this obligatory quote (every good talk has a quote) conveys the framework I used when writing these slides</li>
80 <li>Also we like boats and ships and stuff like that here, so make sense I guess</li>
81 <li>I'm not gonna make you build the ship, I'm gonna try make you curious about the sea that is functional programming</li>
82 <li>With that in mind, don't take any code examples too seriously, they are to demonstrate the big picture idea</li>
83 </ul>
84 </div>
85 </div>
86 <div id="slide-5">
87 <div class="slide-title">=<< What is this Talk About? >>=</div>
88 <div class="slide">
89 <h1>This talk is <span class="highlight">not</span> about:</h1>
90 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
91 <ul>
92 <li>Haskell</li>
93 <li>Convincing you to stop using whichever language you like</li>
94 <li>Mathematics</li>
95 </ul>
96 </div>
97 </div>
98 <div class="notes">
99 <h1>Teaching Violation!</h1>
100 <ul>
101 <li>They say you should never say what something is not when teaching</li>
102 <li>But screw that, anarchy! Yeah!</li>
103 </ul>
104 </div>
105 </div>
106 <div id="slide-6">
107 <div class="slide-title">=<< What is this Talk About? >>=</div>
108 <div class="slide flexotron">
109 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >Perspective</div>
110 </div>
111 <div class="notes">
112 <h1>What It Do, Then?</h1>
113 <ul>
114 <li>So what is it about?</li>
115 <li>When trying to explain to someone why they should learn functional programming, there are many perspectives to try explain it from.</li>
116 <li>For this discussion, I've settled on a very meta one. Perspective itself.</li>
117 </ul>
118 </div>
119 </div>
120 <div id="slide-7">
121 <div class="slide-title">=<< What is Functional Programming? >>=</div>
122 <div class="slide flexotron">
123 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >Programming with functions.</div>
124 </div>
125 <div class="notes">
126 <h1>Who could have guessed!</h1>
127 </div>
128 </div>
129 <div id="slide-8">
130 <div class="slide-title">=<< What is Functional Programming? >>=</div>
131 <div class="slide flexotron">
132 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >Programming with functions.<br /><br />No, really. Everything is a function.</div>
133 </div>
134 </div>
135 <div id="slide-9">
136 <div class="slide-title">=<< What is a Function? >>=</div>
137 <div class="slide flexotron">
138 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >A mapping between sets that associates every element of the first set to exactly one element of the second set.</div>
139 </div>
140 <div class="notes">
141 <h1>Regarding Functions</h1>
142 <ul>
143 <li>These aren't like procedural functions - "function" is a misnomer in that context. They are really procedures or subroutines (hence the name procedural)</li>
144 <li>A function is a mapping. It's equivalent to something like an array in PHP. It associates one value to another one</li>
145 <li>In the same way that a PHP array cannot mutate state, a function cannot</li>
146 <li>These are called "pure functions", but I'll just say "functions"</li>
147 <li>Mat asked a question about "impure functions"</li>
148 <ul>
149 <li>An impure function involves side effects. For example:
150 <div style="text-align: center;"><pre style="display: inline-block;"><code style="text-align: left; font-size: 18px;">
151 function readFileAndAdd2(string $filePath) : int {
152 $number = file_get_contents($filePath);
153 return $number+2;
154 }</code></pre></div>
155 is impure because it could be called with the same input and give different outputs (if the contents of the file at $filePath change).</li>
156 <li>So how can you possibly do I/O in a pure functional language?</li>
157 <li>It is possible, but that's beyond the scope of this talk (maybe another dev training?)</li>
158 </ul>
159 </ul>
160 </div>
161 </div>
162 <div id="slide-10">
163 <div class="slide-title">=<< What is a Function? >>=</div>
164 <div class="slide">
165 <h1>No equivalent constructs for:</h1>
166 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
167 <ul>
168 <li>For loops</li>
169 <li>While loops</li>
170 <li>Gotos</li>
171 <li>Variables*</li>
172 <li>Probably a bunch of other constructs I forgot</li>
173 </ul>
174 </div>
175 <div class="textbox" style="width: 60%; margin: auto; margin-top:10px;">*In pure functional programming, there are true variables. That is, once you declare the value of a variable, you cannot change it. Contrast this with procedural languages where you can reassign a variable whenever you like.</div>
176 </div>
177 <div class="notes">
178 <h1>Consequences</h1>
179 <ul>
180 <li>As a consequence, of having only pure functions, we have no more control structures D:</li>
181 <li>That raises the question... (go to the next slide xD)</li>
182 </ul>
183 </div>
184 </div>
185 <div id="slide-11">
186 <div class="slide-title">=<< Huh? >>=</div>
187 <div class="slide flexotron">
188 <div class="textbox" style="text-align:center; font-size:40px; text-shadow: 4px 4px #000; padding:20px;" >Who in their right mind would wanna do that?!</div>
189 </div>
190 <div class="notes">
191 <h1>Yeah, Who? Crazies, No Doubt</h1>
192 <ul>
193 <li>It seems paradoxical, features have been removed, but somehow functional programming is just as good? Maybe better?</li>
194 <li>How can taking away all the tools we use to write programs be a good thing?</li>
195 <li>It actually makes no difference. You can accomplish anything in FP that you can accomplish in a procedural language, and gain some cool new superpowers</li>
196 </ul>
197 </div>
198 </div>
199 <div id="slide-12">
200 <div class="slide-title">=<< Huh? >>=</div>
201 <div class="slide">
202 <h1>Why tho?</h1>
203 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
204 <ul>
205 <li>Learning is good</li>
206 <li>Brain plasticity</li>
207 <li>Greater perspective</li>
208 </ul>
209 </div>
210 </div>
211 <div class="notes">
212 <h1>Learn for Big Brain</h1>
213 <ul>
214 <li>Learning functional programming is gonna require you to wrestle your brain in to shapes it's never taken before. Which will leave it with the plasticity to learn a greater variety of things you encounter.</li>
215 <li>It will also make the math/logic part of your brain very buff</li>
216 <li>As mentioned, there is no maths in this talk, but if you learn functional programming you will get buff maths brain</li>
217 <li>And as we know, the brain has 3 main cortexes: The passion cortex, the power cortext, and the maths/logic cortex</li>
218 <li>For the purposes of programming, we need a buff maths/logic cortext, learning functional programming will make us better programmers in general</li>
219 </ul>
220 </div>
221 </div>
222 <div id="slide-13">
223 <div class="slide-title">=<< Paradigms >>=</div>
224 <div class="slide flexotron">
225 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >Paradigms</div>
226 </div>
227 </div>
228 <div id="slide-14">
229 <div class="slide-title">=<< Paradigms >>=</div>
230 <div class="slide flexotron">
231 <div class="textbox flexotron" style="position: relative; width: 30%; height: 50%; margin-right:20px;">
232 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Declarative</span>
233 <div class="textbox" style="position: relative; width: 80%; height: 60%;">
234 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Functional</span>
235 <ul style="text-align: left; margin-top: 65px;margin-left: 40px;">
236 <li>Haskell</li>
237 <li>Elm</li>
238 <li>Elixir</li>
239 <li>Clojure</li>
240 </ul>
241 </div>
242 </div>
243 <div class="textbox flexotron" style="position: relative; width: 30%; height: 50%; margin-right:20px;">
244 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Imperative</span>
245 <div class="textbox" style="position: relative; width: 80%; height: 60%;">
246 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Procedural</span>
247 <ul style="text-align: left; margin-top: 65px;margin-left: 40px;">
248 <li>PHP</li>
249 <li>C</li>
250 <li>Java</li>
251 <li>BASIC</li>
252 </ul>
253 </div>
254 </div>
255 </div>
256 <div class="notes">
257 <h1>The Big Two</h1>
258 <ul>
259 <li>Here are the big two paradigms, and inside those we see sub-paradigms.</li>
260 <li>And then in those there are some languages that embody that paradigm (there are more places languages can exist, for example SQL is declaritive but not functional, and Assembly is imperative but not procedural)</li>
261 <li>Marina made a comment that thinking about SQL while reading these slides is helpful - and I think that's true</li>
262 <ul>
263 <li>In SQL you express things in terms of relations, and there are no for loops, while loops, gotos, etc</li>
264 </ul>
265 <li>For this talk it's OK to just think of declaritive and functional as interchangeable, same with imperative and procedural</li>
266 <li>Foreshaddowing: Imperative and declaritive are both words used to describe human language</li>
267 <li>Learning a new paradigm is beneficial the same way learning a new human language is. It makes you better at programming and communicating.</li>
268 </ul>
269 </div>
270 </div>
271 <div id="slide-15">
272 <div class="slide-title">=<< Gramatical Mood >>=</div>
273 <div class="slide flexotron">
274 <div class="textbox" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
275 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Declarative</span>
276 A sentence which expresses a statement of fact.
277 <ul style="text-align: left; margin-top: 65px;margin-left: 40px;">
278 <li>He runs.</li>
279 <li>I like climbing.</li>
280 <li>Ice is cold.</li>
281 <li>Mathieu is shredded.</li>
282 </ul>
283 </div>
284 <div class="textbox" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
285 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Imperative</span>
286 <span>A sentence which expresses instructions, or requests.</span>
287 <ul style="text-align: left; margin-top: 65px;margin-left: 40px;">
288 <li>Shut the door.</li>
289 <li>Don't eat my burger.</li>
290 <li>Let's go to the pub.</li>
291 <li>Don't make eye contact with Craig.</li>
292 </ul>
293 </div>
294 </div>
295 <div class="notes">
296 <ul>
297 <li>We can see here that in the declaritive mood you express statements of fact. They aren't instrcutions</li>
298 <li>Contrast this with the imperative style of expression where there is a very clear instruction to carry out</li>
299 </ul>
300 </div>
301 </div>
302 <div id="slide-16">
303 <div class="slide-title">=<< Gramatical Mood >>=</div>
304 <div class="slide flexotron">
305 <div class="textbox flexotron" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
306 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Declarative</span>
307 This is this.
308 </div>
309 <div class="textbox flexotron" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
310 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Imperative</span>
311 Do this.
312 </div>
313 </div>
314 <div class="notes">
315 This is the simplest way I can think to express difference
316 </div>
317 </div>
318 <div id="slide-17">
319 <div class="slide-title">=<< Big Brain Moment >>=</div>
320 <div class="slide flexotron">
321 <div class="textbox" style="text-align:center; font-size:40px; text-shadow: 4px 4px #000; padding:20px;" >Programming languages <span class="highlight">ARE</span> human languages.</div>
322 </div>
323 <div class="notes">
324 <h1>We don't write programs for computers<br />We write programs for other humans</h1>
325 </div>
326 <div class="notes">
327 <li>Unless your writing machine code or something like that...</li>
328 </div>
329 </div>
330 <div id="slide-18">
331 <div class="slide-title">=<< Why is declaritive mood good? >>=</div>
332 <div class="slide flexotron">
333 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >Atemporality<br />Independant of or unaffected by time.</div>
334 </div>
335 <div class="notes">
336 <h1>I Can See Through Time!</h1>
337 <ul>
338 <li>In both the human language case, and the programming case, we give up our "instruction words" (in the case of programming, this is for, while, goto, etc)</li>
339 <li>Surely giving instructions is useful, otherwise how do you do anything?!</li>
340 <li>Do we get something good in return? Yes! We get atemporality</li>
341 <ul>
342 <li>This is super powerful and quite different from how we normally write programs</li>
343 <li>When we write in a procedural language we have to think about how things change over time</li>
344 </ul>
345 </ul>
346 </div>
347 </div>
348 <div id="slide-19">
349 <div class="slide-title">=<< Why is declaritive mood good? >>=</div>
350 <div class="slide flexotron">
351 <div style="text-align:center; font-size:50px; text-shadow: 4px 4px #000; padding:20px;" >Declarative programs express what <span class="highlight" style="display: block;">they actually do</span> leaving more "brain space" to focus on solving the problem.</div>
352 </div>
353 <div class="notes">
354 <h1>Expressivity</h1>
355 <ul>
356 <li>In an imperative paradigm, thinking about the program means thinking about changes over time - which means we have to keep track of state in our head</li>
357 <li>In a declaritive paradigm, thinking about the program means thinking about relationships - which means we look at what the thing is, how it relates to the other thing, and move on</li>
358 </ul>
359 </div>
360 </div>
361 <div id="slide-20">
362 <div class="slide-title">=<< To Recap >>=</div>
363 <div class="slide">
364 <h1>Why tho?</h1>
365 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
366 <ul>
367 <li>Because sometimes there is such a thing as too much freedom</li>
368 <li>Functional programming is awesome because it stops you shooting yourself in the foot</li>
369 <li>"Constraints liberate, liberties constrain"</li>
370 <li>There are also classes of problems where the declaritive style is a more elegant way of expressing the solution</li>
371 </ul>
372 </div>
373 <div class="notes">
374 <ul>
375 <li>Some problems are very difficult to solve in an imperative way.</li>
376 <li>Especially when talking about events that are happening in parallel and may run at different speed/times</li>
377 <li>In the imperative paradigm, you quite easily come up against data races by virtue of the way the program is expressed</li>
378 <li>Functional programming takes away entire classes of problems by removing the "instruction words" - no more data races</li>
379 <li>If you ever used JavaScript before promises were a thing, I'm sure you've felt the pain</li>
380 </ul>
381 </div>
382 </div>
383 </div>
384 <div id="slide-21">
385 <div class="slide-title">=<< Some Code (Finally) >>=</div>
386 <div class="slide">
387 <h1>My favourite example</h1>
388 <h2>Consider:</h2>
389 <div style="display: flex;">
390 <pre style="margin: auto;"><code style="font-size: 20px;"><span class="code-friends1 code-friends4">x = x + 1;</span></code></pre>
391 </div>
392 <div style="display: flex; margin-top: 20px; margin-bottom: 20px;">
393 <span style="margin: auto;">and:</span>
394 </div>
395 <div style="display: flex; margin-bottom:50px;">
396 <pre style="margin: auto;"><code style="font-size: 20px;"><span class="code-friends2 code-friends3">function repeat(x) {
397 return x + repeat(x);
398 }</span></code></pre>
399 </div>
400 <h2>OK, so what?</h2>
401 <div style="display: flex;">
402 <div class="textbox" style="margin: auto; width: 60%; font-size: 20px;">In the declaritive paradigm, <span class="code-friends1" onmouseenter="activateFriends(1, true);" onmouseleave="deactivateFriends(1);" style="display: inline-block;">this</span> code hangs while <span class="code-friends2" onmouseenter="activateFriends(2, true);" onmouseleave="deactivateFriends(2);" style="display: inline-block;">this</span> code runs. In the imperative paradigm <span class="code-friends3" onmouseenter="activateFriends(3, true);" onmouseleave="deactivateFriends(3);" style="display: inline-block;">this</span> code hangs, while <span class="code-friends4" onmouseenter="activateFriends(4, true);" onmouseleave="deactivateFriends(4);" style="display: inline-block;">this</span> code runs.</div>
403 </div>
404 </div>
405 <div class="notes">
406 <h1>Interactive Slide!</h1>
407 <ul>
408 <li>Hover over the "this" words in the text box! Notice that the code which hangs is flipped in each paradigm!</li>
409 <li>This code is not any specific language. It's one I made up although it looks similar to other languages</li>
410 <li>The idea is to parse it in the declaritive and the imperative style to see the difference</li>
411 </ul>
412 </div>
413 </div>
414 <div id="slide-22">
415 <div class="slide-title">=<< Some Code (Finally) >>=</div>
416 <div class="slide">
417 <div style="display: flex; margin-top: 50px;">
418 <pre style="margin: auto;"><code style="font-size: 20px;">x = x + 1;</code></pre>
419 </div>
420 <div class="flexotron" style="height: 80%;">
421 <div class="textbox" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
422 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Declarative</span>
423 "x is the same as itself plus one."<br /><br /><br /><br /><br />
424 Complete nonsense!
425 </div>
426 <div class="textbox" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
427 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Imperative</span>
428 "Evaluate the thing on the right of `=` then store it in the thing on the left of `=`."<br /><br /><br />
429 A list of instructions that can be followed.
430 </div>
431 </div>
432 </div>
433 <div class="notes">
434 <h1>Reminder!</h1>
435 <ul>
436 <li>In the declaritive paradigm, = means that two things are one in the same - which leads to a contradiction in our example</li>
437 <li>In the imperative paradigm (assuming x is defined) there's no problem - we just add 1 to x, then save it back in x</li>
438 </ul>
439 </div>
440 </div>
441 <div id="slide-23">
442 <div class="slide-title">=<< Some Code (Finally) >>=</div>
443 <div class="slide">
444 <div style="display: flex; margin-top: 50px;">
445 <pre style="margin: auto;"><code style="font-size: 20px;">function repeat(x) {
446 return x + repeat(x);
447 }</code></pre>
448 </div>
449 <div class="flexotron" style="height: 80%;">
450 <div class="textbox" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
451 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Declarative</span>
452 "The repeat of `x` is `x` appended to the repeat of `x`"<br /><br /><br />
453 An infinite list of `x`!
454 </div>
455 <div class="textbox" style="position: relative; width: 30%; height: 50%; margin-right:20px; padding-top: 40px;">
456 <span class="textbox" style="box-shadow: none; position: absolute; top: -20px; left: 30px;">Imperative</span>
457 "To evaluate the repeate of `x`, first evaluate the repeat of `x`"<br /><br /><br />
458 ...Yeah, you see the problem?
459 </div>
460 </div>
461 </div>
462 <div class="notes">
463 <h1>Atemporality</h1>
464 <ul>
465 <li>Here we are starting to touch on atemporality</li>
466 <li>The declaritive style does not read the definition as instructions. It's not things happening over time. It just is what it is. It's a list of whatever you pass in repeated forever</li>
467 </ul>
468 </div>
469 </div>
470 <div id="slide-24">
471 <div class="slide-title">=<< Big Brain Moment >>=</div>
472 <div class="slide flexotron">
473 <div class="textbox" style="text-align:center; font-size:40px; text-shadow: 4px 4px #000; padding:20px;" >Programming languages <span class="highlight">ARE</span> human languages.</div>
474 </div>
475 <div class="notes">
476 <h1>Recap/Reinforcement</h1>
477 <ul>
478 <li>Imperative tells you what to do</li>
479 <li>Declaritive tells you what things are</li>
480 </ul>
481 </div>
482 </div>
483 <div id="slide-25">
484 <div class="slide-title">=<< Some Code (Finally) >>=</div>
485 <div class="slide">
486 <h1>Real Haskell code that runs</h1>
487 <pre><code class="haskell">repeat x = x ++ repeat x
488 threeFs = take 3 (repeat "f")
489
490 > "fff"
491 </code></pre>
492 </div>
493 <div class="notes">
494 <ul>
495 <li>The first line is the Haskell way of defining the repeat function from before</li>
496 <li>And we read this as: "threeFs is the first three elements in an infinite list of fs"</li>
497 <ul>
498 <li>And that's totally fine! If you have an infinitely long list of fs then of course you can just take the first 3</li>
499 <li>It's a but unfortunate that the function to get n elements is called "take" because that's kinda imperative, but whatever</li>
500 </ul>
501 </ul>
502 </div>
503 </div>
504 <div id="slide-26">
505 <div class="slide-title">=<< Some Code (Finally) >>=</div>
506 <div class="slide">
507 <h1>Real JavaScript code that hangs</h1>
508 <pre><code class="javascript">function repeat(x) {
509 return x + repeat(x);
510 }
511
512 threeFs = repeat("f").substr(0,3);
513
514 > InternalError: too much recursion
515 </code></pre>
516 </div>
517 <div class="notes">
518 <ul>
519 <li>By contrast, this reads as "First compute an infinite list of fs" - which is clearly problematic</li>
520 <li>It's left as an exercise to the reader to try work out why the x = x + 1 example would hang in Haskell</li>
521 </ul>
522 </div>
523 </div>
524 <div id="slide-27">
525 <div class="slide-title">=<< Big Brain Moment >>=</div>
526 <div class="slide">
527 <h1>What did declaritive mode give us?</h1>
528 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
529 <ul>
530 <li>Atemporality</li>
531 <li>The ability to wield infinity!</li>
532 </ul>
533 </div>
534 </div>
535 <div class="notes">
536 <h1>Big Brain Moment Two!</h1>
537 <ul>
538 <li>How cool is that? Because we can express what things are, we can express an infininte list!</li>
539 <li>Often when we write programs, we have to worry about things cascading out infinitely, but not in the declaritive paradigm!</li>
540 <li>None of the declaritive version of the program relies on us traking changes anywhere. We just need to know what a thing is and how it relates to another thing</li>
541 <ul>
542 <li>And if that thing happens to be infinite, who cares! That's just what it is</li>
543 </ul>
544 </ul>
545 </div>
546 </div>
547 <div id="slide-28">
548 <div class="slide-title">=<< Promises >>=</div>
549 <div class="slide flexotron">
550 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >Promises</div>
551 </div>
552 <div class="notes">
553 Seems like an odd tangent but let's go with it. And just as before, I'm gonna break one of the carinal rules of teaching by telling you what promises are not...
554 </div>
555 </div>
556 <div id="slide-29">
557 <div class="slide-title">=<< Promises >>=</div>
558 <div class="slide flexotron">
559 <div style="position: relative; text-align:center; font-size:40px; text-shadow: 4px 4px #000;" >
560 <div class="angereyWrap"><div class="angerey">>:(</div></div>
561 <div class="angereyWrap"><div class="angerey">>:(</div></div>
562 <div class="angereyWrap"><div class="angerey">>:(</div></div>
563 <div class="angereyWrap"><div class="angerey">>:(</div></div>
564 <div class="angereyWrap"><div class="angerey">>:(</div></div>
565 <div class="angereyWrap"><div class="angerey">>:(</div></div>
566 <div class="angereyWrap"><div class="angerey">>:(</div></div>
567 <div class="angereyWrap"><div class="angerey">>:(</div></div>
568 <div class="angereyWrap"><div class="angerey">>:(</div></div>
569 <div class="angereyWrap"><div class="angerey">>:(</div></div>
570 Promises <span class="shake-slow shake-constant" style="display: inline-block; color: #f98012">do not</span> make things asynchronous!</div>
571 </div>
572 <div class="notes">
573 <ul>
574 <li>There's a lot of misleading information out there about promises</li>
575 <li>Some people believe that promises "make things asynchronous", but this isn't true</li>
576 <li>You can have perfectly synchronous promises, the thing you pass as a callback to a promise does not get "run in the background" or anything like that</li>
577 </ul>
578 </div>
579 </div>
580 <div id="slide-30">
581 <div class="slide-title">=<< Promises >>=</div>
582 <div class="slide">
583 <h1>The real promises</h1>
584 <h2>What problems do promises solve?</h2>
585 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
586 <ul>
587 <li>Asynchronous request management</li>
588 <li>Error handling for computations which may fail</li>
589 <li>Probably more too</li>
590 </ul>
591 </div>
592 <h2 style="margin-top: 50px;">What are promises then?</h2>
593 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
594 <ul>
595 <li>A value (which may or may not be available yet) wrapped in some additional context</li>
596 <li>Functions to instantiate the wrapper (`Promise.resolve`, `Promise.reject`)</li>
597 <li>A rule for composing these wrapped values (`then`)</li>
598 </ul>
599 </div>
600 </div>
601 <div class="notes">
602 <ul>
603 <li>The "value" is where the asynchronous part comes in</li>
604 <li>That value may become available later, but it may just be there the entire time as well</li>
605 </ul>
606 </div>
607 </div>
608 <div id="slide-31">
609 <div class="slide-title">=<< Promises >>=</div>
610 <div class="slide">
611 <h1>Terminology/syntax recap</h1>
612 <h2>JavaScript arrow functions</h2>
613 <div style="display: flex;">
614 <pre style="margin: auto;"><code style="font-size: 20px;">x => x + 1;</code></pre>
615 </div>
616 <div style="display: flex; margin-top: 20px; margin-bottom: 20px;">
617 <span style="margin: auto;">is equivalent to:</span>
618 </div>
619 <div style="display: flex; margin-bottom:50px;">
620 <pre style="margin: auto;"><code style="font-size: 20px;">function(x) {
621 return x+1;
622 }</code></pre>
623 </div>
624 <h2>Composition</h2>
625 <div style="display: flex;">
626 <div class="textbox" style="margin: auto; width: 60%; font-size: 20px;">A method of combining two things of the same type in to another thing that is also of the same type</div>
627 </div>
628 </div>
629 <div class="notes">
630 <h1>Regarding Composition</h1>
631 <ul>
632 <li>You may have come across function composition. There's lots of different kinds of composition, but function composition is incredibly common, especially in programming. But we'll talk about the composition of promises.</li>
633 <li>In both cases the idea is the same. Function composition lets you combine two functions in to another function. Promise composition lets you combine two promises in to another promise</li>
634 </ul>
635 </div>
636 </div>
637 <div id="slide-32">
638 <div class="slide-title">=<< Promises >>=</div>
639 <div class="slide">
640 <h1>Promise composition</h1>
641 <div style="display: flex;">
642 <div class="textbox" style="margin: auto; width: 60%; font-size: 20px;">The `then` method of a promise accepts a function as an argument. This function takes a value and returns a new promise. The new promise "combines" the previous two using the "composition rule" given to `then`.</div>
643 </div>
644 <div style="display: flex; margin-top: 100px">
645 <pre style="margin: auto;"><code style="font-size: 12px;"><span class="promise3"><span class="promise2"><span class="promise1">Promise.resolve("Some value")</span>.then(x => Promise.resolve(x + " and another value"))</span>.then(x => Promise.resolve(x + " and another one!"))</span>;</code></pre>
646 </div>
647 <div style="display: flex;">
648 <div class="textbox" style="margin: auto; width: 60%; font-size: 20px; margin-top: 50px;">There are three promises in the above snippet. <span onmouseenter="activatePromise(1)" onmouseleave="deactivatePromise(1)" class="promise1">One</span>, <span onmouseenter="activatePromise(2)" onmouseleave="deactivatePromise(2)" class="promise2">two</span>, <span onmouseenter="activatePromise(3)" onmouseleave="deactivatePromise(3)" class="promise3">three</span>.</div>
649 </div>
650 <div style="display: flex; margin-top: 100px">
651 <pre style="margin: auto;"><code style="font-size: 12px;">Promise.resolve("Some value").then(x => Promise.reject("It's all gone wrong")).then(x => Promise.resolve(x + " and another one!"));</code></pre>
652 </div>
653 <div style="display: flex;">
654 <div class="textbox" style="margin: auto; width: 60%; font-size: 20px; margin-top: 50px;">The above snippet still results in a promise, but it is a "failed" one. Any subsequent `then` after the `reject` has no effect.</div>
655 </div>
656 </div>
657 <div class="notes">
658 <h1>Interactive Slide!</h1>
659 <ul>
660 <li>Hover over "one", "two", and "three" to see the promises highlighed</li>
661 <ul>
662 <li>The first one is all by itself</li>
663 <li>The second one is the composition of the first one using the composition rule (the function passed to `then`)</li>
664 <li>The third one is the composition of the second one using the composition rule (the function given to `then`)</li>
665 </ul>
666 <li>The `then` method accepts a function which:</li>
667 <ul>
668 <li>Will be passed the previous promise's wrapped value</li>
669 <li>Causing it return the next promise</li>
670 <li>Which will give its wrapped value to the function passed to the next `then` method</li>
671 </ul>
672 <li>It's very important to realise that we are passing a function to then, not a value, it's not actually doing anything when we set up the promise chain</li>
673 </ul>
674 </div>
675 </div>
676 <div id="slide-33">
677 <div class="slide-title">=<< Promises >>=</div>
678 <div class="slide">
679 <h1>OK great but who cares?</h1>
680 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
681 <ul>
682 <li>We are <span class="highlight" style="display: inline-block;">declaring</span> how we want promises to compose</li>
683 <li>
684 The order that the promises actually resolve their values in is not important
685 <ul><li>In other words, we don't have to worry about <span class="highlight" style="display: inline-block;">timing</span> anymore</li></ul>
686 </li>
687 <li>We can <span class="highlight" style="display: inline-block;">declare</span> a failure anywhere in the chain</li>
688 </ul>
689 </div>
690 </div>
691 <div class="notes">
692 <h1>Pls Note</h1>
693 <ul>
694 <li>Since this is JavaScript, you can mutate state in the functions that are passed to "then"</li>
695 <li>But that's beside the point I'm trying to demonstrate, and you shouldn't really do that anyway</li>
696 </ul>
697 </div>
698 </div>
699 <div id="slide-34">
700 <div class="slide-title">=<< Promises >>=</div>
701 <div class="slide flexotron">
702 <div style="position: relative;">
703 <img src="img/thonking.gif" />
704 <div class="thonkingWrap"><div class="thonk">atemporality?</div></div>
705 <div class="thonkingWrap"><div class="thonk">declaritive?</div></div>
706 <div class="thonkingWrap"><div class="thonk">functions?</div></div>
707 <div class="thonkingWrap"><div class="thonk">composition?</div></div>
708 </div>
709 </div>
710 <div class="notes">
711 This sure is starting to sound familiar.
712 </div>
713 </div>
714 <div id="slide-35">
715 <div class="slide-title">=<< Big Brain Moment >>=</div>
716 <div class="slide">
717 <h1>Woah!</h1>
718 <h2>JavaScript</h2>
719 <pre style="margin-bottom: 50px;"><code class="javascript"><span class="code-friends1" onmouseenter="activateFriends(1);" onmouseleave="deactivateFriends(1);" style="display: inline-block">Promise.resolve</span>(<span class="code-friends2" onmouseenter="activateFriends(2);" onmouseleave="deactivateFriends(2);" style="display: inline-block">"We start here"</span>)
720 <span class="code-friends3" onmouseenter="activateFriends(3);" onmouseleave="deactivateFriends(3);" style="display: inline-block">.then</span>(<span class="code-friends4" onmouseenter="activateFriends(4);" onmouseleave="deactivateFriends(4);" style="display: inline-block">x =></span> <span class="code-friends5" onmouseenter="activateFriends(5);" onmouseleave="deactivateFriends(5);" style="display: inline-block">Promise.resolve</span>(<span class="code-friends6" onmouseenter="activateFriends(6);" onmouseleave="deactivateFriends(6);" style="display: inline-block">x + " then get here"</span>))
721 <span class="code-friends7" onmouseenter="activateFriends(7);" onmouseleave="deactivateFriends(7);" style="display: inline-block">.then</span>(<span class="code-friends8" onmouseenter="activateFriends(8);" onmouseleave="deactivateFriends(8);" style="display: inline-block">x =></span> <span class="code-friends9" onmouseenter="activateFriends(9);" onmouseleave="deactivateFriends(9);" style="display: inline-block">Promise.resolve</span>(<span class="code-friends10" onmouseenter="activateFriends(10);" onmouseleave="deactivateFriends(10);" style="display: inline-block">x + " and finally here!"</span>))</code></pre>
722 <h2>Haskell</h2>
723 <pre><code class="haskell"><span class="code-friends1" onmouseenter="activateFriends(1);" onmouseleave="deactivateFriends(1);" style="display: inline-block">Right</span>(<span class="code-friends2" onmouseenter="activateFriends(2);" onmouseleave="deactivateFriends(2);" style="display: inline-block">"We start here"</span>)
724 <span class="code-friends3" onmouseenter="activateFriends(3);" onmouseleave="deactivateFriends(3);" style="display: inline-block">>>=</span> (<span class="code-friends4" onmouseenter="activateFriends(4);" onmouseleave="deactivateFriends(4);" style="display: inline-block">\x -> </span><span class="code-friends5" onmouseenter="activateFriends(5);" onmouseleave="deactivateFriends(5);" style="display: inline-block">Right</span> (<span class="code-friends6" onmouseenter="activateFriends(6);" onmouseleave="deactivateFriends(6);" style="display: inline-block">x ++ " then get here"</span>))
725 <span class="code-friends7" onmouseenter="activateFriends(7);" onmouseleave="deactivateFriends(7);" style="display: inline-block">>>=</span> (<span class="code-friends8" onmouseenter="activateFriends(8);" onmouseleave="deactivateFriends(8);" style="display: inline-block">\x -></span> <span class="code-friends9" onmouseenter="activateFriends(9);" onmouseleave="deactivateFriends(9);" style="display: inline-block">Right</span> (<span class="code-friends10" onmouseenter="activateFriends(10);" onmouseleave="deactivateFriends(10);" style="display: inline-block">x ++ " and finally here!"</span>))</code></pre>
726 </div>
727 <div class="notes">
728 <h1>Interactive Slide!</h1>
729 <ul>
730 <li>Hover over the language elements to see how they pair up in each language!</li>
731 <li>Promises are very similar to a type in Haskell called "Either". "Either" can be instantiated with `Right` or `Left` which correspond to `resolve` and `reject`, respectively</li>
732 <li>It does not matter what order the promises resolve in, the final promise will always be resolved with "We start here then get here and finally here"</li>
733 <li>Bas made a good point that when looking at the calls to "then" you think about the things happening one after another.</li>
734 <ul>
735 <li>While that's true, what `then` does is describe the way to combine the results once they arrive</li>
736 <li>The "We start here" result could come from a web request that takes 10 seconds, whereas the other strings could resolve instantly, but the final promise would still be "We start here then get here and finally here" - even though that was not the order we got the results</li>
737 <li>Be careful, the `x` in each "then" call is not the same `x`. It's part of the function which is being passed to then, which means it is in a completely different scope. The purpose of the function is to describe what you want to happen</li>
738 <li>It's hard to untwist your brain from thinking about promises in the procedural way - this is what I meant by "wrestling your brain in to new shapes" :)</li>
739 </ul>
740 <li>In this example, there is nothing asynchronous happening, but it's not much work to adapt it so there is. I mainly wanted to really the similarities with haskell</li>
741 <li>How cool is it that these look so similar?</li>
742 <li>As mentioned, in the JavaScript version, you could mutate something inside the `then` functions - but this would absolutely lead to data races</li>
743 <li>In Haskell, you cannot do that, which is one of the ways it stops you shooting yourself in the foot</li>
744 <li>Noel made an interesting comment that JavaScript added async/await as "syntactic sugar" to make this kind of code look more imperative.</li>
745 <ul>
746 <li>And I was pleased to say that the exact same thing exists in Haskell :)</li>
747 </ul>
748 </ul>
749 </div>
750 </div>
751 <div id="slide-36">
752 <div class="slide-title">=<< Big Brain Moment >>=</div>
753 <div class="slide">
754 <h1>Woah!</h1>
755 <h2>JavaScript</h2>
756 <pre style="margin-bottom: 50px;"><code class="javascript"><span class="code-friends1" onmouseenter="activateFriends(1);" onmouseleave="deactivateFriends(1);" style="display: inline-block">Promise.resolve</span>(<span class="code-friends2" onmouseenter="activateFriends(2);" onmouseleave="deactivateFriends(2);" style="display: inline-block">"We start here"</span>)
757 <span class="code-friends3" onmouseenter="activateFriends(3);" onmouseleave="deactivateFriends(3);" style="display: inline-block">.then</span>(<span class="code-friends4" onmouseenter="activateFriends(4);" onmouseleave="deactivateFriends(4);" style="display: inline-block">x =></span> <span class="code-friends5" onmouseenter="activateFriends(5);" onmouseleave="deactivateFriends(5);" style="display: inline-block">Promise.resolve</span>(<span class="code-friends6" onmouseenter="activateFriends(6);" onmouseleave="deactivateFriends(6);" style="display: inline-block">x + " then get here"</span>))
758 <span class="code-friends11" onmouseenter="activateFriends(11);" onmouseleave="deactivateFriends(11);" style="display: inline-block">.then</span>(<span class="code-friends12" onmouseenter="activateFriends(12);" onmouseleave="deactivateFriends(12);" style="display: inline-block">x =></span> <span class="code-friends13" onmouseenter="activateFriends(13);" onmouseleave="deactivateFriends(13);" style="display: inline-block">Promise.reject</span>(<span class="code-friends14" onmouseenter="activateFriends(14);" onmouseleave="deactivateFriends(14);" style="display: inline-block">"Oh noes!"</span>))
759 <span class="code-friends7" onmouseenter="activateFriends(7);" onmouseleave="deactivateFriends(7);" style="display: inline-block">.then</span>(<span class="code-friends8" onmouseenter="activateFriends(8);" onmouseleave="deactivateFriends(8);" style="display: inline-block">x =></span> <span class="code-friends9" onmouseenter="activateFriends(9);" onmouseleave="deactivateFriends(9);" style="display: inline-block">Promise.resolve</span>(<span class="code-friends10" onmouseenter="activateFriends(10);" onmouseleave="deactivateFriends(10);" style="display: inline-block">x + " and finally here!"</span>))</code></pre>
760 <h2>Haskell</h2>
761 <pre><code class="haskell"><span class="code-friends1" onmouseenter="activateFriends(1);" onmouseleave="deactivateFriends(1);" style="display: inline-block">Right</span>(<span class="code-friends2" onmouseenter="activateFriends(2);" onmouseleave="deactivateFriends(2);" style="display: inline-block">"We start here"</span>)
762 <span class="code-friends3" onmouseenter="activateFriends(3);" onmouseleave="deactivateFriends(3);" style="display: inline-block">>>=</span> (<span class="code-friends4" onmouseenter="activateFriends(4);" onmouseleave="deactivateFriends(4);" style="display: inline-block">\x -> </span><span class="code-friends5" onmouseenter="activateFriends(5);" onmouseleave="deactivateFriends(5);" style="display: inline-block">Right</span> (<span class="code-friends6" onmouseenter="activateFriends(6);" onmouseleave="deactivateFriends(6);" style="display: inline-block">x ++ " then get here"</span>))
763 <span class="code-friends11" onmouseenter="activateFriends(11);" onmouseleave="deactivateFriends(11);" style="display: inline-block">>>=</span> (<span class="code-friends12" onmouseenter="activateFriends(12);" onmouseleave="deactivateFriends(12);" style="display: inline-block">\x -></span> <span class="code-friends13" onmouseenter="activateFriends(13);" onmouseleave="deactivateFriends(13);" style="display: inline-block">Left </span>(<span class="code-friends14" onmouseenter="activateFriends(14);" onmouseleave="deactivateFriends(14);" style="display: inline-block">"Oh noes!"</span>))
764 <span class="code-friends7" onmouseenter="activateFriends(7);" onmouseleave="deactivateFriends(7);" style="display: inline-block">>>=</span> (<span class="code-friends8" onmouseenter="activateFriends(8);" onmouseleave="deactivateFriends(8);" style="display: inline-block">\x -></span> <span class="code-friends9" onmouseenter="activateFriends(9);" onmouseleave="deactivateFriends(9);" style="display: inline-block">Right</span> (<span class="code-friends10" onmouseenter="activateFriends(10);" onmouseleave="deactivateFriends(10);" style="display: inline-block">x ++ " and finally here!"</span>))</code></pre>
765 </div>
766 <div class="notes">
767 <h1>You Were Doing FP all Along!</h1>
768 <ul>
769 <li>Likewise, we can put a failure anywhere in the chain. It doesn't matter if the failure happens first, last, or anywhere in between. The result is always a failed promise of "Oh noes!"</li>
770 <li>So, if you've ever used promises, you've been doing declaritive style programming without even realising it!</li>
771 <li>They give us some new powers, we can now write atemporal code, which explains why they emerged in JS, a place where people are constantly dealing with requests that can finish in any order</li>
772 <li>So it's super cool that people using a language very far removed from haskell ended up solving the problem in the same way</li>
773 <li>Although both are independed discoveries, I don't think it's a coincidence promises appeared given the nature of the JS world.</li>
774 </ul>
775 </div>
776 </div>
777 <div id="slide-37">
778 <div class="slide-title">=<< Composition >>=</div>
779 <div class="slide flexotron">
780 <div style="text-align:center; font-size:60px; text-shadow: 4px 4px #000; padding:20px;" >Composition</div>
781 </div>
782 <div class="notes">
783 <h1>The Section on Composition!</h1>
784 <ul>
785 <li>This gets its own section because it is not just important to programming, it's important to life in general</li>
786 </ul>
787 </div>
788 </div>
789 <div id="slide-38">
790 <div class="slide-title">=<< Composition >>=</div>
791 <div class="slide flexotron">
792 <img src="img/composition0.png" style="height: 700px;" />
793 </div>
794 </div>
795 <div id="slide-39">
796 <div class="slide-title">=<< Composition >>=</div>
797 <div class="slide flexotron">
798 <div style="position: relative;">
799 <div style="position: absolute; top: -40px; left: -106px">If we can go from here to here</div>
800 <img src="img/composition1.png" style="height: 700px;" />
801 </div>
802 </div>
803 </div>
804 <div id="slide-40">
805 <div class="slide-title">=<< Composition >>=</div>
806 <div class="slide flexotron">
807 <div style="position: relative;">
808 <div style="position: absolute; top: -40px; left: -106px">If we can go from here to here</div>
809 <div style="position: absolute; max-width: 425px; text-align: center; bottom: 149px; left: 220px">And we can go from here to here</div>
810 <img src="img/composition2.png" style="height: 700px;" />
811 </div>
812 </div>
813 </div>
814 <div id="slide-41">
815 <div class="slide-title">=<< Composition >>=</div>
816 <div class="slide flexotron">
817 <div style="position: relative;">
818 <div style="position: absolute; top: -40px; left: -106px">If we can go from here to here</div>
819 <div style="position: absolute; max-width: 425px; text-align: center; bottom: 149px; left: 220px">And we can go from here to here</div>
820 <div style="position: absolute; text-align: center; bottom: -70px; left: 82px">Then the composition (if it exists) goes from here to here</div>
821 <img src="img/composition3.png" style="height: 700px;" />
822 </div>
823 </div>
824 <div class="notes">
825 <h1>Coins and Pipes!</h1>
826 <ul>
827 <li>Keep in mind the the composition of A->B and B->C is not gauranteed to exist!</li>
828 <li>I'm using coins and pipes to demonstrate, but the coins and pipes could be anything... Functions, promises, etc.</li>
829 <li><span class="highlight">Stop reading here!!</span> After reading the next slide come back to this one pls :)</li>
830 <ul>
831 <li>We want to go from A to C - but maybe that's really hard (I accidentally illustrated that in the diagram! The pipe from A to C is really long! But the A->B->C route is short!)</li>
832 <li>Perhaps someone worked out how to go from A to B, and someone else worked out how to go from B to C</li>
833 <li>If we're in a system where composition is possible, then we just solved the problem!</li>
834 <li>This is the natural way people solve problems</li>
835 <ul>
836 <li>We like to break things up in to small chunks and solve the small bits and then put them back together</li>
837 <li>You probably do it when you're writing software at Moodle!</li>
838 </ul>
839 </ul>
840 <li><span class="highlight">Stop reading here!!</span> After getting to the "What's this got to do with FP" slide, come back pls :)</li>
841 <ul>
842 <li>Maybe A->B has a side effect, this means when we call B->C with the result from A->B, it may not be the same as calling A->C. The point of composition is that the outcome of calling both should be indistinguishable.</li>
843 <li>Procedures in imperative languages (what most programmers call functions) are a short sighted attempt at composition, we wanted a way to break things up, and sure... You can put functions together in imperative languages and it kinda works, but it's not the same as true composition</li>
844 <li>In the functional paradigm, composition is gauranteed because functions cannot have side effects</li>
845 </ul>
846 </ul>
847 </div>
848 </div>
849 <div id="slide-42">
850 <div class="slide-title">=<< Composition >>=</div>
851 <div class="slide">
852 <h1>OK great but who cares?</h1>
853 <div class="textbox" style="width: 60%; margin: auto;">Big problems can often be chopped up in to smaller, easier to solve problems. If we can be clever about how we chop them up, such that the solutions to the individual problems can <span class="highlight">compose</span> together to form a full solution, then we can infinitely speed up the process by adding more workers!</div>
854 </div>
855 <div class="notes">
856 Remember to go back to the previous slide!
857 </div>
858 </div>
859 <div id="slide-43">
860 <div class="slide-title">=<< Composition >>=</div>
861 <div class="slide">
862 <h1>Examples</h1>
863 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
864 <ul>
865 <li>The pyramids</li>
866 <li>The LHC</li>
867 <li>McDonald's</li>
868 <li>Moodle!</li>
869 </ul>
870 </div>
871 </div>
872 <div class="notes">
873 <h1>Composition Power!</h1>
874 <ul>
875 <li>The pyramids were build by having loads of peope work in parallel</li>
876 <li>Same with the LHC - I don't believe there is a single person in the world who fully understands it</li>
877 <ul>
878 <li>But with lots of people working together we can create this amazing thing which recreated conditions similar to that at the near-instant our universe began!</li>
879 <li>That's pretty amazing</li>
880 </ul>
881 <li>And with McDonald's - Instead of having one chef "make a burger" staff make individual parts of the burger and compose it together (I think McDonald's was the first place to do this)</li>
882 <ul>
883 <li>You can make a lot more burgers that way!</li>
884 </ul>
885 <li>And Moodle does it too! Let's think about how we tackle projects...</li>
886 </ul>
887 </div>
888 </div>
889 <div id="slide-44">
890 <div class="slide-title">=<< Composition >>=</div>
891 <div class="slide flexotron">
892 <div style="height:80%;" >
893 <img src="img/10xdev.png" class="textbox" style="margin-bottom: 20px; 40px; height: 60%; display: block; margin: auto; padding-bottom: 0px;" />
894 <div class="textbox" style="margin-top: 20px !important; width: 50%; margin: auto; font-style: italic;">"With these biceps I can solve <span class="highlight shake shake-constant" style="display: inline-block">ANYTHING</span>"</div>
895 </div>
896 </div>
897 <div class="notes">
898 Do we get one ultimate developer to smash out the whole thing? idk about you, but I wouldn't be keen to work with this person. Check out <a href="https://www.freecodecamp.org/news/we-fired-our-top-talent-best-decision-we-ever-made-4c0a99728fde/">this story</a>.
899 </div>
900 </div>
901 <div id="slide-45">
902 <div class="slide-title">=<< Composition >>=</div>
903 <div class="slide flexotron">
904 <div class="textbox"><h1>No, you get friends to take a piece and help!</h1></div>
905 <div id="cloudguys">
906 <div>
907 <span>henlo</span>
908 <img src="img/cloudguy.gif" />
909 </div>
910 <div>
911 <span>henlo</span>
912 <img src="img/cloudguy.gif" />
913 </div> <div>
914 <span>henlo</span>
915 <img src="img/cloudguy.gif" />
916 </div>
917 <div>
918 <span>henlo</span>
919 <img src="img/cloudguy.gif" />
920 </div>
921 <div>
922 <span>henlo</span>
923 <img src="img/cloudguy.gif" />
924 </div>
925 <div>
926 <span>henlo</span>
927 <img src="img/cloudguy.gif" />
928 </div>
929 </div>
930 </div>
931 <div class="notes">
932 <h1>Teamwork!</h1>
933 <ul>
934 <li>Of course not, we do it as a team!</li>
935 <li>This is why it's worth spending the time to plan things and figure out how to "chop up" the work</li>
936 <li>It's very important to make sure the way we "chop" the problem allows the solutions to be composed together later</li>
937 </ul>
938 </div>
939 </div>
940 <div id="slide-46">
941 <div class="slide-title">=<< Composition >>=</div>
942 <div class="slide flexotron">
943 <div class="textbox" style="text-align:center; font-size:40px; text-shadow: 4px 4px #000; padding:20px;" >Composition is nature's greatest hack</div>
944 </div>
945 <div class="notes">
946 <h1>Why Was All that Relevant?</h1>
947 <ul>
948 <li>My goal here was to convince you that composition is fundamental to the way we think</li>
949 <li>Humans are amazing for our ability to break down problems in to manageable chunks and then compose them together - I don't think other animals do that</li>
950 <ul>
951 <li>Which is probably a big part of how we dominated this planet so fast</li>
952 </ul>
953 <li>At its core, composition is about relationships, and building complexity by combining them. Hey, wasn't the whole declaritive thing about relationships?</li>
954 <ul>
955 <li>At the beginning I said in the declaritive paradigm we express things in terms of relationships...</li>
956 </ul>
957 <li>And that brings me to the final point</li>
958 </ul>
959 </div>
960 </div>
961 <div id="slide-47">
962 <div class="slide-title">=<< Composition >>=</div>
963 <div class="slide">
964 <h1>What's this got to do with FP?</h1>
965 <div class="textbox" style="width: 60%; margin: auto;">When writing programs (as an individual in this case, not as a group) we naturally want to break the solution up in to "building blocks" that we put together to solve the problem (it's composition again).</div>
966 <div class="textbox" style="width: 60%; margin: auto; margin-top: 20px;">The building blocks you use matter. Some compose better than others. Because of all the restrictions the functional paradigm places on the programmer (lack of mutation, lack of control flow), functions in a pure language are naturally well-suited to composition.</div>
967 <div class="textbox" style="width: 60%; margin: auto; margin-top: 20px;">This is becoming more relevant than ever in the age of parallel computing</div>
968 </div>
969 <div class="notes">
970 <ul>
971 <li>Functional programming is great because it gaurantees composition</li>
972 <li>In a procedural language, things don't necessarily compose</li>
973 <li>The building blocks we use for our compositions matter (whether it be programs, teamwork, whatever)</li>
974 <ul>
975 <li>The restrictions that the functional paradigm places on you means you cannot work with blocks that do not compose</li>
976 <li>So by taking away our "instruction words" we have gained another powerful tool: composition</li>
977 <li>Remember that slide I said to go back to (the coins and pipes diagram)? Do that now pls! Then come back here cos there's a few more slides after this one.</li>
978 </ul>
979 </div>
980 </div>
981 <div id="slide-48">
982 <div class="slide-title">=<< Closing Notes >>=</div>
983 <div class="slide">
984 <h1>FP is awesome because:</h1>
985 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
986 <ul>
987 <li>By taking away convenient, but dangerous tools we are left with tools that are incredibly powerful, but more difficult to wield</li>
988 <li>In particular</li>
989 <ul>
990 <li>Atemporality: The ability to express things independant of time</li>
991 <li>Infinity: Infinite recursion can't stop you now!</li>
992 <li>Composition: The fundamental way humans solve problems is available to you at all times</li>
993 </ul>
994 </ul>
995 </div>
996 </div>
997 <div class="notes">
998 New super powers!
999 </div>
1000 </div>
1001 <div id="slide-49">
1002 <div class="slide-title">=<< Closing Notes >>=</div>
1003 <div class="slide">
1004 <h1>Very big brain thoughts:</h1>
1005 <div class="textbox" style="width: 60%; margin: auto; text-align: left;">
1006 <ul>
1007 <li>We work across multiple timezones, so being able to solve problems in a time-independant way seems appealing, doesn't it?</li>
1008 <li>Moore's law is coming to an end, now the focus is shifting to parallel computing</li>
1009 <ul>
1010 <li>Imperative paradigms will not be the right tool for this</li>
1011 <li>Either we'll see a shift to declaritive style programming</li>
1012 <li>Or we'll make life hard for ourselves real fast</li>
1013 </ul>
1014 <li>Maybe quantum mechanics is so damn confusing because at a certain point we can't decompose things anymore?</li>
1015 <ul><li>After all... why should The Universe conform to the way our brains want it to be...</li></ul>
1016 </ul>
1017 </div>
1018 </div>
1019 <div class="notes">
1020 <ul>
1021 <li>In regards to Moore's law, I personally hope we start seeing a paradigm shift soon. Because that's the definition of madness really. Doing the same thing over and over but expecting different results.</li>
1022 <li>Other analogies like the frog in slowly boiling water come to mind</li>
1023 <li>And that's about it really!</li>
1024 </ul>
1025 </div>
1026 </div>
1027 <div id="slide-50">
1028 <div id="cloudguy2">
1029 <div style="height: 20%; text-align: center;">
1030 <img style="height: 100%; display: block; margin-left: auto; margin-right: auto; margin-bottom: 10px;" src="img/cloudguy.gif" />
1031 <div style="width: 80%; margin: auto;">Thanks for participating!!</div>
1032 </div>
1033 </div>
1034 <div class="notes">
1035 <ul>
1036 <li>If nothing else, I really hope you're now just a little bit more curious about functional programming</li>
1037 <li>And even though functional programing is not incredibly relevant to our roles just now, learning more about it will make you a better programmer, and that benefits everyone :)</li>
1038 <li>If you have any questions please message me, I'm always happy to talk about programming</li>
1039 <li>I'd suggest going to the pub to talk about it, but, well, we can't do that just now</li>
1040 <li>Bas mentioned that Haskell has a reputation for being academic</li>
1041 <ul>
1042 <li>It's true that it was initially written for academic purposes</li>
1043 <li>But that was over 30 years ago and it has now evolved in to something that is usable in production</li>
1044 </ul>
1045 <li>Bas also asked about real world Haskell applications, here's a few I know of</li>
1046 <ul>
1047 <li>Facebook's spam filter is written in Haskell (and they even contributed back to the Haskell project)</li>
1048 <li>Loads of banks use it for the reason that it's hard to write software that "goes wrong" in Haskell</li>
1049 <li>Target use a program written in Haskell to optimise supply chains</li>
1050 <li>NASA!</li>
1051 </ul>
1052 <li>And here's some resources!</li>
1053 <ul>
1054 <li><a href="http://learnyouahaskell.com/introduction">Learn You a Haskell for Great Good</a></li>
1055 <li><a href="https://haskellbook.com/">Haskell Book</a></li>
1056 <li>Me! I'm no master, but I'll always be happy to talk about Haskell and functional programming - send me a mesage!</li>
1057 </ul>
1058 <li>And remember, you can't spell "Functional Programming" without "Fun"!</li>
1059 </ul>
1060 </div>
1061 </div>
1062 </body>
1063 <script>
1064 var div, i, j, span, duration = "";
1065 var crazyTexts = [];
1066 var opacyTexts = [];
1067 div = document.querySelectorAll("#rant > span.crazy");
1068 for (j = 0; j < div.length; j++) {
1069 span=""
1070 for (i = 0; i < div[j].innerText.length; i++) {
1071 duration = Math.floor(Math.random() * 5) + 100
1072 span += "<span class=\"letter shake-constant shake-little\" style=\"animation-duration: " + duration + "ms\">";
1073 span += div[j].innerText[i] == " " ? "&nbsp;" : div[j].innerText[i];
1074 span += "</span>";
1075 }
1076 crazyTexts.push(span);
1077 }
1078
1079 opacities = document.querySelectorAll("#rant > span.shake-opacity");
1080 for (k = 0; k < opacities.length; k++) {
1081 shrek=Math.random();
1082 opacities[k].style.animationDuration = shrek + 's';
1083 opacyTexts.push(opacities[k].outerHTML);
1084 }
1085
1086 allText = ""
1087 for (l = 0; l< crazyTexts.length; l++) {
1088 allText+=crazyTexts[l] + (opacyTexts[l] ? opacyTexts[l] : "");
1089 }
1090
1091 document.getElementById('rant').innerHTML = allText;
1092
1093 var x = 0;
1094 var y = 0;
1095 var b = document.querySelectorAll('body')[0];
1096 setInterval(function(){
1097 x+=2;
1098 y-=2;
1099
1100 b.style.backgroundPosition = x + 'px ' + y + 'px';
1101 }, 250);
1102
1103 urlParts = document.URL.split('#');
1104
1105 var currentSlide = urlParts.length > 1 ? urlParts[1] : 0;
1106 var bc = new BroadcastChannel('test_channel');
1107 document.getElementById('slide-' + currentSlide).style.display = 'block';
1108 document.onkeydown = checkKey;
1109
1110 notes = document.querySelectorAll('#slide-' + currentSlide + ' .notes');
1111 if(!!notes[0]) {1
1112 bc.postMessage(notes[0].innerHTML);
1113 }
1114
1115 numSlides = document.querySelectorAll('div[id^="slide-"]').length;
1116
1117 function checkKey(e) {
1118
1119 e = e || window.event;
1120 if (e.keyCode == '37') {
1121 if (document.getElementById('slide-' + currentSlide)) {
1122 document.getElementById('slide-' + currentSlide).style.display = 'none';
1123 }
1124 currentSlide--;
1125 document.getElementById('slide-' + currentSlide).style.display = 'block';
1126
1127 notes = document.querySelectorAll('#slide-' + currentSlide + ' .notes');
1128 if(!!notes[0]) {
1129 bc.postMessage(notes[0].innerHTML);
1130 } else {
1131 bc.postMessage("");
1132 }
1133 }
1134 else if (e.keyCode == '39') {
1135 if (document.getElementById('slide-' + currentSlide)) {
1136 document.getElementById('slide-' + currentSlide).style.display = 'none';
1137 }
1138 currentSlide++;
1139 document.getElementById('slide-' + currentSlide).style.display = 'block';
1140
1141 notes = document.querySelectorAll('#slide-' + currentSlide + ' .notes');
1142 if(!!notes[0]) {
1143 bc.postMessage(notes[0].innerHTML);
1144 } else {
1145 bc.postMessage("");
1146 }
1147
1148 }
1149
1150 if (currentSlide == 1 || currentSlide == numSlides-1) {
1151 confetti.start();
1152 } else {
1153 confetti.stop();
1154 }
1155 }
1156
1157
1158 function activateFriends(n, noshake) {
1159 f = document.querySelectorAll('.code-friends' + n);
1160
1161 for (fr = 0; fr< f.length; fr++) {
1162 if (!noshake) {
1163 f[fr].classList.add("shake-constant");
1164 f[fr].classList.add("shake-slow");
1165 }
1166 f[fr].classList.add("on");
1167 }
1168 }
1169
1170 function deactivateFriends(n) {
1171 f = document.querySelectorAll('.code-friends' + n);
1172 for (fr = 0; fr< f.length; fr++) {
1173 f[fr].classList.remove("shake-constant");
1174 f[fr].classList.remove("shake-slow");
1175 f[fr].classList.remove("on");
1176 }
1177 }
1178
1179 function activatePromise(n) {
1180 p = document.querySelectorAll('.promise' + n);
1181 for (fr = 0; fr< p.length; fr++) {
1182 p[fr].classList.add('on');
1183 }
1184 }
1185
1186 function deactivatePromise(n) {
1187 p = document.querySelectorAll('.promise' + n);
1188 for (fr = 0; fr< p.length; fr++) {
1189 p[fr].classList.remove('on');
1190 }
1191 }
1192
1193 cloudGuyQuotes = [
1194 "Welcome to the talk!",
1195 "I hope we'll have fun exploring functional programming together!",
1196 "But while we wait let's share interesting facts and anecdotes :)",
1197 "If you can be disgruntled, can you also be gruntled?",
1198 "0.9 recurring is exactly equal to one if you think about it.",
1199 "In English, the word \"ghoti\" can be pronounced as \"fish\".",
1200 "Your credit card details are somewhere in Pi.",
1201 "It takes 366 people to make sure two of them share a birthday, but only 23 people to ensure a 50% chance.",
1202 "1729 is the best number (I checked them all).",
1203 "The secret to strengh is to eat only a banana for breakfast.",
1204 "All of Back to the Future happened in the past.",
1205 "Everything is in the future.",
1206 "We owe functional and procedural programming to the pre-Socratics, Heraclitus and Anaximander respectively.",
1207 "Preservative has a very different meaning in many European languages.",
1208 "There are more stars in our galaxy than there are atoms in The Universe.",
1209 "One of these facts is a complete fabrication. Oh no, what if it's this one?"
1210 ];
1211
1212 cgq = document.getElementById("cgquotes");
1213 numQuotes = cloudGuyQuotes.length;
1214 currentQuote = 0;
1215 setInterval(function(){
1216 if (currentQuote == 0 || currentQuote == 1) {
1217 currentQuote++;
1218 } else {
1219 currentQuote = Math.floor(Math.random()*(cloudGuyQuotes.length - 3)) + 3;
1220 }
1221
1222 cgq.innerText = cloudGuyQuotes[currentQuote];
1223 }, 10000);
1224
1225 helperQuotes = [
1226 "Let's go!",
1227 "Here to help!",
1228 "Aw yeah!",
1229 "Power!",
1230 "Teamwork!",
1231 "Let's code!",
1232 "Friendship!",
1233 "Bananas",
1234 "Bagels",
1235 "Coffee"
1236 ];
1237
1238 function animateCSS(element, animationName, callback) {
1239 const node = element;
1240 node.classList.add('animated', animationName)
1241
1242 function handleAnimationEnd() {
1243 node.classList.remove('animated', animationName)
1244 node.removeEventListener('animationend', handleAnimationEnd)
1245 node.style.opacity = '0';
1246 if (typeof callback === 'function') callback()
1247 }
1248
1249 node.addEventListener('animationend', handleAnimationEnd)
1250 }
1251
1252 helpers = document.querySelectorAll("#cloudguys span");
1253 setInterval(function(){
1254 quote = Math.floor(Math.random()*(helperQuotes.length));
1255 helper = Math.floor(Math.random()*(helpers.length));
1256 helpers[helper].innerText = helperQuotes[quote];
1257 animateCSS(helpers[helper], 'fadeOutUp');
1258 }, 1000);
1259
1260
1261 angeries = document.querySelectorAll(".angereyWrap");
1262 usedPositions = [];
1263 shakes = ["shake", "shake-hard", "shake-slow", "shake-little", "shake-horizontal", "shake-vertical", "shake-rotate", "shake-opacity", "shake-crazy", "shake-chunk"];
1264 for (a = 0; a < angeries.length; a++) {
1265 randomness = (Math.round(Math.random() * 10) + 1) * -10;
1266 angeries[a].style.top = ((a > 4) ? (-180-randomness) : (180 + randomness)) + "%" ;
1267 thing = ((Math.floor(Math.random() * 10) + 1) * 9) + "%";
1268
1269 while (usedPositions.includes(thing)) {
1270 thing = ((Math.floor(Math.random() * 10) + 1) * 9) + "%";
1271 }
1272
1273 angeries[a].style.left = thing;
1274 angeries[a].classList.add("shake-constant");
1275 angeries[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]);
1276 usedPositions.push(thing);
1277 }
1278
1279 thonkings = document.querySelectorAll(".thonkingWrap");
1280 shakes = ["shake-slow", "shake-little", "shake-opacity"];
1281 usedPositions = [];
1282 for (a = 0; a < thonkings.length; a++) {
1283 thing = Math.floor(Math.random() * 4) + 1;
1284 randomnessX = -(Math.round(Math.random() * 100) + 1);
1285 randomnessY = -(Math.round(Math.random() * 50) + 1);
1286
1287 while (usedPositions.includes(thing)) {
1288 thing = Math.floor(Math.random() * 4) + 1;
1289 }
1290
1291 switch(thing) {
1292 case 1:
1293 thonkings[a].style.left = randomnessX + 'px';
1294 thonkings[a].style.top = randomnessY + 'px';
1295 thonkings[a].children[0].style.transform = 'rotate(-40deg) scale(1, 1.5)'
1296 thonkings[a].classList.add("shake-constant");
1297 thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]);
1298 break;
1299 case 2:
1300 thonkings[a].style.right = randomnessX + 'px';
1301 thonkings[a].style.top = randomnessY + 'px';
1302 thonkings[a].classList.add("shake-constant");
1303 thonkings[a].children[0].style.transform = 'rotate(40deg) scale(1, 1.5)'
1304 thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]);
1305 break;
1306 case 3:
1307 thonkings[a].style.right = randomnessX + 'px';
1308 thonkings[a].style.bottom = randomnessY + 'px';
1309 thonkings[a].classList.add("shake-constant");
1310 thonkings[a].children[0].style.transform = 'rotate(-215deg) scale(1, 1.5)'
1311 thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]);
1312 break;
1313 case 4:
1314 thonkings[a].style.left = randomnessX + 'px';
1315 thonkings[a].style.bottom = randomnessY + 'px';
1316 thonkings[a].classList.add("shake-constant");
1317 thonkings[a].children[0].style.transform = 'rotate(215deg) scale(1, 1.5)'
1318 thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]);
1319 break;
1320 }
1321
1322
1323 usedPositions.push(thing);
1324 }
1325
1326 </script>
1327 </html>