Skip to content

Commit

Permalink
deploy: ad0fc0f
Browse files Browse the repository at this point in the history
  • Loading branch information
noelwelsh committed Oct 29, 2024
1 parent 388dbe3 commit f514709
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 42 deletions.
10 changes: 5 additions & 5 deletions animation/easing.html
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ <h1 id="easing-functions" class="title">Easing Functions</h1>
.</span><span class="identifier">withRender</span><span>(</span><span class="identifier">r</span><span> =&gt; </span><span class="type-name">Image</span><span>.</span><span class="identifier">circle</span><span>(</span><span class="identifier">r</span><span>))
</span><span class="comment">// bubble: Reactor[Double] = Reactor(
// initial = 0.0,
// onMouseClickHandler = doodle.reactor.Reactor$$$Lambda$13993/0x0000000103850040@13c381c8,
// onMouseMoveHandler = doodle.reactor.Reactor$$$Lambda$13994/0x0000000103850840@75f073e0,
// onTickHandler = doodle.reactor.Reactor$$$Lambda$13998/0x0000000103854040@65377bd1,
// onMouseClickHandler = doodle.reactor.Reactor$$$Lambda$14055/0x000000010384f840@2f4b3eec,
// onMouseMoveHandler = doodle.reactor.Reactor$$$Lambda$14056/0x0000000103850840@16c8daba,
// onTickHandler = doodle.reactor.Reactor$$$Lambda$14060/0x0000000103853840@444cd291,
// tickRate = 100 milliseconds,
// renderHandler = repl.MdocSession$MdocApp$$Lambda$14000/0x0000000103855840@6f35ba8,
// stopHandler = doodle.reactor.Reactor$$$Lambda$13999/0x0000000103855040@931477f
// renderHandler = repl.MdocSession$MdocApp$$Lambda$14062/0x0000000103855840@40348dd9,
// stopHandler = doodle.reactor.Reactor$$$Lambda$14061/0x0000000103854840@367e676d
// )</span></code></pre>
<div class="flex flex-row justify-between">
<a class="pageNavigation" href="index.html">←Animation and Fireworks</a> <a class="pageNavigation" href="../collections/paths.html">Drawing Paths→</a>
Expand Down
13 changes: 7 additions & 6 deletions expressions/04-types.html
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,13 @@ <h1 id="types" class="title">Types</h1>
</span><span class="comment">// res0: Int = 3</span></code></pre>
<p>A type of <code>Int</code> means if the expression is successfully evaluated, then the value will be one of the approximately 4.2 billion values the computer understands to be integers. (Integers are also known as &quot;whole numbers&quot;.) </p>
<p>We determine the type of an expression without running it. This means that <em>types are a property of expressions</em>. We can give a type to an expression even if, when we evaluate it, the computation fails. This is also why the type doesn&#39;t tell us the specific value the expression evaluates to: we&#39;d have to evaluate the expression to know that.</p>
<p>Types are worked out in a process known as a <em>type-checking</em>, which is part of <em>compilation</em>. Before a Scala program is run, it must be <em>compiled</em>. Compilation checks that a program makes sense. It must be syntactically correct, meaning it must be written according to the rules of Scala. For example <code>(1 + 2)</code> is syntactically correct, but <code>(1 + 2</code> is not because there is no <code>)</code> to match the <code>(</code>.
Here, &quot;syntax&quot; is the term we use for the rules defining the structure of symbols in a programming language.
In English, we are taught that a sentence can be constructed using the rule: subject + verb + object e.g. &quot;the artist paints a cat&quot;.
A programming language might have a different set of rules and symbols to read <code>Artist.paint(cat);</code>.
This is a general example—languages have syntax variations—and it is worth noting that the English sentence is purely descriptive while the general code example is instructing the computer to paint a cat using &#39;Artist&#39;, which could be a library or object. </p>
<p>It must also <em>type check</em>, meaning the types must be correct for the operations we&#39;re trying to do. <code>1 + 2</code> type checks (we are adding integers), but <code>1.toUpperCase</code> does not (there is no concept of upper and lower case for integers.)</p>
<p>Types are worked out in a process known as <em>compilation</em>. Before a Scala program is run, it must be <em>compiled</em>. Compilation checks that a program makes sense. There are two main checks that the compiler performs: it checks <em>syntax</em>, and it checks types.</p>
<p>Syntax is the term we use for the rules defining the structure of symbols in a language.
In English we are taught that a sentence can be constructed using the rule: subject + verb + object. For example, &quot;the artist paints a cat&quot; follows this structure.
A programming language might have a different set of rules and symbols to read <code>Artist.paint(cat)</code>.
This is a general example—languages have syntax variations—and it is worth noting that the English sentence is purely descriptive while the general code example is instructing the computer to paint a cat using <code>Artist</code>, which could be a library or object.
Nonetheless, our Scala programs must be syntactically correct, meaning they must be written according to the rules of Scala. For example <code>(1 + 2)</code> is syntactically correct, but <code>(1 + 2</code> is not because there is no <code>)</code> to match the <code>(</code>. </p>
<p>Scala programs must also <em>type check</em>, meaning the types must be correct for the operations we&#39;re trying to do. <code>1 + 2</code> type checks (we are adding integers), but <code>1.toUpperCase</code> does not (there is no concept of upper and lower case for integers.) Natural languages, like English, have concepts that are similar to type checks. For example, the determiners &quot;less&quot; and &quot;fewer&quot; must agree with the nouns to which they apply. Fewer is used when the noun refers to discrete objects that can be easily counted. For example, &quot;put fewer eggs in the cake&quot;. Less is used when this is not the case. For example, &quot;put less flour in the cake&quot;.</p>
<p>Only programs that successfully compile can be run. We can think of compilation as being analogous to the rules of grammar in writing. The sentence &quot;FaRf fjrmn;l df.fd&quot; is syntactically incorrect in English. The arrangement of letters doesn&#39;t form any words. The sentence &quot;dog flying a here no&quot; is made out of valid words but their arrangement breaks the rules of grammar, which is analogous to the type checks that Scala performs.</p>
<p>It is important to remember that type checking is done before a program runs. If you have used a language like Python or Javascript, which are sometimes called &quot;dynamically typed&quot;, there is no type checking done before a program runs. In a &quot;statically typed&quot; language like Scala the type checking catches some potential errors for us before we run the code. What is sometimes called a type in a dynamically typed language is <em>not</em> a type as we understand it here. Types, for us, exist at the time when a program is compiled, which we will call <em>compile time</em>. At the time when a program runs, which we call <em>run time</em>, we have only values. Values may record some information about the type of the expression that created them. If they do we call these <em>tags</em>, or sometimes <em>boxes</em>. Not all values are tagged or boxed. Avoiding tagging, which is also called <em>type erasure</em>, allows for more efficient programs.</p>
<div class="flex flex-row justify-between">
Expand Down
6 changes: 3 additions & 3 deletions flowers/functions.html
Original file line number Diff line number Diff line change
Expand Up @@ -500,17 +500,17 @@ <h4 id="exercise-function-literals">Exercise: Function Literals</h4>
<div class="solution-body">
<p>The first function is</p>
<pre><code class="nohighlight"><span>(</span><span class="identifier">x</span><span>: </span><span class="type-name">Int</span><span>) =&gt; </span><span class="identifier">x</span><span> * </span><span class="identifier">x</span><span>
</span><span class="comment">// res10: Function1[Int, Int] = repl.MdocSession$MdocApp0$$Lambda$14298/0x00000001038f1040@3bc430ec</span></code></pre>
</span><span class="comment">// res10: Function1[Int, Int] = repl.MdocSession$MdocApp0$$Lambda$14360/0x000000010394c040@4175a36f</span></code></pre>
<p>The second is</p>
<pre><code class="nohighlight"><span>(</span><span class="identifier">c</span><span>: </span><span class="type-name">Color</span><span>) =&gt; </span><span class="identifier">c</span><span>.</span><span class="identifier">spin</span><span>(</span><span class="number-literal">15</span><span>.</span><span class="identifier">degrees</span><span>)
</span><span class="comment">// res11: Function1[Color, HSLA] = repl.MdocSession$MdocApp0$$Lambda$14299/0x00000001038f0840@2724b87d</span></code></pre>
</span><span class="comment">// res11: Function1[Color, HSLA] = repl.MdocSession$MdocApp0$$Lambda$14361/0x000000010394d040@4b39f01b</span></code></pre>
<p>The third is</p>
<pre><code class="nohighlight"><span>(</span><span class="identifier">image</span><span>: </span><span class="type-name">Image</span><span>) =&gt;
</span><span class="identifier">image</span><span>.</span><span class="identifier">beside</span><span>(</span><span class="identifier">image</span><span>.</span><span class="identifier">rotate</span><span>(</span><span class="number-literal">90</span><span>.</span><span class="identifier">degrees</span><span>))
.</span><span class="identifier">beside</span><span>(</span><span class="identifier">image</span><span>.</span><span class="identifier">rotate</span><span>(</span><span class="number-literal">180</span><span>.</span><span class="identifier">degrees</span><span>))
.</span><span class="identifier">beside</span><span>(</span><span class="identifier">image</span><span>.</span><span class="identifier">rotate</span><span>(</span><span class="number-literal">270</span><span>.</span><span class="identifier">degrees</span><span>))
.</span><span class="identifier">beside</span><span>(</span><span class="identifier">image</span><span>.</span><span class="identifier">rotate</span><span>(</span><span class="number-literal">360</span><span>.</span><span class="identifier">degrees</span><span>))
</span><span class="comment">// res12: Function1[Image, Image] = repl.MdocSession$MdocApp0$$Lambda$14300/0x00000001038e7840@78caae5e</span></code></pre>
</span><span class="comment">// res12: Function1[Image, Image] = repl.MdocSession$MdocApp0$$Lambda$14362/0x000000010394d840@485e600b</span></code></pre>
</div>
</div>
<div class="exercise">
Expand Down
16 changes: 8 additions & 8 deletions generative/flatmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -501,15 +501,15 @@ <h4 id="randomness-and-randomness" class="section">Randomness and Randomness {-}
// c = FlatMapped(
// c = FlatMapped(
// c = Suspend(a = RDouble),
// f = cats.free.Free$$Lambda$14378/0x00000001038e7840@6d827cf2
// f = cats.free.Free$$Lambda$14440/0x000000010396b840@3f8e6b0c
// ),
// f = cats.free.Free$$Lambda$14378/0x00000001038e7840@202091b9
// f = cats.free.Free$$Lambda$14440/0x000000010396b840@10ddc9cb
// ),
// f = cats.free.Free$$Lambda$14378/0x00000001038e7840@4479f03a
// f = cats.free.Free$$Lambda$14440/0x000000010396b840@13735c19
// ),
// f = repl.MdocSession$MdocApp5$$Lambda$14395/0x000000010397d040@4cc7933
// f = repl.MdocSession$MdocApp5$$Lambda$14457/0x000000010397d040@5ea1278c
// ),
// f = repl.MdocSession$MdocApp5$$Lambda$14398/0x000000010397a840@7a57e0b4
// f = repl.MdocSession$MdocApp5$$Lambda$14460/0x000000010397b040@123bf15f
// )</span></code></pre>
<p>which makes it clearer that we&#39;re generating three different circles.
&lt;/div&gt;</p>
Expand All @@ -530,17 +530,17 @@ <h4 id="colored-boxes" class="section">Colored Boxes {-}</h4>
</span><span class="type-name">Random</span><span>.</span><span class="identifier">double</span><span>.</span><span class="identifier">map</span><span>(</span><span class="identifier">x</span><span> =&gt; </span><span class="identifier">x</span><span>.</span><span class="identifier">turns</span><span>)
</span><span class="comment">// randomAngle: Free[[A &gt;: Nothing &lt;: Any] =&gt; RandomOp[A], Angle] = FlatMapped(
// c = Suspend(a = RDouble),
// f = cats.free.Free$$Lambda$14378/0x00000001038e7840@5ede1b1e
// f = cats.free.Free$$Lambda$14440/0x000000010396b840@6cd6f142
// )
</span><span>
</span><span class="keyword">val</span><span> </span><span class="identifier">randomColor</span><span>: </span><span class="type-name">Random</span><span>[</span><span class="type-name">Color</span><span>] =
</span><span class="identifier">randomAngle</span><span>.</span><span class="identifier">map</span><span>(</span><span class="identifier">hue</span><span> =&gt; </span><span class="type-name">Color</span><span>.</span><span class="identifier">hsl</span><span>(</span><span class="identifier">hue</span><span>, </span><span class="number-literal">0.7</span><span>, </span><span class="number-literal">0.7</span><span>))
</span><span class="comment">// randomColor: Free[[A &gt;: Nothing &lt;: Any] =&gt; RandomOp[A], Color] = FlatMapped(
// c = FlatMapped(
// c = Suspend(a = RDouble),
// f = cats.free.Free$$Lambda$14378/0x00000001038e7840@5ede1b1e
// f = cats.free.Free$$Lambda$14440/0x000000010396b840@6cd6f142
// ),
// f = cats.free.Free$$Lambda$14378/0x00000001038e7840@55adc8df
// f = cats.free.Free$$Lambda$14440/0x000000010396b840@654cfb55
// )
</span><span>
</span><span class="keyword">def</span><span> </span><span class="declaration-name">coloredRectangle</span><span>(</span><span class="identifier">color</span><span>: </span><span class="type-name">Color</span><span>): </span><span class="type-name">Image</span><span> =
Expand Down
16 changes: 8 additions & 8 deletions generative/generative.html
Original file line number Diff line number Diff line change
Expand Up @@ -345,29 +345,29 @@ <h1 id="generative-art" class="title">Generative Art</h1>
</tbody>
</table>
<pre><code class="nohighlight"><span class="identifier">math</span><span>.</span><span class="identifier">random</span><span>
</span><span class="comment">// res0: Double = 0.7321923153975315
</span><span class="comment">// res0: Double = 0.33229621098059825
</span><span class="identifier">math</span><span>.</span><span class="identifier">random</span><span>
</span><span class="comment">// res1: Double = 0.8782656665714154</span></code></pre>
</span><span class="comment">// res1: Double = 0.3717700480777597</span></code></pre>
<p>Given <code>math.random</code> we could produce a method that returns a random <code>Angle</code> like so.</p>
<pre><code class="nohighlight"><span class="keyword">def</span><span> </span><span class="declaration-name">randomAngle</span><span>: </span><span class="type-name">Angle</span><span> =
</span><span class="identifier">math</span><span>.</span><span class="identifier">random</span><span>.</span><span class="identifier">turns</span><span>

</span><span class="identifier">randomAngle</span><span>
</span><span class="comment">// res2: Angle = Angle(0.414285054299259)
</span><span class="comment">// res2: Angle = Angle(1.0125427600285282)
</span><span class="identifier">randomAngle</span><span>
</span><span class="comment">// res3: Angle = Angle(4.071737771399261)</span></code></pre>
</span><span class="comment">// res3: Angle = Angle(5.323903050936741)</span></code></pre>
<p>Why might we not want to do this? What principle does this break?</p>
<p>&lt;div class=&quot;solution&quot;&gt;
Generating random numbers in this way breaks substitution. Remember substitution says wherever we see an expression we should be able to substitute the value it evaluates to without changing the meaning of the program. Concretely, this means</p>
<pre><code class="nohighlight"><span class="keyword">val</span><span> </span><span class="identifier">result1</span><span> = </span><span class="identifier">randomAngle</span><span>
</span><span class="comment">// result1: Angle = Angle(1.2436173182622117)
</span><span class="comment">// result1: Angle = Angle(1.4044994925436105)
</span><span class="keyword">val</span><span> </span><span class="identifier">result2</span><span> = </span><span class="identifier">randomAngle</span><span>
</span><span class="comment">// result2: Angle = Angle(4.4462845621989215)</span></code></pre>
</span><span class="comment">// result2: Angle = Angle(5.905359748101744)</span></code></pre>
<p>and </p>
<pre><code class="nohighlight"><span class="keyword">val</span><span> </span><span class="identifier">result1</span><span> = </span><span class="identifier">randomAngle</span><span>
</span><span class="comment">// result1: Angle = Angle(1.3486939806513423)
</span><span class="comment">// result1: Angle = Angle(1.7533247680916995)
</span><span class="keyword">val</span><span> </span><span class="identifier">result2</span><span> = </span><span class="identifier">result1</span><span>
</span><span class="comment">// result2: Angle = Angle(1.3486939806513423)</span></code></pre>
</span><span class="comment">// result2: Angle = Angle(1.7533247680916995)</span></code></pre>
<p>should be the same program and clearly they are not.
&lt;/div&gt;</p>
<p>What should we do? Suffer the slings and arrows of outrageous computational models, or take arms against a sea of side-effects, and by opposing end them! There&#39;s really only one choice.</p>
Expand Down
6 changes: 3 additions & 3 deletions generative/random.html
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ <h1 id="randomness-without-effect" class="title">Randomness without Effect</h1>
// )</span></code></pre>
<p>No random numbers are actually created until we call the <code>run</code> method.</p>
<pre><code class="nohighlight"><span class="identifier">randomDouble</span><span>.</span><span class="identifier">run</span><span>
</span><span class="comment">// res0: Double = 0.5642217044601665</span></code></pre>
</span><span class="comment">// res0: Double = 0.8099168768899584</span></code></pre>
<p>The type <code>Random[Double]</code> indicates we have something that will produce a random <code>Double</code> when we <code>run</code> it. Just like with <code>Image</code> and <code>draw</code>, substitution holds with <code>Random</code> up until the point we call <code>run</code>.</p>
<p>Table Table generative:random shows some of the ways to construct <code>Random</code> values.</p>
<hr>
Expand Down Expand Up @@ -368,9 +368,9 @@ <h3 id="composing-random" class="section">Composing Random</h3>
</span><span class="type-name">Random</span><span>.</span><span class="identifier">double</span><span>.</span><span class="identifier">map</span><span>(</span><span class="identifier">x</span><span> =&gt; </span><span class="identifier">x</span><span>.</span><span class="identifier">turns</span><span>)</span></code></pre>
<p>When we <code>run</code> <code>RandomAngle</code> we can generate randomly created <code>Angle</code></p>
<pre><code class="nohighlight"><span class="identifier">randomAngle</span><span>.</span><span class="identifier">run</span><span>
</span><span class="comment">// res1: Angle = Angle(3.9249264337729093)
</span><span class="comment">// res1: Angle = Angle(3.7411920899876865)
</span><span class="identifier">randomAngle</span><span>.</span><span class="identifier">run</span><span>
</span><span class="comment">// res2: Angle = Angle(4.845887994207942)</span></code></pre>
</span><span class="comment">// res2: Angle = Angle(4.067171156042168)</span></code></pre>

<h3 id="exercises" class="section">Exercises {-}</h3>

Expand Down
Loading

0 comments on commit f514709

Please sign in to comment.