From: Cameron Ball Date: Sun, 15 Mar 2020 14:38:48 +0000 (+0800) Subject: Expand promises section further X-Git-Url: http://cameron1729.xyz/?p=fp-lecture.git;a=commitdiff_plain;h=467407bf8bd181ad0f2d6b788653ca277cc46923 Expand promises section further --- diff --git a/img/thonking.gif b/img/thonking.gif new file mode 100644 index 0000000..dcb74bc Binary files /dev/null and b/img/thonking.gif differ diff --git a/img/thonking.mp4 b/img/thonking.mp4 new file mode 100644 index 0000000..1496f68 Binary files /dev/null and b/img/thonking.mp4 differ diff --git a/img/wip/cloudguy.xcf b/img/wip/cloudguy.xcf new file mode 100644 index 0000000..7f6999e Binary files /dev/null and b/img/wip/cloudguy.xcf differ diff --git a/index.html b/index.html index d3d17d0..803851f 100644 --- a/index.html +++ b/index.html @@ -21,6 +21,7 @@ image-rendering: crisp-edges; background-position-x: -33px; background-blend-mode: multiply; + font-size: 22px; } .angereyWrap { @@ -33,6 +34,10 @@ transform: rotate(90deg) scale(1, 1.5); } + .thonkingWrap { + position: absolute; + } + .hljs { font-family: 'Press Start 2P', cursive; text-shadow: 2px 2px 0 #000; @@ -40,7 +45,7 @@ overflow: hidden; } - h1 { + h1, h2 { text-shadow: 2px 2px 0 #000; } @@ -52,10 +57,6 @@ } .slide { - display: flex; - justify-content: center; - align-items: center; - flex-wrap: wrap; margin: 40px; box-shadow: 10px 10px 0 #00000069; padding: 20px; @@ -63,6 +64,13 @@ background: #6187a8e3; } + .flexotron { + display: flex; + justify-content: center; + align-items: center; + flex-wrap: wrap; + } + #slide-0 #cloudguy { height: 100%; display: flex; @@ -81,6 +89,10 @@ width: 40%; } + li { + list-style: square; + } + img { image-rendering: optimizeSpeed; /* STOP SMOOTHING, GIVE ME SPEED */ image-rendering: -moz-crisp-edges; /* Firefox */ @@ -91,6 +103,10 @@ -ms-interpolation-mode: nearest-neighbor; /* IE8+ */ } + .fig { + text-align: center; + } + #slide-1 .shake-constant, .crazy, .span { display: inline-block; } @@ -102,12 +118,15 @@ #slide-1 #rant { padding: 20px; height: 80%; + font-size: 14px; } .textbox { + text-align: center; box-shadow: 10px 10px 0 #00000069; border: 2px solid black; background-color: #3f6382; + padding: 5px; } .hljs { @@ -119,14 +138,21 @@ display: none; } - span[class^="code-friends"].on { + span[class^="code-friends"].on, span[class^="code-friends"].on > span { color: #f98012 !important; } - span[class^="code-friends"].on > span { + span[class^="promise"].on, span[class^="promise"].on > span, span[class^="promise"].on > span > span, span[class^="promise"].on > span > span > span { color: #f98012 !important; } + .highlight { + color: #f98012; + } + + li { + margin-top: 10px; + } @@ -140,7 +166,7 @@
=<< What's up with that Title? >>=
-
+
@@ -151,7 +177,7 @@
-
Fig 1.1 What most people thing a typical functional programming advocate looks like
+
Fig 1.1 What most people thing a typical functional programming advocate looks like
@@ -167,7 +193,7 @@
=<< Promises >>=
-
+
>:(
>:(
@@ -181,34 +207,127 @@
>:(
Promises do not make things asynchronous!
-
=<< Promises >>=
-

JavaScript

-
Promise.resolve("We start here")
+            

What problem to promises solve?

+
+
    +
  • Asynchronous request management
  • +
  • Error handling for computations which may fail
  • +
  • Probably more too
  • +
+
+

What are promises then?

+
+
    +
  • A value (which may or may not be available yet) wrapped in some additional context
  • +
  • Functions to instantiate the wrapper (Promise.resolve, Promise.reject) +
  • A rule for composing these wrapped values (then)
  • +
+
+
+
+
+
=<< Promises >>=
+
+

Terminology/syntax recap

+

JavaScript arrow functions

+
+
x => x + 1;
+
+
+ is equivalent to: +
+
+
function(x) {
+    return x+1;
+}
+
+

Composition

+
+
A method of combining two things of the same type in to another thing that is also of the same type
+
+
+
+
+
=<< Promises >>=
+
+

Promise composition

+
+
The `then` method of a promise accepts a function as an argument. This function takes a value and returns a new promise. The result of `then` is now a new promise that combines the previous two using the "composition rule" given to `then`.
+
+
+
Promise.resolve("Some value").then(x => Promise.resolve(x + " and another value")).then(x => Promise.resolve(x + "and another one!"));
+
+
+
There are three promises in the above snippet. One, two, three.
+
+
+
Promise.resolve("Some value").then(x => Promise.reject("It's all gone wrong")).then(x => Promise.resolve(x + "and another one!"));
+
+
+
The above snippet still results in a promise, but it is a "failed" one. Any subsequent `then` after the `reject` has no effect.
+
+
+
+
+
=<< Promises >>=
+
+

OK great but who cares?

+
+
    +
  • We are declaring how we want promises to compose
  • +
  • + The order that the promises actually resolve their values in is not important +
    • In other words, we don't have to worry about timing anymore
    +
  • +
  • We can declare a failure anywhere in the chain
  • +
+
+
+
+
+
=<< Promises >>=
+
+
+ +
atemporality?
+
declaritive?
+
functions?
+
composition?
+
+
+
+
+
=<< Promises >>=
+
+

Woah!

+

JavaScript

+
Promise.resolve("We start here")
     .then(x => Promise.resolve(x + " then get here"))
     .then(x => Promise.resolve(x + " and finally here!"))
-

Haskell

+

Haskell

Right("We start here")
-            >>= (\x -> Right (x ++ " then get here"))
-            >>= (\x -> Right (x ++ " and finally here!"))
+ >>= (\x -> Right (x ++ " then get here")) + >>= (\x -> Right (x ++ " and finally here!"))
-
+
=<< Promises >>=
-

JavaScript

-
Promise.resolve("We start here")
+            

Woah!

+

JavaScript

+
Promise.resolve("We start here")
     .then(x => Promise.resolve(x + " then get here"))
     .then(x => Promise.reject("Oh noes!"))
     .then(x => Promise.resolve(x + " and finally here!"))
-

Haskell

+

Haskell

Right("We start here")
-            >>= (\x -> Right (x ++ " then get here"))
-            >>= (\x -> Left ("Oh noes!"))
-            >>= (\x -> Right (x ++ " and finally here!"))
+ >>= (\x -> Right (x ++ " then get here")) + >>= (\x -> Left ("Oh noes!")) + >>= (\x -> Right (x ++ " and finally here!"))
@@ -275,8 +394,10 @@ document.getElementById('slide-' + currentSlide).style.display = 'block'; } + console.log(currentSlide); } + function activateFriends(n) { f = document.querySelectorAll('.code-friends' + n); @@ -296,6 +417,20 @@ } } + function activatePromise(n) { + p = document.querySelectorAll('.promise' + n); + for (fr = 0; fr< p.length; fr++) { + p[fr].classList.add('on'); + } + } + + function deactivatePromise(n) { + p = document.querySelectorAll('.promise' + n); + for (fr = 0; fr< p.length; fr++) { + p[fr].classList.remove('on'); + } + } + cloudGuyQuotes = [ "Welcome to the talk!", "I hope we'll have fun exploring functional programming together!", @@ -342,5 +477,52 @@ angeries[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]); usedPositions.push(thing); } + + thonkings = document.querySelectorAll(".thonkingWrap"); + shakes = ["shake-slow", "shake-little", "shake-opacity"]; + usedPositions = []; + for (a = 0; a < thonkings.length; a++) { + thing = Math.floor(Math.random() * 4) + 1; + randomnessX = -(Math.round(Math.random() * 50) + 1); + randomnessY = -(Math.round(Math.random() * 50) + 1); + + while (usedPositions.includes(thing)) { + thing = Math.floor(Math.random() * 4) + 1; + } + + switch(thing) { + case 1: + thonkings[a].style.left = randomnessX + 'px'; + thonkings[a].style.top = randomnessY + 'px'; + thonkings[a].children[0].style.transform = 'rotate(-45deg) scale(1, 1.5)' + thonkings[a].classList.add("shake-constant"); + thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]); + break; + case 2: + thonkings[a].style.right = randomnessX + 'px'; + thonkings[a].style.top = randomnessY + 'px'; + thonkings[a].classList.add("shake-constant"); + thonkings[a].children[0].style.transform = 'rotate(45deg) scale(1, 1.5)' + thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]); + break; + case 3: + thonkings[a].style.right = randomnessX + 'px'; + thonkings[a].style.bottom = randomnessY + 'px'; + thonkings[a].classList.add("shake-constant"); + thonkings[a].children[0].style.transform = 'rotate(-225deg) scale(1, 1.5)' + thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]); + break; + case 4: + thonkings[a].style.left = randomnessX + 'px'; + thonkings[a].style.bottom = randomnessY + 'px'; + thonkings[a].classList.add("shake-constant"); + thonkings[a].children[0].style.transform = 'rotate(225deg) scale(1, 1.5)' + thonkings[a].classList.add(shakes[Math.floor(Math.random()*shakes.length)]); + break; + } + + + usedPositions.push(thing); + }