<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Graham Enos</title>
    <subtitle>math, programming, statistics, and data</subtitle>
    <link rel="self" type="application/atom+xml" href="https://grahamenos.com/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://grahamenos.com"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-04-29T00:00:00+00:00</updated>
    <id>https://grahamenos.com/atom.xml</id>
    <entry xml:lang="en">
        <title>About</title>
        <published>2026-04-29T00:00:00+00:00</published>
        <updated>2026-04-29T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/about/"/>
        <id>https://grahamenos.com/about/</id>
        
        <content type="html" xml:base="https://grahamenos.com/about/">&lt;p&gt;While working as an Applied Research Mathematician for the US Department of Defense, I received my Ph.D. in Applied Mathematics from &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;math.uncc.edu&quot;&gt;UNC Charlotte’s Math Department&lt;&#x2F;a&gt; in the Spring of 2013.
After that, I worked as a Senior Data Scientist for &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.quantifind.com&quot;&gt;Quantifind, Inc&lt;&#x2F;a&gt;.
Then I was a Lead Research Scientist, Quantum Advantage for &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.rigetti.com&quot;&gt;Rigetti Computing&lt;&#x2F;a&gt;, before starting my current role as Chief Scientist at &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;strangeworks.com&#x2F;&quot;&gt;Strangeworks&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;On this blog I explore those topics I never had the time to when I was working on my dissertation; essentially anything mathematical, computational, statistical, etc. that I happen to find interesting.
For more about me, you can view my &lt;a href=&quot;&#x2F;pdfs&#x2F;genos_cv.pdf&quot;&gt;CV&lt;&#x2F;a&gt;, my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&quot;&gt;GitHub&lt;&#x2F;a&gt;, my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;gitlab.com&#x2F;grenos&quot;&gt;GitLab&lt;&#x2F;a&gt;, or my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;mathstodon.xyz&#x2F;@grahamenos&quot;&gt;Mathstodon&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Obligatory notice: my views do not necessarily reflect those of anyone kind enough to employ me.&lt;&#x2F;p&gt;
&lt;p&gt;(Unfortunately) obligatory notice: Nothing here was written with or by a large language model (LLM).
Quoting the introduction of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;fset.common-lisp.dev&#x2F;Modern-CL&#x2F;Top_html&#x2F;index.html&quot;&gt;Modern Common Lisp with FSet&lt;&#x2F;a&gt;,&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;This document contains no LLM-generated text — zero, zip, nada.
(Yes, I do use em-dashes and semicolons; yes, I have written every one myself.)&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Tempest in a Teacup</title>
        <published>2026-04-14T00:00:00+00:00</published>
        <updated>2026-04-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/trinculo/"/>
        <id>https://grahamenos.com/trinculo/</id>
        
        <content type="html" xml:base="https://grahamenos.com/trinculo/">&lt;h1 id=&quot;i-shall-laugh-myself-to-death-at-this-puppy-headed-monster&quot;&gt;I shall laugh myself to death at this puppy-headed monster&lt;&#x2F;h1&gt;
&lt;p&gt;Last year, Matt Keeter put out the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.mattkeeter.com&#x2F;projects&#x2F;prospero&#x2F;&quot;&gt;Prospero challenge&lt;&#x2F;a&gt;.
It’s a really great playground for learning about programming languages,
compilation, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Constructive_solid_geometry&quot;&gt;constructive solid
geometry&lt;&#x2F;a&gt;, and more!&lt;&#x2F;p&gt;
&lt;p&gt;I’m a bit late to the game, but in my “spare time,” I’ve been working on my own
attempt.
You can see the code &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;codeberg.org&#x2F;genos&#x2F;trinculo&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So far, my approach has been pretty similar to others: after some
canonicalization and a few simplifying&#x2F;optimizing passes (e.g. eliding unused
expressions), parallelize over disjoint chunks of the image, and use SIMD
instructions to compute multiple pixels in a chunk at one time.&lt;&#x2F;p&gt;
&lt;p&gt;I’ve been tempted to push further, using intervals &amp;amp; quad-trees like in Matt
Keeter’s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=UxGxsGnbyJ4&quot;&gt;very interesting talk&lt;&#x2F;a&gt;,
but haven’t managed to pull it off yet.
In the meantime, though, I’d like to highlight how important testing,
especially property-based testing, has been.&lt;&#x2F;p&gt;
&lt;p&gt;Given the nature of the challenge, I’ve been leaning on some &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;~asampson&#x2F;blog&#x2F;turnt.html&quot;&gt;compiler
wisdom&lt;&#x2F;a&gt; and reached for
some snapshot testing.
Since I’ve been working in Rust, I’m using &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;insta.rs&#x2F;&quot;&gt;&lt;code&gt;insta&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
Being able to assert that my output matches the expected one byte-for-byte is
pretty usefule.&lt;&#x2F;p&gt;
&lt;p&gt;Beyond that, I’ve also been using property-based testing.
Instead of my usual go-to
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;altsysrq.github.io&#x2F;proptest-book&#x2F;intro.html&quot;&gt;&lt;code&gt;proptest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, I’ve been
trying out the relatively new
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;chaos_theory&#x2F;latest&#x2F;chaos_theory&#x2F;index.html&quot;&gt;&lt;code&gt;chaos_theory&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
Besides being less macro-heavy, I think its design feels closer to
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hypothesis.works&#x2F;&quot;&gt;&lt;code&gt;hypothesis&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; from the Python world.
Anecdotally, its tests ran faster for me, too!
Having these testing tools have proven invaluable in approaching this
challenge.
I even found a bug in optimization code from one of the other entries!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Parallel Means</title>
        <published>2025-07-25T00:00:00+00:00</published>
        <updated>2025-07-25T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/means/"/>
        <id>https://grahamenos.com/means/</id>
        
        <content type="html" xml:base="https://grahamenos.com/means/">&lt;h1 id=&quot;don-t-be-mean&quot;&gt;Don’t be mean&lt;&#x2F;h1&gt;
&lt;p&gt;I got curious about parallelism—in Rust, but the same ideas probably apply
elsewhere—comparing using multiple CPUs via
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;rayon&#x2F;latest&#x2F;rayon&#x2F;&quot;&gt;&lt;code&gt;rayon&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and SIMD via
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;simd&#x2F;index.html&quot;&gt;&lt;code&gt;portable_simd&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
I suspected that which one would be the fastest would be highly dependent on
multiple factors, like the amount and distribution of the data involved and the
complexity of desired computation.&lt;&#x2F;p&gt;
&lt;p&gt;To investigate, I wrote some code to calculate the mean of a collection of
32-bit floating point numbers in four different ways.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
The first is a sequential version of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Algorithms_for_calculating_variance#Welford&amp;#x27;s_online_algorithm&quot;&gt;Welford’s online
algorithm&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; seq&lt;&#x2F;span&gt;&lt;span&gt;(xs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;f32&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span&gt; n,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; mut&lt;&#x2F;span&gt;&lt;span&gt; mu)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0.0&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; xs {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1.0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        mu&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span&gt; (x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; mu)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; n;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    mu&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, we have a &lt;code&gt;rayon&lt;&#x2F;code&gt;-ified version of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Algorithms_for_calculating_variance#Parallel_algorithm&quot;&gt;Chan et. al.’s parallel
generalization&lt;&#x2F;a&gt;.
It first folds the same sequential version over each chunk in parallel, and
then merges the chunks together.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; par&lt;&#x2F;span&gt;&lt;span&gt;(xs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;f32&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f32&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    xs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;into_par_iter&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;fold&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            ||&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0.0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            |&lt;&#x2F;span&gt;&lt;span&gt;(n_old, mu_old), x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; n_old&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1.0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span&gt; mu&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; mu_old&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; (x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; mu_old)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; n;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                (n, mu)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            ||&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0.0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            |&lt;&#x2F;span&gt;&lt;span&gt;(n_a, mu_a), (n_b, mu_b)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; n_a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; n_b;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                let&lt;&#x2F;span&gt;&lt;span&gt; mu&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; mu_a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; (mu_b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; mu_a)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; n_b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; n;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                (n, mu)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I also developed two SIMD versions of the above, &lt;code&gt;seq_simd&amp;lt;N&amp;gt;&lt;&#x2F;code&gt; and
&lt;code&gt;par_simd&amp;lt;N&amp;gt;&lt;&#x2F;code&gt; for supported lane count &lt;code&gt;N&lt;&#x2F;code&gt;.
These were relatively straightforward to write and test&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; based on their
non-SIMD counterparts.
The only parts that required a little more thought were collecting the &lt;code&gt;N&lt;&#x2F;code&gt;
intermediate values and then “cleaning up” at the end; if the number of values
isn’t a multiple of the lane count &lt;code&gt;N&lt;&#x2F;code&gt;, we need to pick up the last
(potentially up to &lt;code&gt;N&lt;&#x2F;code&gt; - 1) elements.
I won’t include the SIMD code here, as I’ve already copy-pasted a fair amount
of code as it is, but it’s all available on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;means&quot;&gt;my
GitHub&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;do-you-even-bench&quot;&gt;Do you even bench?&lt;&#x2F;h1&gt;
&lt;p&gt;Once these four different implementations were ready, I reached for
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;bheisler.github.io&#x2F;criterion.rs&#x2F;book&#x2F;index.html&quot;&gt;&lt;code&gt;criterion&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to try
and benchmark them.
I measured &lt;code&gt;seq&lt;&#x2F;code&gt;, &lt;code&gt;par&lt;&#x2F;code&gt;, &lt;code&gt;seq_simd&amp;lt;N&amp;gt;&lt;&#x2F;code&gt; and &lt;code&gt;par_simd&amp;lt;N&amp;gt;&lt;&#x2F;code&gt; for &lt;code&gt;N&lt;&#x2F;code&gt; in \( \lbrace 8, 16,
32, 64\rbrace \).
For each power of ten between ten (\(10^1\)) and a billion (\(10^9\)), I
generated a vector of that length of &lt;code&gt;f32&lt;&#x2F;code&gt;s uniformly from the unit interval,
and had &lt;code&gt;criterion&lt;&#x2F;code&gt; measure the time it took each version to calculate the mean
of that vector.&lt;&#x2F;p&gt;
&lt;p&gt;All the usual caveats about this type of benchmarking still apply!
I did the minimal due diligence—no other applications were running on my
laptop, e.g.—but your mileage may vary due to different hardware (in this
case, Apple M3 Max), data, computational load, etc.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;results&quot;&gt;Results&lt;&#x2F;h1&gt;
&lt;p&gt;After running the benchmarks, I &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;means&#x2F;justfile#L20-L56&quot;&gt;collated and plotted the
results&lt;&#x2F;a&gt;.
Here’s the plot of #floats vs. nanoseconds (click on it for the full SVG); note
the log-log scale.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;&#x2F;images&#x2F;means.svg&quot;&gt;&lt;img src=&quot;&#x2F;images&#x2F;means.svg&quot; alt=&quot;Times in nanoseconds, by #floats and function version&quot; &#x2F;&gt;&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To my eye, there’s a couple interesting things going on.
When dealing with a small number of floats, the sequential versions look
faster; indeed, the relative simplicity of &lt;code&gt;seq&lt;&#x2F;code&gt; is the fastest for taking the
mean of ten values.
SIMD starts being useful pretty quickly, however; all four of the &lt;code&gt;seq_simd&amp;lt;N&amp;gt;&lt;&#x2F;code&gt;
versions are faster than &lt;code&gt;seq&lt;&#x2F;code&gt; by the time we’re working with a thousand
floats.
Multi-CPU parallelism starts to be useful much later, but eventually wins out.
&lt;code&gt;par&lt;&#x2F;code&gt; beats &lt;code&gt;seq&lt;&#x2F;code&gt; by the time we reach \(10^5\), and each &lt;code&gt;par_simd&amp;lt;N&amp;gt;&lt;&#x2F;code&gt; beats
all four of the &lt;code&gt;seq_simd&amp;lt;N&amp;gt;&lt;&#x2F;code&gt;s by \(10^7\) and beyond.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;fin&quot;&gt;Fin.&lt;&#x2F;h1&gt;
&lt;p&gt;All in all, the usual wisdom applies: benchmark for your particular use-case,
realistic workloads don’t look like benchmarks run in a vacuum, etc.
That said, this was a fun exercise!
The &lt;code&gt;std::simd&lt;&#x2F;code&gt; APIs are rather pleasant to use; I look forward to them being
available on stable instead of only on nightly.
Please check out &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;means&quot;&gt;the code&lt;&#x2F;a&gt;
for more!&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;I used &lt;code&gt;f32&lt;&#x2F;code&gt; instead of &lt;code&gt;f64&lt;&#x2F;code&gt; so as to fit more values in a single SIMD vector. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;I smell a semigroup! &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;With both manual unit testing and
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;proptest-rs.github.io&#x2F;proptest&#x2F;intro.html&quot;&gt;&lt;code&gt;proptest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; of course. &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>APL Pi Day</title>
        <published>2025-03-14T00:00:00+00:00</published>
        <updated>2025-03-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/apl-pi/"/>
        <id>https://grahamenos.com/apl-pi/</id>
        
        <content type="html" xml:base="https://grahamenos.com/apl-pi/">&lt;h1 id=&quot;happy-pi-day&quot;&gt;Happy \( \pi \) Day&lt;&#x2F;h1&gt;
&lt;p&gt;In honor of today’s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Pi_Day&quot;&gt;date&lt;&#x2F;a&gt;, I played
around with different ways of approximating everyone’s favorite constant in
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.dyalog.com&#x2F;&quot;&gt;Dyalog APL&lt;&#x2F;a&gt;.
What follows are translations of a few entries from
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;List_of_formulae_involving_%CF%80&quot;&gt;this&lt;&#x2F;a&gt; fun
Wikipedia list.
I tried to avoid anything that felt like cheating, e.g. using trigonometric
functions or other constants like \( \phi \).&lt;&#x2F;p&gt;
&lt;p&gt;First, we set index-origin to its correct value &lt;code&gt;⎕io←0&lt;&#x2F;code&gt;.
Next up, here’s the familiar “throw darts at the unit square” Monte Carlo
approach (the first line sets our random seed):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    ⎕rl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;←&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1729&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    6 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍴&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+⌿÷≢&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;+&#x2F;×⍨?&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵ 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍴&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;¨&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.144&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.162&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.1458&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Eschewing stochasticity, using Riemann sums for calculating the area of the
unit circle converges faster:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      6 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍴&lt;&#x2F;span&gt;&lt;span&gt;{(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;÷×⍨&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×+⌿&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×⍨&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×⍨&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.5&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;¨&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2.904518326&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.120417032&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.139555467&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141391478&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141572616&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;as does this translation of the first entry in the iterative algorithm from the
Wikipedia list (as of this writing), viz.&lt;&#x2F;p&gt;
&lt;p&gt;\[
a_0 = 1, \quad a_{n + 1} = \left(1 + \frac{1}{2n + 1}\right)a_n, \quad \pi = \lim_{n\to\infty}\frac{a_n^2}{n}
\]&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;     6 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍴&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;÷⍨×⍨×⌿&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+÷&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;¨&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.221088997&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.149456428&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.14237815&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141671194&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141600508&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Leibniz’s summation \(\sum_0^\infty \frac{(-1)^n}{2n+1}\) converges at a similar rate:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      6 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍴&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×+⌿&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;¯1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;∘*÷&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;∘×&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;¨&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.041839619&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.131592904&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.140592654&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141492654&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141582654&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Though as the
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Leibniz_formula_for_%CF%80#Convergence&quot;&gt;corresponding&lt;&#x2F;a&gt;
entry notes, this is still sublinear.
The fastest one I tried is a slight reworking of&lt;&#x2F;p&gt;
&lt;p&gt;\[
\sum_{n=0}^\infty \frac{(-1)^n}{4}\left(\frac{2}{4n+1}+\frac{2}{4n+2}+\frac{1}{4n+3}\right)
\]
This is the first entry from the Wikipedia’s list of infinite series which “are
efficient for calculating arbitrary binary digits of \(\pi\),” and credited
to the book &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;link.springer.com&#x2F;book&#x2F;10.1007&#x2F;978-3-642-56735-3&quot;&gt;Pi
Unleashed&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      15 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍴&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+⌿&lt;&#x2F;span&gt;&lt;span&gt;{((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;÷&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;÷&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+÷&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;×&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;÷&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;¯4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;⍵&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;¨⍳&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;15&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.333333333&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.114285714&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.146356421&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.140678766&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141777944&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141553701&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141601054&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141590807&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141593065&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141592561&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141592675&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141592649&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141592655&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.141592653&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is a better approximation in 15 steps than the others reached in a million!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Adding up uniform variates</title>
        <published>2025-02-27T00:00:00+00:00</published>
        <updated>2025-02-27T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/rge1/"/>
        <id>https://grahamenos.com/rge1/</id>
        
        <content type="html" xml:base="https://grahamenos.com/rge1/">&lt;h1 id=&quot;a-wild-e-appears&quot;&gt;A wild &lt;em&gt;e&lt;&#x2F;em&gt; appears&lt;&#x2F;h1&gt;
&lt;p&gt;Here’s a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;mostlymaths.net&#x2F;2010&#x2F;08&#x2F;and-e-appears-from-nowhere.html&#x2F;&quot;&gt;fun math tidbit&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Select a random number between 0 and 1. Now select another and add it to the
first. Keep doing this, piling on random numbers. How many random numbers, on
average, do you need to make the total greater than 1? Answer: 2.71828….&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;— John Derbyshire, &lt;em&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.johnderbyshire.com&#x2F;Books&#x2F;Prime&#x2F;Blog&#x2F;page.html&quot;&gt;Prime
Obsession&lt;&#x2F;a&gt;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The inimitable &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;mathworld.wolfram.com&#x2F;UniformSumDistribution.html&quot;&gt;MathWorld&lt;&#x2F;a&gt; has more
info on it, and the first link above has a nice proof sketch involving the
standard simplex (modified from Derbyshire’s book), as well as some Clojure code.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;repo-man&quot;&gt;Repo, Man&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve used this problem for a short programming exercise a number of times, and
I really like it.
Besides the fun math involved, it can give a decent understanding of how to get
things done in a language.
For instance, tackling it in Haskell can lead to thinking about monads (or
not), while doing it in a high performance language can make one think about
ways to squeeze out every last drop of performance.
I’ve collected implementations in a number of different languages
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;rge1&quot;&gt;here&lt;&#x2F;a&gt;.
Some highlights and&#x2F;or interesting points:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The Rust version uses &lt;code&gt;rayon&lt;&#x2F;code&gt; for parallelism and a
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Algorithms_for_calculating_variance#Parallel_algorithm&quot;&gt;modification&lt;&#x2F;a&gt;
of Welford’s online algorithm for calculating the mean. This problem has a
lot of room for exploring parallelism, trading memory for time, and more.&lt;&#x2F;li&gt;
&lt;li&gt;The array language versions in BQN and ngn&#x2F;k do an approximation where
they build a large matrix of uniform variates and approximate from there,
rather than relying on loops or recursion.&lt;&#x2F;li&gt;
&lt;li&gt;Writing WebAssembly by hand and calling it from TypeScript is surprisingly
painless (thanks to a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;alisey&#x2F;pcg32&quot;&gt;&lt;code&gt;pcg&lt;&#x2F;code&gt; implementation&lt;&#x2F;a&gt;
I found); what’s more, it’s competitive with the more “low-level” C&#x2F;C++ and their ilk.&lt;&#x2F;li&gt;
&lt;li&gt;Some things are much easier to package in a &lt;code&gt;nix&lt;&#x2F;code&gt; flake than others.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Different Implementations of a Tiny Programming Language</title>
        <published>2024-12-11T00:00:00+00:00</published>
        <updated>2024-12-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/random-art-impls/"/>
        <id>https://grahamenos.com/random-art-impls/</id>
        
        <content type="html" xml:base="https://grahamenos.com/random-art-impls/">&lt;h1 id=&quot;random-art-revisited&quot;&gt;Random Art, Revisited&lt;&#x2F;h1&gt;
&lt;p&gt;In &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;random-art&#x2F;&quot;&gt;a previous article&lt;&#x2F;a&gt;, we played around
with implementing a tiny programming language for generating random art.
In that article and the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;ra&quot;&gt;accompanying
code&lt;&#x2F;a&gt;, we implemented a trick
I learned from &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;users.rust-lang.org&#x2F;t&#x2F;is-there-a-better-way-to-represent-an-abstract-syntax-tree&#x2F;9549&#x2F;4&quot;&gt;this
post&lt;&#x2F;a&gt;,
specifically &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;matklad.github.io&#x2F;&quot;&gt;&lt;code&gt;matklad&lt;&#x2F;code&gt;’s&lt;&#x2F;a&gt; response.
In learning more about programming language implementation, I came across &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;~asampson&#x2F;blog&#x2F;flattening.html&quot;&gt;this
wonderful post&lt;&#x2F;a&gt; by
Professor Adrian Sampson, which examines the effects of flattening the abstract
syntax tree of a similar little language.
I thought it’d be interesting to perform a similar analysis with our random art
language implementation, so I put together &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;ra-impls&quot;&gt;a Rust
project&lt;&#x2F;a&gt; with four
different implementations.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementations&quot;&gt;Implementations&lt;&#x2F;h1&gt;
&lt;p&gt;The first implementation is the most basic, where we represent our AST directly:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Expr&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    X&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Y&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SinPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    CosPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Mul&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Avg&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Thresh&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As mentioned &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;random-art&#x2F;&quot;&gt;last time&lt;&#x2F;a&gt;, it can be more
efficient store a single pointer (i.e. &lt;code&gt;Box&lt;&#x2F;code&gt;), which my code confusingly calls
the “branch” version (apologies, but I wanted the names to be in alphabetical
order and it was the best I could come up with).
In this version, the main &lt;code&gt;Expr&lt;&#x2F;code&gt; looks like&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Expr&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    X&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Y&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SinPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    CosPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Mul&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Mul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Avg&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Avg&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Thresh&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Thresh&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;where each of the intermediate structures looks like &lt;code&gt;struct Mul(Expr, Expr)&lt;&#x2F;code&gt;, etc.&lt;&#x2F;p&gt;
&lt;p&gt;After that, we move to a flat version similar to the one explored in Dr.
Sampson’s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;~asampson&#x2F;blog&#x2F;flattening.html&quot;&gt;post&lt;&#x2F;a&gt;.
In this, we go back to the basic structure, wherein we store (potentially)
multiple things in a single &lt;code&gt;Expr&lt;&#x2F;code&gt; variant:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Expr&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    X&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Y&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SinPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ExprRef&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    CosPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ExprRef&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Mul&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ExprRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ExprRef&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Avg&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ExprRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ExprRef&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Thresh&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ExprRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ExprRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ExprRef&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ExprRef&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This time, though, the &lt;code&gt;ExprRef&lt;&#x2F;code&gt;s are instead indices, 32 bit unsigned integers, like &lt;code&gt;struct ExprRef(u32)&lt;&#x2F;code&gt;.
We then use a pool of expressions, &lt;code&gt;struct ExprPool(Vec&amp;lt;Expr&amp;gt;)&lt;&#x2F;code&gt;; during its
construction, every &lt;code&gt;ExprRef&lt;&#x2F;code&gt; index points to a valid index into this vector.
This controls memory layout even more tightly than our branch version, though
interpretation still
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;ra-impls&#x2F;src&#x2F;flat.rs#L72&quot;&gt;uses&lt;&#x2F;a&gt;
an additional vector of the same length.&lt;&#x2F;p&gt;
&lt;p&gt;The final version follows &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;ProgrammingLanguages&#x2F;comments&#x2F;mrifdr&#x2F;treewalking_interpreters_and_cachelocality&#x2F;gumsi2v&#x2F;&quot;&gt;this
comment&lt;&#x2F;a&gt;
by &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;craftinginterpreters.com&#x2F;&quot;&gt;Bob Nystrom&lt;&#x2F;a&gt;, modifying our flat version
to implement a stack-based bytecode interpreter.
Every subexpression is interpreted before the larger expression that uses it,
and the ordering of items is all that matters.
We can eschew any kind of pointing or indexing, and move directly to a stack of individual instructions:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Expr&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    X&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Y&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SinPi&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    CosPi&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Mul&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Avg&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Thresh&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These are then stored in order in a &lt;code&gt;struct ExprPool(Vec&amp;lt;Expr&amp;gt;)&lt;&#x2F;code&gt; during
construction, and
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;ra-impls&#x2F;src&#x2F;stack.rs#L66&quot;&gt;interpretation&lt;&#x2F;a&gt;
uses a single stack.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;do-you-even-bench-mark&quot;&gt;Do you even bench(mark)?&lt;&#x2F;h1&gt;
&lt;p&gt;For benchmarking, for each version we build up a random expression of “depth”
26 in the same fashion as last post, then evaluate that expression with a
pseudorandomly chosen &lt;code&gt;x&lt;&#x2F;code&gt; and &lt;code&gt;y&lt;&#x2F;code&gt;.
Reaching for the amazing &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;sharkdp&#x2F;hyperfine&quot;&gt;&lt;code&gt;hyperfine&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to
time our various approaches:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;ra_shell.png&quot; alt=&quot;Hyperfine command for generating benchmarks&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;We get the following times:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;ra_bench.png&quot; alt=&quot;Plot of benchmark times by command&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Interestingly, there’s a large time improvement in moving from the basic to the branch version alone!
From there, we get further improvements moving to the flat and stack versions,
though the savings are less dramatic.&lt;&#x2F;p&gt;
&lt;p&gt;To keep myself honest, I also double-checked that versions yield equivalent
results (at least, up to a certain depth) via some &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;ra-impls&#x2F;src&#x2F;main.rs#L44-L69&quot;&gt;property-based
tests&lt;&#x2F;a&gt;
with the help of the exceptional &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;proptest-rs&#x2F;proptest&quot;&gt;&lt;code&gt;proptest&lt;&#x2F;code&gt;
crate&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;coda-code&quot;&gt;Coda &amp;amp; Code&lt;&#x2F;h1&gt;
&lt;p&gt;As always, it was fun and interesting to take the lessons learned in reading and see their impacts in practice.
Please feel free to check out &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;ra-impls&quot;&gt;the code&lt;&#x2F;a&gt; for more details!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Here, Have a Fractal</title>
        <published>2024-11-06T00:00:00+00:00</published>
        <updated>2024-11-06T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/here-have-a-fractal/"/>
        <id>https://grahamenos.com/here-have-a-fractal/</id>
        
        <content type="html" xml:base="https://grahamenos.com/here-have-a-fractal/">&lt;h1 id=&quot;not-feeling-it&quot;&gt;Not feeling it&lt;&#x2F;h1&gt;
&lt;p&gt;Today isn’t great.
I don’t like it.
So here, to take minds off things, have a fractal.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;mandelbrot.png&quot; alt=&quot;Mandelbrot Set&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Ugly Rust code, using &lt;code&gt;image&lt;&#x2F;code&gt; and &lt;code&gt;num_complex&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; image&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ImageBuffer&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Rgb&lt;&#x2F;span&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; num_complex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Complex&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; img&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 3200&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; scale&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 3.0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; img&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f32&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; max_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 42.0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; imgbuf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ImageBuffer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;from_par_fn&lt;&#x2F;span&gt;&lt;span&gt;(img, img,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span&gt;x, y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Complex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;new&lt;&#x2F;span&gt;&lt;span&gt;(x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; scale&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2.25&lt;&#x2F;span&gt;&lt;span&gt;, y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; scale&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1.5&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span&gt; z,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; mut&lt;&#x2F;span&gt;&lt;span&gt; i)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Complex&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;f32&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;ZERO&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0.0&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        while&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; max_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;norm_sqr&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 16.0&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; c;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1.0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1.0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;norm&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;ln&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;log2&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 360.0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; max_iter&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 210.0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 360.0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; max_iter)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; u8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f32&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        let&lt;&#x2F;span&gt;&lt;span&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = |&lt;&#x2F;span&gt;&lt;span&gt;n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            let&lt;&#x2F;span&gt;&lt;span&gt; k&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; (h&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 60.0&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 6.0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;255.0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0.5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; v&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;f32&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;max&lt;&#x2F;span&gt;&lt;span&gt;(k&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;min&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;4.0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; k)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;min&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;))))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; u8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        Rgb&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;5.0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3.0&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1.0&lt;&#x2F;span&gt;&lt;span&gt;)])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    imgbuf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;save&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;mandelbrot.png&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Tropical Semiring in Dyalog APL</title>
        <published>2024-04-04T00:00:00+00:00</published>
        <updated>2024-04-04T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/apl-min-plus/"/>
        <id>https://grahamenos.com/apl-min-plus/</id>
        
        <content type="html" xml:base="https://grahamenos.com/apl-min-plus/">&lt;h1 id=&quot;a-few-of-my-favorite-things&quot;&gt;A few of my favorite things&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tropical_semiring&quot;&gt;Tropical (min-plus)
semiring&lt;&#x2F;a&gt; is one of my
favorite examples of how changing one’s perspective can make difficult problems
much simpler.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
In this semiring, instead of using the usual addition and multiplication,
we replace them with minimum and addition, so, for instance, \( 1 \oplus 2 = 1
\) and \( 3 \otimes 2 = 5 \).
I’ve &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;apl-min-plus&#x2F;%7B@&#x2F;2023-05-11-kompreni.md&quot;&gt;written&lt;&#x2F;a&gt; and
&lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;algebraic-structure&#x2F;&quot;&gt;presented&lt;&#x2F;a&gt; about
&lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;ascb-in-rust&#x2F;&quot;&gt;this&lt;&#x2F;a&gt; before.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;links-from-the-farm&quot;&gt;Links from the farm&lt;&#x2F;h1&gt;
&lt;p&gt;Recently, someone on the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;aplwiki.com&#x2F;wiki&#x2F;APL_Farm&quot;&gt;APL Farm&lt;&#x2F;a&gt; posted
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;blog.sigfpe.com&#x2F;2009&#x2F;05&#x2F;three-projections-of-doctor-futamura.html&quot;&gt;an excellent
article&lt;&#x2F;a&gt;
by &lt;code&gt;sigfpe&lt;&#x2F;code&gt; (AKA &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;mathstodon.xyz&#x2F;@dpiponi&quot;&gt;Dan&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;sigfpe&quot;&gt;Piponi&lt;&#x2F;a&gt;), which prompted me to post a
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;blog.sigfpe.com&#x2F;2007&#x2F;06&#x2F;how-to-write-tolerably-efficient.html&quot;&gt;different&lt;&#x2F;a&gt;
article where they work with the min-+ semiring.
I also posted &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;r6.ca&#x2F;blog&#x2F;20110808T035622Z.html&quot;&gt;this article&lt;&#x2F;a&gt; by
Russell O’Connor, which is one of my all-time favorites.
In it, there’s an example where the distances between nodes in a graph gets
turned into a Tropical matrix, and algorithms for computing shortest distances
become iterated linear algebra over this different semiring.
And then I wanted to work in APL instead of Haskell…&lt;&#x2F;p&gt;
&lt;h1 id=&quot;an-open-dialogue&quot;&gt;An open dialogue&lt;&#x2F;h1&gt;
&lt;p&gt;After a bit of head scratching, I realized that &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.dyalog.com&#x2F;&quot;&gt;Dyalog
APL&lt;&#x2F;a&gt; is really good for manipulating matrices over the
min-+ semiring.
Taking &lt;code&gt;exampleGraph2&lt;&#x2F;code&gt; from O’Connor’s blog post, and letting \( 10^{20} \)
stand in for \( \infty \)&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;inf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;←&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1e20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;dist&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;←&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;7 9&lt;&#x2F;span&gt;&lt;span&gt; inf inf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 14 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      0 10 15&lt;&#x2F;span&gt;&lt;span&gt; inf inf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      9 10  0 11&lt;&#x2F;span&gt;&lt;span&gt; inf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      inf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 15 11 0 6&lt;&#x2F;span&gt;&lt;span&gt; inf&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      inf inf inf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 6 0 9&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      14&lt;&#x2F;span&gt;&lt;span&gt; inf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt; inf&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 9 0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we have the following distance matrix&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      dist&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.0E0  7.0E0  9.0E0  1.0E20 1E20 1.4E1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;7.0E0  0.0E0  1.0E1  1.5E1  1E20 1.0E20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;9.0E0  1.0E1  0.0E0  1.1E1  1E20 2.0E0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1.0E20 1.5E1  1.1E1  0.0E0  6E0  1.0E20&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1.0E20 1.0E20 1.0E20 6.0E0  0E0  9.0E0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1.4E1  1.0E20 2.0E0  1.0E20 9E0  0.0E0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this matrix, representing distances between vertices in our graph, we can
use the APL matrix product operator &lt;code&gt;.&lt;&#x2F;code&gt; &lt;em&gt;with different operations&lt;&#x2F;em&gt; to perform
our calculations; instead of &lt;code&gt;+&lt;&#x2F;code&gt; and &lt;code&gt;×&lt;&#x2F;code&gt; for addition and multiplication, we
turn to &lt;code&gt;⌊&lt;&#x2F;code&gt; (min) and &lt;code&gt;+&lt;&#x2F;code&gt;; then &lt;code&gt;dist ⌊.+ dist&lt;&#x2F;code&gt; gives us the two-hop distances:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      dist&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ⌊.+&lt;&#x2F;span&gt;&lt;span&gt; dist&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0  7  9 20 23 11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 7  0 10 15 21 12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 9 10  0 11 11  2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;20 15 11  0  6 13&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;23 21 11  6  0  9&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;11 12  2 13  9  0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can make this more succinct with &lt;code&gt;⍨&lt;&#x2F;code&gt;, telling the interpreter to apply our
function with &lt;code&gt;dist&lt;&#x2F;code&gt; as both arguments:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;      ⌊.+⍨&lt;&#x2F;span&gt;&lt;span&gt; dist&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0  7  9 20 23 11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 7  0 10 15 21 12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 9 10  0 11 11  2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;20 15 11  0  6 13&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;23 21 11  6  0  9&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;11 12  2 13  9  0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I was very excited that I could write down the steps of the
Gauss-Jordan-Floyd-Warshall-Kleene algorithm in so few characters!
Moreover, iterating that step until convergence is similarly succinct using the
power operator &lt;code&gt;⍣&lt;&#x2F;code&gt;; we can keep running &lt;code&gt;⌊.+&lt;&#x2F;code&gt; until the output matches (&lt;code&gt;≡&lt;&#x2F;code&gt;) the input:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;apl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;      ⌊.+⍨⍣≡&lt;&#x2F;span&gt;&lt;span&gt;dist&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0  7  9 20 20 11&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 7  0 10 15 21 12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 9 10  0 11 11  2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;20 15 11  0  6 13&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;20 21 11  6  0  9&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;11 12  2 13  9  0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Imagine my surprise when I searched for “distance” in Iverson’s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.jsoftware.com&#x2F;papers&#x2F;tot.htm&quot;&gt;notation as a
tool of thought&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;and if &lt;code&gt;p&lt;&#x2F;code&gt; gives distances from a source to transhipment points and &lt;code&gt;q&lt;&#x2F;code&gt; gives
distances from the transhipment points to the destination, then &lt;code&gt;p⌊.+q&lt;&#x2F;code&gt; gives
the minimum distance possible.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;As Alan Kay &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;quoteinvestigator.com&#x2F;2018&#x2F;05&#x2F;29&#x2F;pov&#x2F;&quot;&gt;says&lt;&#x2F;a&gt;, point
of view is worth 80 IQ points. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;I tried this in &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;mlochbaum.github.io&#x2F;BQN&#x2F;&quot;&gt;BQN&lt;&#x2F;a&gt; as well, since it
has \( \infty \), but the linear algebra and fixed point iteration are
nicer in APL. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Evaluating quantum generative models via imbalanced data classification benchmarks</title>
        <published>2023-08-22T00:00:00+00:00</published>
        <updated>2023-08-22T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/gb-paper-announcement/"/>
        <id>https://grahamenos.com/gb-paper-announcement/</id>
        
        <content type="html" xml:base="https://grahamenos.com/gb-paper-announcement/">&lt;h1 id=&quot;new-paper-who-dis&quot;&gt;New paper, who dis?&lt;&#x2F;h1&gt;
&lt;p&gt;I’m excited to announce that our paper on using classification performance on
imbalanced datasets as a proxy for measuring generative model quality &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2308.10847&quot;&gt;up on
the arXiv&lt;&#x2F;a&gt;!
It combines a lot of interesting techniques, like post-hoc testing, the
Bayesian boostrap, and explainable AI techniques, all to get an idea of how
well a quantum generative model performs.
Please check it out!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Rusty Quantum Interpreter</title>
        <published>2023-08-14T00:00:00+00:00</published>
        <updated>2023-08-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/quantum-interpreter/"/>
        <id>https://grahamenos.com/quantum-interpreter/</id>
        
        <content type="html" xml:base="https://grahamenos.com/quantum-interpreter/">&lt;h1 id=&quot;the-original&quot;&gt;The Original&lt;&#x2F;h1&gt;
&lt;p&gt;Robert Smith, a.k.a. &lt;code&gt;stylewarning&lt;&#x2F;code&gt;, has a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.stylewarning.com&#x2F;posts&#x2F;quantum-interpreter&#x2F;&quot;&gt;lovely blog
post&lt;&#x2F;a&gt; that walks
through implementing an interpreter for a “general-purpose quantum programming
language called \( \mathscr{L} \).”
In only 150 lines of Common Lisp, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;stylewarning&#x2F;quantum-interpreter&#x2F;&quot;&gt;the
implementation&lt;&#x2F;a&gt; is
featureful, self-contained, and a delight to read.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;imitation-is-the-highest-form-of-flattery&quot;&gt;Imitation is the highest form of flattery&lt;&#x2F;h1&gt;
&lt;p&gt;At first I was content with only reading the post and code, but then &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;Sheganinans&#x2F;status&#x2F;1683875551587741703&quot;&gt;I
saw&lt;&#x2F;a&gt; someone else
had put together an OCaml implementation as well.
The game was afoot!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;carcinization&quot;&gt;Carcinization&lt;&#x2F;h1&gt;
&lt;p&gt;Eventually I put together a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;quantum-interpreter&quot;&gt;Rust
implementation&lt;&#x2F;a&gt;.
This version weights in at more than twice the line count as the
original—even though it relies on
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;faer&#x2F;latest&#x2F;faer&#x2F;index.html&quot;&gt;&lt;code&gt;faer&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for linear
algebra instead of implementing it by hand!&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
However, it does have a couple of features not present in the original (or
OCaml) implementation(s):&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;constructing a &lt;code&gt;Machine&lt;&#x2F;code&gt; takes an unsigned 64-bit &lt;code&gt;seed&lt;&#x2F;code&gt; for repeatable PRNG
behavior; and&lt;&#x2F;li&gt;
&lt;li&gt;with the help of the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;peg&#x2F;latest&#x2F;peg&#x2F;&quot;&gt;&lt;code&gt;peg&lt;&#x2F;code&gt;
crate&lt;&#x2F;a&gt;, I added parsing, so one can pass
&lt;code&gt;&quot;H 0\nMEASURE&quot;&lt;&#x2F;code&gt; as a string and get an interpretable quantum program.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h1 id=&quot;code-or-it-didn-t-happen&quot;&gt;Code or it didn’t happen&lt;&#x2F;h1&gt;
&lt;p&gt;The implementation is
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;quantum-interpreter&quot;&gt;here&lt;&#x2F;a&gt; in my
catch-all “workbench” repository.
It’s definitely &lt;em&gt;not&lt;&#x2F;em&gt; production-worthy code; it’s got an &lt;code&gt;assert!&lt;&#x2F;code&gt; that will
panic if not met, and it uses &lt;code&gt;expect&lt;&#x2F;code&gt; to pave over some errors.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
Please feel free to take a look and let me know what you think!&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;The original version used &lt;code&gt;ndarray&lt;&#x2F;code&gt;, since I was more familiar with that crate. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;Update 2024-06-27: As of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;pull&#x2F;1&quot;&gt;this PR&lt;&#x2F;a&gt;, the code is a little more respectable now, with proper error handling via &lt;code&gt;Result&lt;&#x2F;code&gt; and no panics; that said, I’d still advise against using it in a production setting. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Random Art with a Tiny Language</title>
        <published>2023-06-02T00:00:00+00:00</published>
        <updated>2023-06-02T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/random-art/"/>
        <id>https://grahamenos.com/random-art/</id>
        
        <content type="html" xml:base="https://grahamenos.com/random-art/">&lt;h1 id=&quot;pl-resources-everywhere&quot;&gt;PL Resources Everywhere&lt;&#x2F;h1&gt;
&lt;p&gt;In &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;kompreni&#x2F;&quot;&gt;playing around with OCaml&lt;&#x2F;a&gt;, I’ve
spent some spare time perusing more programming language resources.
There are a bunch out there, especially in this era of online learning; for
instance, Cornell has made some
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;courses&#x2F;cs6120&#x2F;2020fa&#x2F;lesson&#x2F;&quot;&gt;great&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.cs.cornell.edu&#x2F;courses&#x2F;cs6110&#x2F;2019sp&#x2F;schedule.html&quot;&gt;resources&lt;&#x2F;a&gt;
available.
In looking specifically for more ML-ish&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; flavored ones, I came across
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;ucsd-progsys.github.io&#x2F;cse130&#x2F;homeworks&#x2F;hw2.html&quot;&gt;this&lt;&#x2F;a&gt; fun undergrad
homework set.
In the second problem, it asks you to implement a tiny (recursively
defined) language of expressions encoding functions from the unit square to the
unit interval, \( [0, 1]^2 \mapsto [0, 1] \).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; VarX&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;          |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; VarY&lt;&#x2F;span&gt;&lt;span&gt; &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;          |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Sine&lt;&#x2F;span&gt;&lt;span&gt; of expr &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;          |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Cosine&lt;&#x2F;span&gt;&lt;span&gt; of expr &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;          |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Average&lt;&#x2F;span&gt;&lt;span&gt; of expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; expr &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;          |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Times&lt;&#x2F;span&gt;&lt;span&gt; of expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; expr &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;          |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Thresh&lt;&#x2F;span&gt;&lt;span&gt; of expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; expr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; expr &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After you learn to build them up randomly, you can evaluate them across the
unit square to generate pictures—have each output value encode either a
greyscale value, or one of an RGB triple.
Note to undergrads: &lt;em&gt;please&lt;&#x2F;em&gt; don’t cheat off me; you’ll only rob yourself of a
fun learning experience.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;no-peeking&quot;&gt;No Peeking!&lt;&#x2F;h1&gt;
&lt;p&gt;This was a fun exercise in OCaml, but I also found it instructive to tackle it
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;ra&quot;&gt;in Rust&lt;&#x2F;a&gt; and compare.
Besides the superficial differences, Rust’s concern with and ability to control
memory usage is apparent.
I defined the core enumeration thusly:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub enum&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Expr&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    X&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Y&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    SinPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    CosPi&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Expr&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Mul&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Mul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Avg&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Avg&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Thresh&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Box&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Thresh&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I renamed some variants to more accurately express their intent; e.g.
&lt;code&gt;SinPi(expr)&lt;&#x2F;code&gt; evaluates to \( sin(\pi \cdot \) &lt;code&gt;expr&lt;&#x2F;code&gt; \(\!)\).
More importantly, I reworked the recursive variants to each contain (at most) a
single &lt;code&gt;Box&lt;&#x2F;code&gt; (i.e. unique pointer to a heap allocated value); for instance,
&lt;code&gt;Expr::Mul&lt;&#x2F;code&gt; contains a &lt;code&gt;Box&amp;lt;Mul&amp;gt;&lt;&#x2F;code&gt;, where &lt;code&gt;Mul&lt;&#x2F;code&gt; is defined as&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Mul&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    e1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Expr&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    e2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Expr&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I took up this trick after
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;users.rust-lang.org&#x2F;t&#x2F;is-there-a-better-way-to-represent-an-abstract-syntax-tree&#x2F;9549&#x2F;4&quot;&gt;learning&lt;&#x2F;a&gt;
more about it from the inimitable &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;matklad.github.io&#x2F;&quot;&gt;&lt;code&gt;matklad&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
As the linked response says, this keeps the size of the base enum smaller.
In the source code, I’ve got a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;ra&#x2F;src&#x2F;lib.rs#L242-L245&quot;&gt;property
test&lt;&#x2F;a&gt;
that asserts the size of an arbitrary&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; &lt;code&gt;Expr&lt;&#x2F;code&gt; is exactly 16 bytes&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; size_of&lt;&#x2F;span&gt;&lt;span&gt;(e&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; arb_expr&lt;&#x2F;span&gt;&lt;span&gt;()) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;size_of_val&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;e),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 16&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we instead went with a more direct translation of the OCaml code, like&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;diff&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;diff --git a&#x2F;src&#x2F;lib.rs b&#x2F;src&#x2F;lib.rs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;index 3b04a74..68c600c 100644&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B31D28;background-color: #FFEEF0;&quot;&gt;--- a&#x2F;src&#x2F;lib.rs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #22863A;background-color: #F0FFF4;&quot;&gt;+++ b&#x2F;src&#x2F;lib.rs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;font-weight: bold;&quot;&gt;@@ -7,9 +7,9 @@&lt;&#x2F;span&gt;&lt;span&gt; pub enum Expr {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     Y,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     SinPi(Box&amp;lt;Expr&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     CosPi(Box&amp;lt;Expr&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B31D28;background-color: #FFEEF0;&quot;&gt;-    Mul(Box&amp;lt;Mul&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B31D28;background-color: #FFEEF0;&quot;&gt;-    Avg(Box&amp;lt;Avg&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B31D28;background-color: #FFEEF0;&quot;&gt;-    Thresh(Box&amp;lt;Thresh&amp;gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #22863A;background-color: #F0FFF4;&quot;&gt;+    Mul { e1: Box&amp;lt;Expr&amp;gt;, e2: Box&amp;lt;Expr&amp;gt; },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #22863A;background-color: #F0FFF4;&quot;&gt;+    Avg { e1: Box&amp;lt;Expr&amp;gt;, e2: Box&amp;lt;Expr&amp;gt; },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #22863A;background-color: #F0FFF4;&quot;&gt;+    Thresh { e1: Box&amp;lt;Expr, e2: Box&amp;lt;Expr&amp;gt;, e3: Box&amp;lt;Expr&amp;gt;, e4: Box&amp;lt;Expr&amp;gt; },&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;then this property test would no longer pass, as the enum needs to be large
enough to contain its largest variant.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;pretty-pics-plz&quot;&gt;Pretty Pics Plz&lt;&#x2F;h1&gt;
&lt;p&gt;Once I coded up the library, I also put together a small
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;ra&#x2F;src&#x2F;main.rs&quot;&gt;executable&lt;&#x2F;a&gt; to
take in some parameters and generate greyscale or RGB pictures, similar to the
original exercise.
With that defined, I could run it a bunch of times using
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.oreilly.com&#x2F;library&#x2F;view&#x2F;mac-os-x&#x2F;0596003706&#x2F;re254.html&quot;&gt;&lt;code&gt;jot&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to
generate random seeds and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;parallel&#x2F;&quot;&gt;&lt;code&gt;parallel&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
to use multiple cores:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;jot&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;w&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span&gt;i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;r&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 20 0 1000000000&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span&gt; parallel&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; --&lt;&#x2F;span&gt;&lt;span&gt;bar&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; .&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;target&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;release&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;ra&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;s {}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;d&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;r&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here are some gems I found combing through the PNGs:&lt;&#x2F;p&gt;
&lt;table&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;126089461_7_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=126089461, depth=7 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;208259458_7_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=208259458, depth=7 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;347723950_7_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=347723950, depth=7 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;765744456_7_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=765744456, depth=7 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;983185677_6_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=983185677, depth=6 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;161582962_7_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=161582962, depth=7 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
    &lt;tr&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;176235008_7_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=176235008, depth=7 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;61786666_7_512_512_rgb.png&quot;  width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=61786666, depth=6 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
        &lt;td&gt;&lt;img src=&quot;&#x2F;images&#x2F;627656271_7_512_512_rgb.png&quot; width=&quot;66&quot; height=&quot;66&quot; alt=&quot;RGB Image for seed=627656271, depth=7 width &amp; height=512&quot;&gt;&lt;&#x2F;td&gt;
    &lt;&#x2F;tr&gt;
&lt;&#x2F;table&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;ML_(programming_language)&quot;&gt;ML&lt;&#x2F;a&gt; as in OCaml
and SML, not machine learning. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;I &lt;em&gt;really&lt;&#x2F;em&gt; like property-based testing, in this case with
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;proptest&#x2F;latest&#x2F;proptest&#x2F;&quot;&gt;&lt;code&gt;proptest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;Since
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;mem&#x2F;fn.size_of_val.html&quot;&gt;&lt;code&gt;size_of_val&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
returns the size of the pointed-to value in bytes, this test returns 16 on
my 64-bit laptop; it will probably fail on a 32-bit architecture. Be sure
to check out
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;fasterthanli.me&#x2F;articles&#x2F;peeking-inside-a-rust-enum&quot;&gt;this&lt;&#x2F;a&gt; awesome
article for more on Rust enums. &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Kompreni: Understanding OCaml with Abstract Algebra Examples</title>
        <published>2023-05-11T00:00:00+00:00</published>
        <updated>2023-05-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/kompreni/"/>
        <id>https://grahamenos.com/kompreni/</id>
        
        <content type="html" xml:base="https://grahamenos.com/kompreni/">&lt;h1 id=&quot;why-and-wherefore&quot;&gt;Why and wherefore&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve wanted to level up my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;ocaml.org&#x2F;&quot;&gt;OCaml&lt;&#x2F;a&gt; understanding for a
while now, but between doing a fair amount of work in Rust at my current job
and already having a bit of Haskell, I wasn’t sure I could justify the
“distraction.”
But between some &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;cs3110.github.io&#x2F;textbook&#x2F;cover.html&quot;&gt;excellent&lt;&#x2F;a&gt;
resources available &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;dev.realworldocaml.org&#x2F;index.html&quot;&gt;online&lt;&#x2F;a&gt;, some
interesting reading (more on that in a moment), and the newest &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;ocaml.org&#x2F;releases&#x2F;5.0.0&quot;&gt;5.0.0
release&lt;&#x2F;a&gt;, I decided it was time to dig in!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;oleg-kiselyov-does-something-cool-news-at-11&quot;&gt;Oleg Kiselyov does something cool, news at 11&lt;&#x2F;h1&gt;
&lt;p&gt;The first reading topic I’ve stumbled across recently come from an extremely
prolific CS researcher, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;okmij.org&#x2F;ftp&#x2F;&quot;&gt;Oleg Kiselyov&lt;&#x2F;a&gt; who has written
a lot of amazing papers and code.
One such project is the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;okmij.org&#x2F;ftp&#x2F;tagless-final&#x2F;&quot;&gt;tagless-final&lt;&#x2F;a&gt;
style of embedding domain-specific languages in a typed functional language
like Haskell or OCaml.
I can’t do it justice, it’s truly amazing stuff, you should go read it
yourself.
But!
Having seen some of the work before in Haskell, it was intriguing to look
through this work and compare and contrast Haskell and OCaml versions.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;modules-modules-modules&quot;&gt;Modules modules modules&lt;&#x2F;h1&gt;
&lt;p&gt;The second topic is modules, as in &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.stephendiehl.com&#x2F;posts&#x2F;exotic01.html&quot;&gt;“module systems” and “modular
programming”&lt;&#x2F;a&gt;.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
They’re really interesting; indeed, some say &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;existentialtype.wordpress.com&#x2F;2011&#x2F;04&#x2F;16&#x2F;modules-matter-most&#x2F;&quot;&gt;modules matter
most&lt;&#x2F;a&gt;.
OCaml (like SML and other related languages) allow for some really interesting
flexibility around designing and implementing interfaces.
I won’t be able to explain as well as the above link or
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.pathsensitive.com&#x2F;2023&#x2F;03&#x2F;modules-matter-most-for-masses.html&quot;&gt;this&lt;&#x2F;a&gt;
more verbose and less esoteric explanation.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;algebraic-building-blocks&quot;&gt;Algebraic building blocks&lt;&#x2F;h1&gt;
&lt;p&gt;Which leads me to the point of my little
project named &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;kompreni&#x2F;&quot;&gt;&lt;code&gt;kompreni&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
While it is certainly nowhere near as complete as, say,
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Risto-Stevcev&#x2F;bastet&#x2F;&quot;&gt;&lt;code&gt;bastet&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; I wanted to use
describing abstract algebra ideas as a way to explore expressing things in
OCaml.&lt;&#x2F;p&gt;
&lt;p&gt;We’ll start with a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Semigroup&quot;&gt;semigroup&lt;&#x2F;a&gt;; a
semigroup consists of a set \( S \) and an associative binary operation \( \cdot \)
on that set; that is, for any \(x\), \(y\), and \(z\) in \(S\), it must be the
case that \( x \cdot (y \cdot z) = (x \cdot y) \cdot z \).&lt;&#x2F;p&gt;
&lt;p&gt;In &lt;code&gt;kompreni&lt;&#x2F;code&gt;, I’ve written a &lt;code&gt;Signature&lt;&#x2F;code&gt; for semigroups:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Signature =&lt;&#x2F;span&gt;&lt;span&gt; sig&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; t&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;along with an accompanying &lt;code&gt;Laws&lt;&#x2F;code&gt; functor, a function which takes &lt;em&gt;any&lt;&#x2F;em&gt; module
implementing the above &lt;code&gt;Signature&lt;&#x2F;code&gt; and creates a new module:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Laws&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Signature&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  open&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; S&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; associative&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One thing I miss compared to Haskell is being able to specify the precedence
and associativity of user-defined operators like &lt;code&gt;+&amp;amp;&lt;&#x2F;code&gt;.
Since it begins with the plus symbol, OCaml treats it like a plus; ah well.
That means that this expression above, when formatted with &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ocaml&#x2F;dune&quot;&gt;&lt;code&gt;dune fmt&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, looks like it’s missing some parentheses;
OCaml says &lt;code&gt;+&lt;&#x2F;code&gt; is left-associative, so it says &lt;code&gt;+&amp;amp;&lt;&#x2F;code&gt; is too.&lt;&#x2F;p&gt;
&lt;p&gt;With that signature and functor together, I’ve next turned to
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;c-cube&#x2F;qcheck&quot;&gt;&lt;code&gt;qcheck&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for property-based testing in
OCaml.
For instance, in &lt;code&gt;kompreni&lt;&#x2F;code&gt;’s test suite I’ve got the following.
First we define a base signature for testing:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Testable =&lt;&#x2F;span&gt;&lt;span&gt; sig&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; t&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; gen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; QCheck2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Gen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;t&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Then we combine it with the &lt;code&gt;Signature&lt;&#x2F;code&gt; above to actually test the &lt;code&gt;associative&lt;&#x2F;code&gt; property:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; SemigroupLaws&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Testable&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Semigroup&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Signature &lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;text-decoration: underline;&quot;&gt;with&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; = X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Semigroup&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Laws&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; tests&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      make_test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        QCheck2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Gen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;(triple&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;gen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;gen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;gen)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;quot;associative&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; (uncurry3 associative)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With other scaffolding (like &lt;code&gt;uncurry3&lt;&#x2F;code&gt;) defined elsewhere, this takes two
modules—one implementing the testable interface and another that is a
&lt;code&gt;Semigroup&lt;&#x2F;code&gt; with the &lt;em&gt;same&lt;&#x2F;em&gt; internal type &lt;code&gt;t&lt;&#x2F;code&gt;—and defines a list of &lt;code&gt;qcheck&lt;&#x2F;code&gt; tests.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;kompreni&lt;&#x2F;code&gt; also contains &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Monoid&quot;&gt;monoids&lt;&#x2F;a&gt;,
semigroups with an identity element we’ll call &lt;code&gt;zero&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Signature =&lt;&#x2F;span&gt;&lt;span&gt; sig&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Semigroup&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Signature&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; zero&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that &lt;code&gt;include&lt;&#x2F;code&gt; statement, which says “bring in the body of the &lt;code&gt;Semigroup&lt;&#x2F;code&gt;
signature, too.”
We also have more &lt;code&gt;Laws&lt;&#x2F;code&gt; for monoids to fulfill, in addition to &lt;code&gt;associative&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Laws&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;M&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Signature&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  open&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; M&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Semigroup&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Laws&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;M&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; left_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; zero&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; right_id&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; zero&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;which we can also test:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; MonoidLaws&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Testable&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;M&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Monoid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;Signature &lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;text-decoration: underline;&quot;&gt;with&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; = X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Monoid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Laws&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;M&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; tests&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;&quot;&gt; module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; SL&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; = SemigroupLaws&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;M&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;in&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    List&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;map (uncurry2 (make_test&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;gen))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      [&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;left id&amp;quot;,&lt;&#x2F;span&gt;&lt;span&gt; left_id)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;right id&amp;quot;,&lt;&#x2F;span&gt;&lt;span&gt; right_id)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    @&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; SL&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;tests&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;semirings-in-paradise&quot;&gt;Semirings in paradise&lt;&#x2F;h1&gt;
&lt;p&gt;This post is already getting too long, so I’ll finish with my favorite example.
I think this setup shows the power of OCaml modules, but also shows a place
where either I don’t know how to use them well enough (&lt;em&gt;very&lt;&#x2F;em&gt; plausible!) or
they fall just a tad short of what I’d like.&lt;&#x2F;p&gt;
&lt;p&gt;Consider &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Semiring&quot;&gt;semirings&lt;&#x2F;a&gt;, which consist of
a set of elements \( S \), &lt;em&gt;two&lt;&#x2F;em&gt; operations \( + \) and \( \cdot \), and
two special elements \(0\) and \(1\) such that:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;\((S, +, 0)\) forms a commutative (i.e. \(x + y = y + x\)) monoid,&lt;&#x2F;li&gt;
&lt;li&gt;\((S, \cdot, 1)\) is a monoid,&lt;&#x2F;li&gt;
&lt;li&gt;multiplication of anything by \(0\) returns \(0\), and&lt;&#x2F;li&gt;
&lt;li&gt;multiplication \(\cdot\) distributes over \(+\).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;My absolute favorite semiring is the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tropical_semiring&quot;&gt;Tropical
semiring&lt;&#x2F;a&gt;, also known as the
min-plus semiring, which consists of:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;\( \mathbb{R} \cup \infty \), numbers that are either finite real numbers or infinite,&lt;&#x2F;li&gt;
&lt;li&gt;\(x + y\) is defined to be \(\min(x, y)\) wiht identity element \(\infty\), and&lt;&#x2F;li&gt;
&lt;li&gt;\(x \cdot y\) is defined to be \(x + y\) in the usual sense, so its identity is zero.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Here’s &lt;code&gt;kompreni&lt;&#x2F;code&gt;’s implementation&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; MinPlus&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span&gt; of&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Q&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Infinite&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    match&lt;&#x2F;span&gt;&lt;span&gt; (a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Q&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;max x y)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Infinite&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; zero&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Infinite&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; )&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    match&lt;&#x2F;span&gt;&lt;span&gt; (a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span&gt; b)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; with&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Q&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;add x y)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Infinite&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; one&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Finite&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Q&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;zero&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And here are the laws that a &lt;code&gt;Semiring&lt;&#x2F;code&gt; must abide by in &lt;code&gt;kompreni&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;ocaml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;background-color: #F0FFF4;text-decoration: underline;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Laws&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Signature&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; struct&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  open&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; S&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Commutative_monoid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Laws&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;S&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt;  (* (S, 1, *&amp;amp;) is also a Monoid *)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; times_associative&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; times_left_one&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; one&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; times_right_one&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; one&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt;  (* Zero annihilates *)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; zero_annihilates_left&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; zero&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; zero&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; zero_annihilates_right&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; zero&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; zero&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;font-weight: bold;&quot;&gt;  (* Distributivity *)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; left_distributive&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; y)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #000000;&quot;&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;text-decoration: underline;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; right_distributive&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #000000;&quot;&gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;text-decoration: underline;&quot;&gt;z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; y)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; +&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; *&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; z)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;end&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I &lt;em&gt;really&lt;&#x2F;em&gt; like being able to include previous law definitions in new ones; for
instance, having defined the &lt;code&gt;Commutative_monoid.Laws&lt;&#x2F;code&gt; functor, the line
&lt;code&gt;include Commutative_monoid.Laws (S)&lt;&#x2F;code&gt; ensures that I include all those laws
into my tests for almost free—ensuring that I test \((S, +, 0)\) is indeed
a commutative monoid.
However, I don’t see a way to cleverly check \((S, ⋅, 1)\) is also a monoid
without writing out the &lt;code&gt;times_&lt;&#x2F;code&gt; rules explicitly.
Maybe I need more OCaml module &amp;amp; functor goodness?
&lt;code&gt;bastet&lt;&#x2F;code&gt; forgoes wrapping the monoid definitions and laws into the semiring one, for what it’s worth.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;fin&quot;&gt;Fin.&lt;&#x2F;h1&gt;
&lt;p&gt;While I miss the usability of &lt;code&gt;cargo&lt;&#x2F;code&gt; from Rust and some fun things from
Haskell, OCaml is fun and eye-opening!
Please feel free to head to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;kompreni&#x2F;&quot;&gt;GitHub&lt;&#x2F;a&gt; to
check out the code.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;Not to be confused with a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Module_(mathematics)&quot;&gt;module over a ring&lt;&#x2F;a&gt; or one of the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Module&quot;&gt;many other&lt;&#x2F;a&gt; definitions of “module.” &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;Nor will it likely &lt;em&gt;ever&lt;&#x2F;em&gt; be. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;Rather than floats, this uses rational numbers from &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ocaml&#x2F;Zarith&quot;&gt;&lt;code&gt;zarith&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for finite values. Using \(\mathbb{Q}\) is not the same as using \(\mathbb{R}\), but this still forms a semiring! Also it’s really, &lt;em&gt;really&lt;&#x2F;em&gt; hard to represent arbitrary real numbers in software. For instance, the associativity tests fail if you use OCaml floats, because &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.quora.com&#x2F;Why-are-floating-point-numbers-defined-as-float-instead-of-real&quot;&gt;floating point numbers aren’t real&lt;&#x2F;a&gt;. &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Scum: write me a Scheme-ish</title>
        <published>2023-04-20T00:00:00+00:00</published>
        <updated>2023-04-20T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/scum/"/>
        <id>https://grahamenos.com/scum/</id>
        
        <content type="html" xml:base="https://grahamenos.com/scum/">&lt;h1 id=&quot;a-wretched-hive-of-scum-and-villainy&quot;&gt;A wretched hive of scum and villainy&lt;&#x2F;h1&gt;
&lt;p&gt;With it being my birthday today, I took a little bit of time to noodle on a pet project.
For all the computer science classes I’ve taken, books I’ve read, course notes I’ve looked through, and code I’ve written, reviewed, or perused, I had yet to implement any sort of Lisp-like language myself.
Having decided to change that, I’ve been toying around for the last month or so on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;scum&quot;&gt;&lt;code&gt;scum&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, a Scheme-ish, Lisp-like language implementation.
It’s by no means good&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; in any sense of the word; it’s incomplete, under-commented, probably loaded with bugs, undoubtedly slow, and profligate with its memory usage.
However, as of today, it’s cleared the bar I set for myself for being a reasonable-ish implementation: I can define and call &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Lambda_calculus&quot;&gt;lambdas&lt;&#x2F;a&gt; from the REPL!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;how-long-is-a-piece-of-string&quot;&gt;How long is a piece of string&lt;&#x2F;h1&gt;
&lt;p&gt;There’s not a lot of code currently, so feel free to peruse the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;scum&quot;&gt;repo&lt;&#x2F;a&gt; yourself:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;github&#x2F;scum ∃ l&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Permissions&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Size User  Date Modified Git Name&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;.rw-r--r--@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;  25k genos&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Apr 14:58&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   --&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Cargo.lock&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;.rw-r--r--@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   94&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; genos&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Apr 14:58&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   --&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Cargo.toml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;.rw-r--r--@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; 1.1k genos&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Apr 14:58&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   --&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; LICENSE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;.rw-r--r--@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   56&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; genos&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Apr 14:58&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   --&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; README.md&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;drwxr-xr-x@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    - genos&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Apr 15:18&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   --&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; scum-lib&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;drwxr-xr-x@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    - genos&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 13&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Apr 14:58&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   --&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; scum-repl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;drwxr-xr-x@&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    - genos&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Apr 13:22&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;   -I&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; target&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;github&#x2F;scum ∃ tokei&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Language&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            Files        Lines         Code     Comments       Blanks&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Markdown&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                1            3            0            2            1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Pest&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                    1           38           26            8            4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Rust&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                    8          628          577            9           42&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; TOML&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                    3           27           23            0            4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Total&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                  13          696          626           19           51&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By no means as succinct as, say &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Robert-van-Engelen&#x2F;tinylisp&quot;&gt;&lt;code&gt;tinylisp&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, nor as complete as the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;kanaka&#x2F;mal&quot;&gt;&lt;code&gt;mal&lt;&#x2F;code&gt; implementations&lt;&#x2F;a&gt;, &lt;code&gt;scum&lt;&#x2F;code&gt; consists of&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;a Rust library which defines the basic expression type, as well as a single step of the read-eval-print loop, and&lt;&#x2F;li&gt;
&lt;li&gt;a Rust executable which does little more than reach out to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;rustyline&#x2F;latest&#x2F;rustyline&#x2F;&quot;&gt;&lt;code&gt;rustyline&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to provide an interactive REPL:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scheme&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ&amp;gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;define&lt;&#x2F;span&gt;&lt;span&gt; square (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;lambda&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; x x)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;lambda&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;) (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt; x x))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;λ&amp;gt;  (square &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;39&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;1521&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I originally started in Haskell, but decided to shift to Rust because I kind of wanted this to be an exercise in satisfying an annoying pedant; Haskell provides so much already, it almost felt like cheating to use it.
Besides the aforementioned &lt;code&gt;rustyline&lt;&#x2F;code&gt; crate in &lt;code&gt;scum-repl&lt;&#x2F;code&gt;, I’ve used &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;pest.rs&#x2F;&quot;&gt;&lt;code&gt;pest&lt;&#x2F;code&gt; and &lt;code&gt;pest_derive&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for parsing and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;crate&#x2F;thiserror&#x2F;latest&quot;&gt;&lt;code&gt;thiserror&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for organizing the sundry reasons things can go wrong.
While &lt;code&gt;pest&lt;&#x2F;code&gt; was new to me and has been fun, I’ve used &lt;code&gt;thiserror&lt;&#x2F;code&gt; on other projects at &lt;code&gt;$WORK&lt;&#x2F;code&gt; before; I absolutely would &lt;em&gt;not&lt;&#x2F;em&gt; write any substantial Rust project without it at this point.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;inspirations&quot;&gt;Inspirations&lt;&#x2F;h1&gt;
&lt;p&gt;I wouldn’t have gotten even this far on &lt;code&gt;scum&lt;&#x2F;code&gt;&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; without the help of&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;kanaka&#x2F;mal&quot;&gt;&lt;code&gt;mal&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,&lt;&#x2F;li&gt;
&lt;li&gt;“Write you a Scheme,” the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;wespiser.com&#x2F;writings&#x2F;wyas&#x2F;00_overview.html&quot;&gt;2.0 update&lt;&#x2F;a&gt; and the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;Write_Yourself_a_Scheme_in_48_Hours&quot;&gt;OG version&lt;&#x2F;a&gt;,&lt;&#x2F;li&gt;
&lt;li&gt;SICP, both the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Structure_and_Interpretation_of_Computer_Programs&quot;&gt;book&lt;&#x2F;a&gt; and the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;ocw.mit.edu&#x2F;courses&#x2F;6-001-structure-and-interpretation-of-computer-programs-spring-2005&#x2F;video_galleries&#x2F;video-lectures&#x2F;&quot;&gt;video lectures&lt;&#x2F;a&gt;,&lt;&#x2F;li&gt;
&lt;li&gt;Peter Norvig’s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;norvig.com&#x2F;lispy.html&quot;&gt;beautifully&lt;&#x2F;a&gt; clean &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;norvig.com&#x2F;lispy2.html&quot;&gt;Python&lt;&#x2F;a&gt; implementations.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h1 id=&quot;postscript&quot;&gt;Postscript&lt;&#x2F;h1&gt;
&lt;p&gt;👋 to the former coworker who enjoined me to blog more!
I miss working with you, friend.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;It’s the exact &lt;em&gt;opposite&lt;&#x2F;em&gt; of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=3067434&quot;&gt;&lt;code&gt;raganwald&lt;&#x2F;code&gt;’s&lt;&#x2F;a&gt; README section, inspiration &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;glenjamin&#x2F;node-fib#is-it-any-good&quot;&gt;here&lt;&#x2F;a&gt;. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;I didn’t consume all of this recently in my quest to put &lt;code&gt;scum&lt;&#x2F;code&gt; together, but I would almost certainly be remiss in not mentioning these; in fact, I’m probably leaving lots out! &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Algebraic Structure ⇒ Computational Benefits, Now With 100% More Oxidization</title>
        <published>2022-09-13T00:00:00+00:00</published>
        <updated>2022-09-13T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/ascb-in-rust/"/>
        <id>https://grahamenos.com/ascb-in-rust/</id>
        
        <content type="html" xml:base="https://grahamenos.com/ascb-in-rust/">&lt;h1 id=&quot;code-or-it-didn-t-happen&quot;&gt;Code or it didn’t happen&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve been noodling with a Rust implementation of the ideas from &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;algebraic-structure&#x2F;&quot;&gt;this
talk&lt;&#x2F;a&gt;, and decided I should probably
share it—or at least put it up online in case my laptop crashes. The repo is
available &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;ascb&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Time Traveling Key-Value Store, This Time in Rust</title>
        <published>2022-04-21T00:00:00+00:00</published>
        <updated>2022-04-21T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/ttkv-rs/"/>
        <id>https://grahamenos.com/ttkv-rs/</id>
        
        <content type="html" xml:base="https://grahamenos.com/ttkv-rs/">&lt;h1 id=&quot;previously&quot;&gt;Previously…&lt;&#x2F;h1&gt;
&lt;p&gt;We’ve &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;time-traveling-kv-store&#x2F;&quot;&gt;talked&lt;&#x2F;a&gt;
&lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;ttkv-again&#x2F;&quot;&gt;previously&lt;&#x2F;a&gt; about implementing a time-traveling
key-value store.
Having worked more with &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.rust-lang.org&#x2F;&quot;&gt;Rust&lt;&#x2F;a&gt; in the interim, I
tried my hand at a Rust implementation.
Rust’s type system, standard library, and attention to pedantic details make
this my favorite version yet.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementation&quot;&gt;Implementation&lt;&#x2F;h1&gt;
&lt;p&gt;The standard library provides everything we need:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;collections&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;BTreeMap&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Instant&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Ttkv&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    started&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Instant&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    mapping&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; BTreeMap&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span&gt;, (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;)&amp;gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Recording the &lt;code&gt;std::time::Instant&lt;&#x2F;code&gt; when we created our store allows us to
monotonically record insertion timestamps—on most hardware, at least—and we
can still check if this assumption is violated.&lt;&#x2F;p&gt;
&lt;p&gt;We build a new store via implementing &lt;code&gt;Default&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;impl&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Default&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Ttkv&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; default&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        Self&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            started&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Instant&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;now&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            mapping&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; BTreeMap&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Digging into the implementation (the following snippets are all wrapped in an
&lt;code&gt;impl&lt;&#x2F;code&gt; block), it’s straightforward to check for emptiness:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; is_empty&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mapping&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;is_empty&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Adding a pair to our store gives us an opportunity to assert that time is
monotonic:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; put&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;, key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; K&lt;&#x2F;span&gt;&lt;span&gt;, value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;, timestamp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; timestamp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap_or_else&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;||&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        Instant&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;now&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;checked_duration_since&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;started)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap_or_else&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;||&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; panic!&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;non-monotonic insertion&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;as_nanos&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mapping&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;insert&lt;&#x2F;span&gt;&lt;span&gt;(t, (key, value));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Retrieval from the store is similar to previous implementations:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;, timestamp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    K&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; PartialEq&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mapping&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;timestamp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;unwrap_or&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;MAX&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;(_, (k, _))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; k&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; key)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;last&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        .&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt;(_, (_, v))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; v)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, collecting the insertion times in order:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; times&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Vec&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;u128&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;mapping&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;keys&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cloned&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;collect&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;testing&quot;&gt;Testing&lt;&#x2F;h1&gt;
&lt;p&gt;Rust’s (and &lt;code&gt;cargo&lt;&#x2F;code&gt;’s) approach to testing is a pleasure to use.
We can do regular unit-style tests:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; initially_empty&lt;&#x2F;span&gt;&lt;span&gt;() {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Ttkv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    assert!&lt;&#x2F;span&gt;&lt;span&gt;(t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;is_empty&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    assert!&lt;&#x2F;span&gt;&lt;span&gt;(t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;times&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;is_empty&lt;&#x2F;span&gt;&lt;span&gt;());&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And with the help of
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;proptest&#x2F;latest&#x2F;proptest&#x2F;index.html&quot;&gt;&lt;code&gt;proptest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; we can do
property-based testing:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#[test]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; two_gets_different_keys&lt;&#x2F;span&gt;&lt;span&gt;(a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;, b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;, x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;, y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; String&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    prop_assume!&lt;&#x2F;span&gt;&lt;span&gt;(a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span&gt; b);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    let mut&lt;&#x2F;span&gt;&lt;span&gt; t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Ttkv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span&gt;();&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;put&lt;&#x2F;span&gt;&lt;span&gt;(a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(), x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;put&lt;&#x2F;span&gt;&lt;span&gt;(b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(), y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    prop_assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;times&lt;&#x2F;span&gt;&lt;span&gt;()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    prop_assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;a,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;x));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    prop_assert_eq!&lt;&#x2F;span&gt;&lt;span&gt;(t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;b,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt;y));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As a side note, this test originally failed for me in an early draft where the
timestamping mechanism wasn’t monotonic.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;fin&quot;&gt;Fin.&lt;&#x2F;h1&gt;
&lt;p&gt;This was a fun exercise, and again is probably my favorite implementation.
See &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;ttkv&#x2F;tree&#x2F;main&#x2F;ttkv_rs&quot;&gt;the repo&lt;&#x2F;a&gt; for the full
code!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Synthetic weather radar using hybrid quantum-classical machine learning</title>
        <published>2021-12-01T00:00:00+00:00</published>
        <updated>2021-12-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/paper-announcement/"/>
        <id>https://grahamenos.com/paper-announcement/</id>
        
        <content type="html" xml:base="https://grahamenos.com/paper-announcement/">&lt;h1 id=&quot;new-paper&quot;&gt;New paper&lt;&#x2F;h1&gt;
&lt;p&gt;I’m excited to announce that our paper on using quantum ML to predict
weather radar products is &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2111.15605&quot;&gt;up on the arXiv&lt;&#x2F;a&gt;!
What’s more, per the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.globenewswire.com&#x2F;news-release&#x2F;2021&#x2F;12&#x2F;01&#x2F;2344216&#x2F;0&#x2F;en&#x2F;Rigetti-Enhances-Predictive-Weather-Modeling-with-Quantum-Machine-Learning.html&quot;&gt;press
release&lt;&#x2F;a&gt;,
we’ll be presenting the paper at the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.hadr.ai&#x2F;&quot;&gt;Artificial Intelligence for Humanitarian
Assistance and Disaster Response Workshop&lt;&#x2F;a&gt; at &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;neurips.cc&#x2F;Conferences&#x2F;2021&#x2F;Schedule?showEvent=21858&quot;&gt;NeurIPS
2021&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>CvRDT Exposition in Rust</title>
        <published>2020-09-01T00:00:00+00:00</published>
        <updated>2020-09-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/cvrdt-exposition/"/>
        <id>https://grahamenos.com/cvrdt-exposition/</id>
        
        <content type="html" xml:base="https://grahamenos.com/cvrdt-exposition/">&lt;h1 id=&quot;programming-with-types-for-understanding&quot;&gt;Programming with types for understanding&lt;&#x2F;h1&gt;
&lt;p&gt;One of the best uses of the type system I’ve seen is the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;research&#x2F;publication&#x2F;build-systems-la-carte&#x2F;&quot;&gt;Build systems a la carte&lt;&#x2F;a&gt; paper.
In it, the authors use Haskell’s types to outline and explore the problem domain in really novel ways.
I wanted to understand &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Conflict-free_replicated_data_type&quot;&gt;CRDTs&lt;&#x2F;a&gt; more, so I made a thing.
It’s nowhere near the level of &lt;em&gt;Build systems a la carte&lt;&#x2F;em&gt;, but I found it useful.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;understanding-cvrdts&quot;&gt;Understanding CvRDTs&lt;&#x2F;h1&gt;
&lt;p&gt;CRDTs are important to modern distributed programming; as the above Wikipedia article explains, they are&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;data structure[s] which can be replicated across multiple computers in a network, where the replicas can be updated independently and concurrently without coordination between the replicas, and where it is always mathematically possible to resolve inconsistencies that might come up.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;In reading about CRDTs, I came across the phenomenal paper &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hal.inria.fr&#x2F;inria-00555588&#x2F;&quot;&gt;A comprehensive study of Convergent and Commutative Replicated Data Types&lt;&#x2F;a&gt;.
With this paper, Wikipedia, and some other references as my guide, I made a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;cvrdt-exposition&#x2F;&quot;&gt;Rust crate&lt;&#x2F;a&gt; to understand state-based CRDTs (a.k.a. convergent replicated data types or CvRDTs).
I really like how this crate uses two traits with associated types to describe CvRDTs, fitting them into a common framework.&lt;&#x2F;p&gt;
&lt;p&gt;The first trait is for CvRDTs that can only grow, i.e. only add items.
Here’s an slightly modified version of it; the code (on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;cvrdt-exposition&quot;&gt;GitHub&lt;&#x2F;a&gt;, with documentation on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;cvrdt-exposition&#x2F;&quot;&gt;docs.rs&lt;&#x2F;a&gt;) has more:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Grow&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Clone&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Payload&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Eq&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;                                    &#x2F;&#x2F; Internal state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Update&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;                                         &#x2F;&#x2F; Message to update internal state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Query&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;                                          &#x2F;&#x2F; Query message&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    type&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Value&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;                                          &#x2F;&#x2F; Query response&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; new&lt;&#x2F;span&gt;&lt;span&gt;(payload&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Payload&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;              &#x2F;&#x2F; Create a new version of our CvRDT from Payload&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; payload&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Payload&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;                  &#x2F;&#x2F; Retrieve Payload&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; add&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;, update&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Update&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;             &#x2F;&#x2F; Add item, mutating in-place&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; le&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, other&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; bool&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;                  &#x2F;&#x2F; Is this ≤ other in semilattice&amp;#39;s partial order?&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; merge&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, other&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;               &#x2F;&#x2F; Merge this and other into new CvRDT&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; query&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, query&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;: &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Query&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Value&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; &#x2F;&#x2F; Query to get some Value&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our rigorous typing does make for one difference from the aforementioned &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hal.inria.fr&#x2F;inria-00555588&#x2F;&quot;&gt;paper&lt;&#x2F;a&gt; and Wikipedia.
When implementing &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Conflict-free_replicated_data_type#G-Counter_(Grow-only_Counter)&quot;&gt;&lt;code&gt;GCounter&lt;&#x2F;code&gt;s&lt;&#x2F;a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Conflict-free_replicated_data_type#PN-Counter_(Positive-Negative_Counter)&quot;&gt;&lt;code&gt;PNCounter&lt;&#x2F;code&gt;s&lt;&#x2F;a&gt;, we no longer have an unspecified &lt;code&gt;myId()&lt;&#x2F;code&gt; function giving the current node’s index.
In order for the &lt;code&gt;Payload&lt;&#x2F;code&gt; type to fully specify their internal state, these classes require that index be part of the payload.&lt;&#x2F;p&gt;
&lt;p&gt;Other CvRDTs can also shrink, i.e. remove items.
Here’s a slightly modified version of &lt;code&gt;cvrdt-exposition&lt;&#x2F;code&gt;’s trait:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;pub trait&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Shrink&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Grow&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    fn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; del&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;mut&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;, update&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; Self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Update&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;             &#x2F;&#x2F; Delete item, mutating in-place&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;testing-and-verification&quot;&gt;Testing and Verification&lt;&#x2F;h1&gt;
&lt;p&gt;We have the &lt;code&gt;Eq&lt;&#x2F;code&gt; bound on the &lt;code&gt;Payload&lt;&#x2F;code&gt; type for verification and testing.
In order to verify that my CvRDT implementations behave as required, i.e. that their merge functions are commutative, associative, and idempotent, I turned to the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;proptest&quot;&gt;&lt;code&gt;proptest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; crate for property-based testing, via &lt;code&gt;assert_eq!&lt;&#x2F;code&gt; calls.
I turned to Rust’s macro system to generate some of these tests for me; see &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;cvrdt-exposition&#x2F;blob&#x2F;main&#x2F;src&#x2F;properties.rs&quot;&gt;&lt;code&gt;properties.rs&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in the GitHub source and the &lt;code&gt;test&lt;&#x2F;code&gt; sub-modules of the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;cvrdt-exposition&#x2F;tree&#x2F;main&#x2F;src&quot;&gt;implementation modules&lt;&#x2F;a&gt; for more.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Tiny REST API Demo in Python</title>
        <published>2020-08-17T00:00:00+00:00</published>
        <updated>2020-08-17T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/tiny-api-demo/"/>
        <id>https://grahamenos.com/tiny-api-demo/</id>
        
        <content type="html" xml:base="https://grahamenos.com/tiny-api-demo/">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;p&gt;The other day, someone asked me about the difference between “an API and a regular webpage.”
After understanding more about the context of their question, I tried to come up with a decent explanation about the differences between a server sending HTML pages and one handling REST API requests.
As &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;A_picture_is_worth_a_thousand_words#History&quot;&gt;a thousand words leave not the same deep impression as does a single deed&lt;&#x2F;a&gt;, I thought I’d put together a tiny demonstration that serves HTML to a web browser and JSON over a REST endpoint.
My demo uses the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flask.palletsprojects.com&#x2F;&quot;&gt;Flask&lt;&#x2F;a&gt; microframework to handle the details; you can find a copy &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;tiny_api_demo&quot;&gt;here&lt;&#x2F;a&gt;, though we’ll go through some of it below.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;how-smol&quot;&gt;How smol?&lt;&#x2F;h1&gt;
&lt;p&gt;Before we get into the specifics, here’s how tiny the demo is: the whole thing, including a &lt;code&gt;flake.nix&lt;&#x2F;code&gt; file to set the stage, is under 115 lines.
Via &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;XAMPPRocky&#x2F;tokei&quot;&gt;&lt;code&gt;tokei&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Language            Files        Lines         Code     Comments       Blanks&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; CSS                     1           10           10            0            0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; HTML                    3           41           41            0            0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Markdown                1           10            0            6            4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Nix                     1           25           23            0            2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Python                  2           54           40            2           12&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Total                   8          140          114            8           18&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;===============================================================================&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This whole thing cuts so many corners it might as well be a sphere.
It’s &lt;em&gt;certainly&lt;&#x2F;em&gt; not a production-ready example by &lt;em&gt;any&lt;&#x2F;em&gt; stretch of the imagination; it’s only enough for demonstration purposes.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;preliminaries&quot;&gt;Preliminaries&lt;&#x2F;h1&gt;
&lt;p&gt;First, we need some data.
Pretend we’re creating the &lt;em&gt;School Reporter 5000™&lt;&#x2F;em&gt;, wherein we have student information that consists of a collection of &lt;code&gt;(timestamp, person, activity)&lt;&#x2F;code&gt; tuples.
In &lt;code&gt;data.py&lt;&#x2F;code&gt;, we generate some fake data.
Our application code will interface with this data via &lt;code&gt;NAMES&lt;&#x2F;code&gt; and &lt;code&gt;SELECT&lt;&#x2F;code&gt;; pretend this stands in for a proper database.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; datetime&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; datetime, timedelta&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; itertools&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; accumulate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; operator&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; add&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; random&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;random.seed(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1729&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;NAMES&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Alice&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Bob&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Carol&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Dave&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;_ACTIVITIES&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Reading&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Writing&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Arithmetic&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;_DATA&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;quot;timestamp&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: ts.time().isoformat(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;timespec&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;minutes&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;quot;person&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: random.choice(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;NAMES&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;quot;activity&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: random.choice(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;_ACTIVITIES&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; ts&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; accumulate(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        (timedelta(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;minutes&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;random.randrange(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;17&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; _&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span&gt;)),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;        func&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;add,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;        initial&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;datetime.today().replace(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;hour&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;SELECT&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {name: [x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; _DATA&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; x[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;person&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; name]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; NAMES&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;app&quot;&gt;App&lt;&#x2F;h1&gt;
&lt;p&gt;The &lt;em&gt;School Reporter 5000™&lt;&#x2F;em&gt; has three routes, all described in &lt;code&gt;app.py&lt;&#x2F;code&gt;.
We serve HTML for the homepage at &lt;code&gt;&#x2F;index.html&lt;&#x2F;code&gt; and individual person pages at &lt;code&gt;&#x2F;person&#x2F;&amp;lt;name&amp;gt;&lt;&#x2F;code&gt; for each of the names in &lt;code&gt;NAMES&lt;&#x2F;code&gt;.
We also serve JSON in response to GET requests via the &lt;code&gt;&#x2F;api&#x2F;&amp;lt;name&amp;gt;&lt;&#x2F;code&gt; route.
Thanks to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;flask.palletsprojects.com&#x2F;&quot;&gt;Flask&lt;&#x2F;a&gt;, we even have rudimentary error handling.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; flask&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Flask, jsonify, render_template&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; NAMES&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; SELECT&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;app&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Flask(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;__name__&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;@app.route&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&#x2F;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; home&lt;&#x2F;span&gt;&lt;span&gt;():&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; render_template(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;index.html&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; names&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;NAMES&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;@app.route&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&#x2F;person&#x2F;&amp;lt;string:name&amp;gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; person&lt;&#x2F;span&gt;&lt;span&gt;(name):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    try&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; render_template(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;person.html&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;name,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;SELECT&lt;&#x2F;span&gt;&lt;span&gt;[name],&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    except&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; KeyError&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; render_template(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;person.html&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;name,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; error&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 400&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;@app.route&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&#x2F;api&#x2F;&amp;lt;string:name&amp;gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; api&lt;&#x2F;span&gt;&lt;span&gt;(name):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    try&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; jsonify(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;SELECT&lt;&#x2F;span&gt;&lt;span&gt;[name])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    except&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; KeyError&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;error&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Unknown person &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;},&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 400&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With the server running, we can visit the home page:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;tiny_api_demo_index.png&quot; alt=&quot;Homepage&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Or report pages for any specific student:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;tiny_api_demo_alice.png&quot; alt=&quot;Alice’s page&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;However, we can also get JSON data for individual students by pinging the REST endpoint; using, e.g. &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;httpie.org&quot;&gt;HTTPie&lt;&#x2F;a&gt; to query the endpoint via &lt;code&gt;http --body :5000&#x2F;api&#x2F;Dave&lt;&#x2F;code&gt;, we get&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;images&#x2F;tiny_api_demo_dave.png&quot; alt=&quot;Dave’s JSON&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;everything-else&quot;&gt;Everything Else&lt;&#x2F;h1&gt;
&lt;p&gt;To round out the example, we also have &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;tiny_api_demo&#x2F;blob&#x2F;main&#x2F;static&#x2F;styles.css&quot;&gt;some minimal CSS&lt;&#x2F;a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;tiny_api_demo&#x2F;blob&#x2F;main&#x2F;templates&quot;&gt;two other HTML templates&lt;&#x2F;a&gt;.
Here’s the &lt;code&gt;flake.nix&lt;&#x2F;code&gt; to specify the W O R L D:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;nix&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  description&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Tiny API Demo&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:numtide&#x2F;flake-utils&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:NixOS&#x2F;nixpkgs&#x2F;nixos-22.11&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  outputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;lib&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;eachDefaultSystem&lt;&#x2F;span&gt;&lt;span&gt; (system:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; let&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; nixpkgs&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;inherit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; system&lt;&#x2F;span&gt;&lt;span&gt;;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      py&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;python310&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;withPackages&lt;&#x2F;span&gt;&lt;span&gt; (p: [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;flask&lt;&#x2F;span&gt;&lt;span&gt;]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    in&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      packages&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;writeShellApplication&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;tiny_api_demo&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        text&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;          FLASK_APP=app.py ${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;py&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}&#x2F;bin&#x2F;python -m flask run&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;From the base directory, one can run &lt;code&gt;nix run&lt;&#x2F;code&gt; to set up the environment and kick off the server.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>PostgreSQL Makes Delicious Dog Food</title>
        <published>2020-06-23T00:00:00+00:00</published>
        <updated>2020-06-23T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/psql-dogfood/"/>
        <id>https://grahamenos.com/psql-dogfood/</id>
        
        <content type="html" xml:base="https://grahamenos.com/psql-dogfood/">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;p&gt;It’s probably not news to you, but folks behind the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.postgresql.org&quot;&gt;PostgreSQL
Database&lt;&#x2F;a&gt; have made an &lt;em&gt;incredible&lt;&#x2F;em&gt; piece of
software. It’s fast, it can handle loads of data, and it has interesting
builtin functions and index types for almost anything you might need. For this
post, though, I’d like to highlight how it facilitates DB maintenance tasks by
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Eating_your_own_dog_food&quot;&gt;eating its own dog
food&lt;&#x2F;a&gt;—that is, how
answering questions &lt;em&gt;about&lt;&#x2F;em&gt; the database is no different from running regular
SQL queries.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;problem-statement&quot;&gt;Problem statement&lt;&#x2F;h1&gt;
&lt;p&gt;At &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.quantifind.com&quot;&gt;work&lt;&#x2F;a&gt;, we’ve got a fair amount of data. I’m
charged with the care and feeding of a couple of these databases, one of which
recently got a &lt;em&gt;huge&lt;&#x2F;em&gt; upgrade of incoming data. Previously it contained
our data vendor’s restricted version of US-and-Canada-only data; we switched to
the &lt;code&gt;WHOLE_DANG_WORLD&lt;&#x2F;code&gt; option.&lt;&#x2F;p&gt;
&lt;p&gt;When we (I) created our DB schema, we relied heavily upon materialized views;
the thought was we’d keep the incoming raw data in tables, perform some
DB-level ETL work, then put the main data we wish to use into tidy,
consolidated materialized views. This design made sense given what we thought
our paradigm was going to be: we were expecting&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;raw data that needed extensive work before being suitable for processing, and&lt;&#x2F;li&gt;
&lt;li&gt;we would receive small incremental updates of that data frequently from our vendor, so we could &lt;code&gt;UPDATE&lt;&#x2F;code&gt; the raw tables and &lt;code&gt;REFRESH&lt;&#x2F;code&gt; the materialized views to stay up to date.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;However, this is decidedly not the paradigm in which we find ourselves. The
raw data is almost ready for our ML algorithms to grab, modulo some
normalizations, and instead of frequent small updates we receive quarterly
dumps of the &lt;em&gt;entire&lt;&#x2F;em&gt; database. This led to a lot of waste, but there was one
particular pain point: because we used materialized views, when our intrepid
Ops team copied the DB over to other environments (e.g. stage &amp;amp; prod), they had
to &lt;em&gt;rebuild every index&lt;&#x2F;em&gt; on the materialized views—unlike a table index, an
index on a materialized view doesn’t copy over.&lt;&#x2F;p&gt;
&lt;p&gt;I had a strong suspicion that we didn’t need every index we were copying over;
over half are obsolete artifacts of query patterns we no longer used. I thought
of combing through our Scala monolith to find every time we accessed these
materialized views via &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;scala-slick.org&quot;&gt;Slick&lt;&#x2F;a&gt;, but calling that
error-prone would be an understatement. Part of the appeal of Slick is that it
turns normal-looking Scala code into SQL queries for you, but that means you
need to translate it—either in code by, say, debug logging the query statements
or in your head—to fully understand what it’s doing with your DB.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;it-s-tables-all-the-way-down&quot;&gt;It’s tables all the way down&lt;&#x2F;h1&gt;
&lt;p&gt;Luckily, the PostgreSQL devs have made answering these types of questions
manageable. One answers questions about PostgreSQL by, well, querying PostgreSQL.
There are a bevy of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;9.6&#x2F;catalogs-overview.html&quot;&gt;system
catalogs&lt;&#x2F;a&gt; and
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;12&#x2F;monitoring-stats.html#MONITORING-STATS-VIEWS-TABLE&quot;&gt;collected statistics
views&lt;&#x2F;a&gt;
chock-full of helpful information.&lt;&#x2F;p&gt;
&lt;p&gt;Trimming down an example query from the wiki page on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;wiki.postgresql.org&#x2F;wiki&#x2F;Index_Maintenance&quot;&gt;index
maintenance&lt;&#x2F;a&gt;, I ran the
following to get the names, table names, and human-readable sizes of the
indexes that were&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;public (&lt;code&gt;d.schemaname = &#x27;public&#x27;&lt;&#x2F;code&gt;), meaning we made them,&lt;&#x2F;li&gt;
&lt;li&gt;not unique (&lt;code&gt;not a.indisunique&lt;&#x2F;code&gt;), so they’re not primary keys&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, and&lt;&#x2F;li&gt;
&lt;li&gt;never scanned (&lt;code&gt;d.idx_scan = 0&lt;&#x2F;code&gt;), so they’re unused:&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;select&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  c&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;relname&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; index_name,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  b&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;relname&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; table_name,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  pg_size_pretty(pg_relation_size(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    quote_ident(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;schemaname&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;||&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;.&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ||&lt;&#x2F;span&gt;&lt;span&gt; quote_ident(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;indexrelname&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )) &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;as&lt;&#x2F;span&gt;&lt;span&gt; index_size&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; pg_index a&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt; pg_class b &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;on&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; b&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;oid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;indrelid&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt; pg_class c &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;on&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; c&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;oid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;indexrelid&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt; pg_stat_all_indexes d &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;on&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;indexrelid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; d&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;indexrelid&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; d&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;schemaname&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;public&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; and not&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; a&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;indisunique&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; and&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; d&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;idx_scan&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;order by&lt;&#x2F;span&gt;&lt;span&gt; table_name, index_name;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When run, this query immediately returns something like&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|   index_name   |   table_name   |   index_size   |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|----------------|----------------|----------------|&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|   unused_idx   |   a_table      |   1.81 GB      |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;|   also_bad     |   another_tbl  |   77 MB        |&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With this helpful query in hand, I knew exactly which indexes we could safely
delete and how much memory we’d save the database by doing so.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;Or the approximate equivalent for materialized views, which can’t have primary indexes, but can have unique indexes. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;Names changed to protect the guilty. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Silly Shenanigans</title>
        <published>2019-08-14T00:00:00+00:00</published>
        <updated>2019-08-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/shenanigans/"/>
        <id>https://grahamenos.com/shenanigans/</id>
        
        <content type="html" xml:base="https://grahamenos.com/shenanigans/">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;p&gt;In an effort to both post more than once a year and clean out some silly things
from my &lt;code&gt;~&#x2F;tmp&lt;&#x2F;code&gt; directory, here’s a quick post about two programming
shenanigans.
Perhaps unsurprisingly due to its history of jocularity, both originate with
Perl.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;bleach&quot;&gt;Bleach&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Damian_Conway&quot;&gt;Damian Conway&lt;&#x2F;a&gt; is a truly prolific
member of the Perl community.
One of Conway’s less serious contributions is the
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;metacpan.org&#x2F;pod&#x2F;Acme::Bleach&quot;&gt;&lt;code&gt;Acme::Bleach&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; module, famous for
being the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.perlmonks.org&#x2F;?node_id=967004&quot;&gt;first &lt;code&gt;Acme&lt;&#x2F;code&gt; module and the first source filter joke
module.&lt;&#x2F;a&gt;
&lt;code&gt;Acme::Bleach&lt;&#x2F;code&gt; takes programs that look like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;perl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span&gt; Acme::Bleach;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Hello world&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and turns them into&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;perl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;use&lt;&#x2F;span&gt;&lt;span&gt; Acme::Bleach;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	 	 	 	 	 	 	 			   	   &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 		 		 	 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	 		  		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	 		 	 	 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 		      &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  	   		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;       	 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 			 			 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 			 	   &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   			   &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		 		  	 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	        &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			  	  	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		 	  	 	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  			 		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   	 			 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     	   &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	   	    &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	  	 	 	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		   		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 		   		 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		 				 	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	      	 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 			 			 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;				 		  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	  			   &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;		 		   	&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  		  	  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; 	  		 		&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	   	 	  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;No, that’s not an error.
Through the magic of Perl’s &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;perldoc.perl.org&#x2F;perlfilter.html&quot;&gt;source
filters&lt;&#x2F;a&gt;, the above program whose
source consists of nothing but one import statement and a bunch of whitespace
&lt;em&gt;still prints “Hello world”!&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;After digging through the source, I decided to try my hand at recreating it in
Python:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;#!&#x2F;usr&#x2F;bin&#x2F;env python3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;For really clean programs&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; functools&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; reduce&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; pathlib&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Path&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; subprocess&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; run&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; sys&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; argv&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;ZERO&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ord&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot; &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;ONE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ord&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\t&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; zero_one&lt;&#x2F;span&gt;&lt;span&gt;(x:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Convert a byte to a series of `ZERO` and `ONE`&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;ZERO&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ONE&lt;&#x2F;span&gt;&lt;span&gt;][(x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; i)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; un_zero_one&lt;&#x2F;span&gt;&lt;span&gt;(xs:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Undo `zero_one`&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; reduce&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;lambda&lt;&#x2F;span&gt;&lt;span&gt; i, x: (i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;(x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ONE&lt;&#x2F;span&gt;&lt;span&gt;), xs,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; encode&lt;&#x2F;span&gt;&lt;span&gt;(xs:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Use `zero_one` to encode&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.join(zero_one(x)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; xs)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; decode&lt;&#x2F;span&gt;&lt;span&gt;(xs:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Use `un_zero_one` to decode&amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; bytes&lt;&#x2F;span&gt;&lt;span&gt;(un_zero_one(xs[i : i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; len&lt;&#x2F;span&gt;&lt;span&gt;(xs),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __name__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;__main__&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; len&lt;&#x2F;span&gt;&lt;span&gt;(argv)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        exit&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;I need a file to bleach&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        XS&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Path(argv[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]).read_bytes()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; any&lt;&#x2F;span&gt;&lt;span&gt;(x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; not in&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;ZERO&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ONE&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; XS&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            print&lt;&#x2F;span&gt;&lt;span&gt;(encode(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;XS&lt;&#x2F;span&gt;&lt;span&gt;).decode(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;ascii&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; end&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; flush&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            run([&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;python3&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;-&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; input&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;decode(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;XS&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since Python doesn’t have Perl’s source filters, we can’t do everything that
&lt;code&gt;Acme::Bleach&lt;&#x2F;code&gt; does, but &lt;code&gt;bleach.py&lt;&#x2F;code&gt; uses the same main trick: source code
consists of bytes, as far as computers care, so we can encode and decode them
however we want—including using spaces for zeros and tabs for ones.
If we have a file called &lt;code&gt;simple.py&lt;&#x2F;code&gt; consisting of &lt;code&gt;print(&quot;hello world&quot;)&lt;&#x2F;code&gt;,
we can say &lt;code&gt;.&#x2F;bleach.py simple.py &amp;gt; simple_bleached.py&lt;&#x2F;code&gt;.
Then running &lt;code&gt;.&#x2F;bleach.py simple_bleached.py&lt;&#x2F;code&gt; will result in the interpreter
printing &lt;code&gt;&quot;hello world&quot;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;d-d&quot;&gt;D&amp;amp;D&lt;&#x2F;h1&gt;
&lt;p&gt;I recently came across a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;datakinds.github.io&#x2F;2019&#x2F;08&#x2F;07&#x2F;d-d-rolls-in-perl-6&quot;&gt;fun
post&lt;&#x2F;a&gt; that describes
implementing basic dice notation from Dungeons &amp;amp; Dragons in Perl 6.
It’s a fun read, so you should definitely check it out, but its basic
implementation looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;perl&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;sub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; infix&lt;&#x2F;span&gt;&lt;span&gt;:&amp;lt;d&amp;gt;(Int $n, Int $max) { (1..$max).pick xx $n }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So one can enter &lt;code&gt;say (3 d 20);&lt;&#x2F;code&gt; and see something like &lt;code&gt;(17 1 12)&lt;&#x2F;code&gt; printed.
Haskell has nice support for infix-ing any function by wrapping it in
backticks, so I came up with the following &lt;code&gt;dnd.hs&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;#!&#x2F;usr&#x2F;bin&#x2F;env stack&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;-- stack --install-ghc runghc --package random --resolver lts-14.0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Main&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Data.Foldable&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;traverse_&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; System.Random&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;getStdGen&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; randomRs&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Word -&amp;gt; Word -&amp;gt; IO&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Word&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;d n size &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; take (fromIntegral n)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; .&lt;&#x2F;span&gt;&lt;span&gt; randomRs (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, size)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;$&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; getStdGen&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: IO&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; traverse_ (print &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;) [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `d`&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 8&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `d`&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 20&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `d`&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 6&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running it via &lt;code&gt;.&#x2F;dnd.hs&lt;&#x2F;code&gt;&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, I got&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Though of course different runs will probably output different results.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;Assuming of course that you’ve got &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Stack_(Haskell)&quot;&gt;&lt;code&gt;stack&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; installed. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Time Traveling Key-Value Store, Again</title>
        <published>2019-08-05T00:00:00+00:00</published>
        <updated>2019-08-05T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/ttkv-again/"/>
        <id>https://grahamenos.com/ttkv-again/</id>
        
        <content type="html" xml:base="https://grahamenos.com/ttkv-again/">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;p&gt;In my &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;time-traveling-kv-store&#x2F;&quot;&gt;last post&lt;&#x2F;a&gt; I talked
about a time traveling key-value store implementation in some functional Scala.
After thinking about the problem more—&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;xkcd.com&#x2F;356&#x2F;&quot;&gt;probably a little too
much&lt;&#x2F;a&gt;—I decided to try my hand at another
implementation, this time in Python again.
Rather than going the purely functional route, though, I wanted to focus on how
we represent the data internally.
Since we essentially have tabular data with three columns (timestamp, key, and
value), it made some sense to try and work with a table via the &lt;code&gt;sqlite3&lt;&#x2F;code&gt;
module in the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;sqlite3.html&quot;&gt;Python standard
library&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementation&quot;&gt;Implementation&lt;&#x2F;h1&gt;
&lt;p&gt;First, some imports:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; pickle&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; dumps, loads&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; sqlite3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Row, connect&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; time&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; perf_counter&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; typing&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Any, List, Optional&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We use the &lt;code&gt;pickle&lt;&#x2F;code&gt; module to marshal (almost) arbitrary keys and values to
binary blobs, &lt;code&gt;time&lt;&#x2F;code&gt; to give us a global clock as in my in-the-room
implementation from the interview, and &lt;code&gt;typing&lt;&#x2F;code&gt; to label our types.&lt;&#x2F;p&gt;
&lt;p&gt;Since I wanted to wrap all this up with a stable release of
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;nixos.org&#x2F;nix&#x2F;&quot;&gt;&lt;code&gt;nix&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, I needed to use Python 3.6; this meant that
&lt;code&gt;time.perf_counter_ns()&lt;&#x2F;code&gt; wasn’t available, so we make our own out of
&lt;code&gt;time.perf_counter()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; perf_counter_ns&lt;&#x2F;span&gt;&lt;span&gt;() -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;(perf_counter()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1e9&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If one feels really strongly about Python 3.7, one could use the unstable
&lt;code&gt;nixpkgs&lt;&#x2F;code&gt; channel.&lt;&#x2F;p&gt;
&lt;p&gt;Next we create a &lt;code&gt;TTKV&lt;&#x2F;code&gt; class:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; TTKV&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(self) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.conn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; connect(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;:memory:&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.conn.row_factory&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Row&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.cursor&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.conn.cursor()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            &amp;quot;create table ttkv (timestamp integer, key blob, value blob)&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;create index timestamp_index on ttkv(timestamp)&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;create index key_index on ttkv(key)&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.conn.commit()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using this in-memory &lt;code&gt;sqlite&lt;&#x2F;code&gt; db, &lt;code&gt;put()&lt;&#x2F;code&gt; is an &lt;code&gt;INSERT&lt;&#x2F;code&gt; statement:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; put&lt;&#x2F;span&gt;&lt;span&gt;(self, key: Any, value: Any) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            &amp;quot;insert into ttkv values (?, ?, ?)&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            (perf_counter_ns(), dumps(key), dumps(value)),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;While a &lt;code&gt;SELECT&lt;&#x2F;code&gt; statement takes care of &lt;code&gt;get()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; get&lt;&#x2F;span&gt;&lt;span&gt;(self, key: Any, timestamp: Optional[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; Any:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        select&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;select * from ttkv where key = ?&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        order&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;order by timestamp desc&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; timestamp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; is&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;select&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;order&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, (dumps(key),))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        elif&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; isinstance&lt;&#x2F;span&gt;&lt;span&gt;(timestamp,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;                f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;select&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; and timestamp &amp;lt;= ? &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;order&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, (dumps(key), timestamp)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            raise&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ValueError&lt;&#x2F;span&gt;&lt;span&gt;(timestamp)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.fetchone()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; result&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; is&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            raise&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; KeyError&lt;&#x2F;span&gt;&lt;span&gt;(key)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; loads(result[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;value&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To be more Pythonic, &lt;code&gt;get()&lt;&#x2F;code&gt; throws errors if given an unknown key or a
non-integral timestamp rather than going the Scala version’s &lt;code&gt;Option&lt;&#x2F;code&gt; route.&lt;&#x2F;p&gt;
&lt;p&gt;Finally, the &lt;code&gt;times()&lt;&#x2F;code&gt; helper returns relevant timestamps like in the Scala
version:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; times&lt;&#x2F;span&gt;&lt;span&gt;(self, key: Any&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; List[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;]:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        select&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;select timestamp from ttkv&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        order&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;order by timestamp desc&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; key&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; is&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;select&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;} {&lt;&#x2F;span&gt;&lt;span&gt;order&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;            self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.execute(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;select&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; where key = ? &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{&lt;&#x2F;span&gt;&lt;span&gt;order&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, (dumps(key),))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; [row[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;timestamp&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; row&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.cursor.fetchall()]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;testing&quot;&gt;Testing&lt;&#x2F;h1&gt;
&lt;p&gt;My
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;ttkv&#x2F;blob&#x2F;main&#x2F;ttkv_py&#x2F;ttkv_spec.py&quot;&gt;&lt;code&gt;ttkv_spec.py&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
looks similar to the previous
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;ttkv&#x2F;blob&#x2F;main&#x2F;ttkv_sc&#x2F;src&#x2F;test&#x2F;scala&#x2F;ttkv&#x2F;TTKVSpec.scala&quot;&gt;&lt;code&gt;TTKVSpec.scala&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
using the wonderful &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hypothesis.readthedocs.io&#x2F;en&#x2F;latest&#x2F;&quot;&gt;&lt;code&gt;hypothesis&lt;&#x2F;code&gt;
library&lt;&#x2F;a&gt; in place of &lt;code&gt;scalacheck&lt;&#x2F;code&gt;
for property testing.
I also lean on &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.pytest.org&#x2F;en&#x2F;latest&#x2F;&quot;&gt;&lt;code&gt;pytest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for running
the tests and for handling (deliberate) exceptions.
I’ll copy just three tests here; interested readers should check the source on
GitHub for the rest.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;@given&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;STR&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; test_initially_empty&lt;&#x2F;span&gt;&lt;span&gt;(a:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; str&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    with&lt;&#x2F;span&gt;&lt;span&gt; raises(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;KeyError&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        TTKV().get(a)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;@given&lt;&#x2F;span&gt;&lt;span&gt;(distinct_keys(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; INT&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; INT&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; test_two_gets_different_keys&lt;&#x2F;span&gt;&lt;span&gt;(ab: Tuple[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; str&lt;&#x2F;span&gt;&lt;span&gt;], x:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;, y:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    a, b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ab&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ttkv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; TTKV()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ttkv.put(a, x)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ttkv.put(b, y)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    assert&lt;&#x2F;span&gt;&lt;span&gt; ttkv.get(a)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    assert&lt;&#x2F;span&gt;&lt;span&gt; ttkv.get(b)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;@given&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;STR&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; INT&lt;&#x2F;span&gt;&lt;span&gt;, integers(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;min_value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; max_value&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;SQLITE_MAX_INT&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; test_get_before_time&lt;&#x2F;span&gt;&lt;span&gt;(a:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; str&lt;&#x2F;span&gt;&lt;span&gt;, x:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;, t:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ttkv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; TTKV()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ttkv.put(a, x)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    t0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ttkv.times(a)[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    with&lt;&#x2F;span&gt;&lt;span&gt; raises(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;KeyError&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        TTKV().get(a, t0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; t)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;thoughts&quot;&gt;Thoughts&lt;&#x2F;h1&gt;
&lt;p&gt;I’m of two minds here.
Like the in-the-interview version I mentioned in my last post, this code relies
heavily on side-effects, using  its global nanosecond clock for timestamp
generation.
I liked how the functional Scala version explicitly isolated the effectful
&lt;code&gt;put()&lt;&#x2F;code&gt; computation.
However, I like how easily we map the &lt;code&gt;TTKV&lt;&#x2F;code&gt; API onto the &lt;code&gt;sqlite&lt;&#x2F;code&gt; table; the
&lt;code&gt;ORDER BY&lt;&#x2F;code&gt; clauses obviate the need to sort things, e.g.&lt;&#x2F;p&gt;
&lt;p&gt;It also seems that if the spec for &lt;code&gt;TTKV&lt;&#x2F;code&gt; changes at all (as software specs are
wont to do), we could easily modify this approach.
It also suggests using multiple data backends, wherein we might define an
abstract API and connect it to different specific data structures depending on
our needs.&lt;&#x2F;p&gt;
&lt;p&gt;As a happy accident of using the &lt;code&gt;pickle&lt;&#x2F;code&gt; module, this &lt;code&gt;TTKV&lt;&#x2F;code&gt; implementation
can use more types of things as keys than even the base &lt;code&gt;dict&lt;&#x2F;code&gt; class (e.g.
lists).
There are &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;pickle.html#what-can-be-pickled-and-unpickled&quot;&gt;restrictions on what can be
pickled&lt;&#x2F;a&gt;,
of course, and this implementation doesn’t handle any &lt;code&gt;PicklingError&lt;&#x2F;code&gt;s that
might occur if a use tries to save particular exotic keys or values.&lt;&#x2F;p&gt;
&lt;p&gt;I’m a &lt;em&gt;huge&lt;&#x2F;em&gt; fan of
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hypothesis.readthedocs.io&#x2F;en&#x2F;latest&#x2F;&quot;&gt;&lt;code&gt;hypothesis&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;,
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.pytest.org&#x2F;en&#x2F;latest&#x2F;&quot;&gt;&lt;code&gt;pytest&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, and
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;typing.html&quot;&gt;&lt;code&gt;typing&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; along with
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;mypy-lang.org&quot;&gt;&lt;code&gt;mypy&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;; I’d go so far as to say that I’d be reticent to
develop serious software in Python without them.
One (perhaps) interesting thing that happened in testing: though the &lt;code&gt;sqlite3&lt;&#x2F;code&gt;
module’s documentation claims that one can make arbitrary Python integers into
native &lt;code&gt;sqlite&lt;&#x2F;code&gt; integer values, &lt;code&gt;hypothesis&lt;&#x2F;code&gt; quickly exposed a flaw in that
claim, running into &lt;code&gt;OverflowError&lt;&#x2F;code&gt;s when trying to save integral values too
large to fit into 64 bits.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;appendix&quot;&gt;Appendix&lt;&#x2F;h1&gt;
&lt;p&gt;Here’s the &lt;code&gt;flake.nix&lt;&#x2F;code&gt; file to specify the W O R L D:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;nix&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  description&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Python TTKV&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:numtide&#x2F;flake-utils&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:NixOS&#x2F;nixpkgs&#x2F;nixos-23.11&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  outputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;lib&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;eachDefaultSystem&lt;&#x2F;span&gt;&lt;span&gt; (system:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; let&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; nixpkgs&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;inherit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; system&lt;&#x2F;span&gt;&lt;span&gt;;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      python&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;python311&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;withPackages&lt;&#x2F;span&gt;&lt;span&gt; (p: [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;hypothesis p&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;pytest&lt;&#x2F;span&gt;&lt;span&gt;]);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    in&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      packages&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;writeShellApplication&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;ttkv&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        text&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;          ${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;python&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}&#x2F;bin&#x2F;python -mpytest ttkv_spec.py&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To run the tests yourself, poke around in the code, etc. feel free to copy the
whole &lt;code&gt;ttkv_py&lt;&#x2F;code&gt; directory from my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;ttkv&#x2F;tree&#x2F;main&#x2F;ttkv_py&quot;&gt;programming workbench on
GitHub.&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>A Time Traveling Key-Value Store</title>
        <published>2019-04-16T00:00:00+00:00</published>
        <updated>2019-04-16T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/time-traveling-kv-store/"/>
        <id>https://grahamenos.com/time-traveling-kv-store/</id>
        
        <content type="html" xml:base="https://grahamenos.com/time-traveling-kv-store/">&lt;h1 id=&quot;never-give-up&quot;&gt;Never Give Up&lt;&#x2F;h1&gt;
&lt;p&gt;Once upon a time I interviewed for an ML&#x2F;DS&#x2F;software engineering position.
I didn’t get the gig (as the inimitable Time Hopper says, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;tdhopper&#x2F;status&#x2F;711948018224799746&quot;&gt;never give
up&lt;&#x2F;a&gt;), but came away
with a fun programming exercise.
In the room, I put together some reasonable Python to solve and test it, but I
wanted to try my hand at using
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;typelevel.org&#x2F;cats-effect&#x2F;&quot;&gt;&lt;code&gt;cats-effect&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to work through this in
some functional Scala.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-is-your-problem-and-why-is-it-interesting&quot;&gt;What is Your Problem and Why is it Interesting?&lt;&#x2F;h1&gt;
&lt;p&gt;Your mission, should you choose to accept it, is to implement (and test) a data
structure that looks a bit like a dictionary&#x2F;map&#x2F;hash&#x2F;associative
array&#x2F;whatever your language calls it, but with a twist.
In pseudo-Scala, your &lt;em&gt;Time Traveling Key-Value Store&lt;&#x2F;em&gt; (TTKV from now on)
should have two methods that look a bit like the following (note that we change
this a little in our implementation for functional programming goodness):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; put&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Unit&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Long&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Calling &lt;code&gt;put&lt;&#x2F;code&gt; should store the &lt;code&gt;(key, value)&lt;&#x2F;code&gt; pair in your TTKV.
Calling &lt;code&gt;get&lt;&#x2F;code&gt;, however, has some interesting behavior.
If you don’t supply an optional &lt;code&gt;time&lt;&#x2F;code&gt;, it returns the latest &lt;code&gt;value&lt;&#x2F;code&gt; you stored
with the &lt;code&gt;key&lt;&#x2F;code&gt;.
If you do supply a &lt;code&gt;time&lt;&#x2F;code&gt;, TTKV returns the &lt;code&gt;value&lt;&#x2F;code&gt; it had for &lt;code&gt;key&lt;&#x2F;code&gt; at that
&lt;code&gt;time&lt;&#x2F;code&gt;.
For instance, if you first call &lt;code&gt;TTKV.put(&quot;a&quot;, 1)&lt;&#x2F;code&gt; at time 3, then later at
time 7 you call &lt;code&gt;TTKV.put(&quot;a&quot;, 2)&lt;&#x2F;code&gt;, then&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;TTKV.get(&quot;a&quot;)&lt;&#x2F;code&gt; should be &lt;code&gt;Some(2)&lt;&#x2F;code&gt;,&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TTKV.get(&quot;a&quot;, Some(6))&lt;&#x2F;code&gt; should be 1, since 6 is after 3 but before 7,&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TTKV.get(&quot;a&quot;, Some(n))&lt;&#x2F;code&gt; should be &lt;code&gt;Some(2)&lt;&#x2F;code&gt; for any &lt;code&gt;n ≥ 7&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TTKV.get(&quot;a&quot;, Some(n))&lt;&#x2F;code&gt; should be &lt;code&gt;None&lt;&#x2F;code&gt; for any &lt;code&gt;n &amp;lt; 3&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;TTKV.get(x, y)&lt;&#x2F;code&gt; should be &lt;code&gt;None&lt;&#x2F;code&gt; for any other &lt;code&gt;x&lt;&#x2F;code&gt; and &lt;code&gt;y&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;My Python in-the-room implementation relied on side-effects, reading a global
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3.7&#x2F;library&#x2F;time.html#time.process_time_ns&quot;&gt;nanosecond
clock.&lt;&#x2F;a&gt;
This was less than ideal, and though it met the remit, it wasn’t easily
testable—which I suspect was the point of the exercise.&lt;&#x2F;p&gt;
&lt;p&gt;By its nature, &lt;code&gt;put&lt;&#x2F;code&gt; involves an effect: reading a time.
To do things functionally, I’ll turn to &lt;code&gt;cats-effect&lt;&#x2F;code&gt; and explicitly make note
of this effectful computation.
This involves a slight change to TTKV’s API; while &lt;code&gt;get&lt;&#x2F;code&gt; stays the same, we’ll
change &lt;code&gt;put&lt;&#x2F;code&gt; to the following:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; put&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[_]](&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;implicit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; sync&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Sync&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;clock&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Clock&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;TTKV&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First, rather than mutating &lt;code&gt;TTKV&lt;&#x2F;code&gt; in place, &lt;code&gt;put&lt;&#x2F;code&gt; returns a new one with the
&lt;code&gt;(key, value)&lt;&#x2F;code&gt; pair added for a specific time.
Second, we’re explicitly noting that this work takes place inside a &lt;code&gt;Clock&lt;&#x2F;code&gt;
effect, from which we grab the current time.
I like how we can still &lt;code&gt;get&lt;&#x2F;code&gt; the same way as before, since querying a TTKV
shouldn’t have to concern itself with effects, but this design specifically
highlights that &lt;code&gt;put&lt;&#x2F;code&gt; is different.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementation&quot;&gt;Implementation&lt;&#x2F;h1&gt;
&lt;p&gt;Here’s my implementation in its entirety (again, head to
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;ttkv&#x2F;tree&#x2F;main&#x2F;ttkv_sc&quot;&gt;GitHub&lt;&#x2F;a&gt; to
see it in action):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;package&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; ttkv&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; cats&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;effect&lt;&#x2F;span&gt;&lt;span&gt;.{&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Clock&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Sync&lt;&#x2F;span&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; cats&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;implicits&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;_&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; alleycats&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;std&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;iterable&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;_&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; scala&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;collection&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;immutable&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;LongMap&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;case class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; TTKV&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;](&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;inner&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Map&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;LongMap&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;]])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; extends&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; AnyVal&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; put&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;[_]](&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; V&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;               (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;implicit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; sync&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Sync&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; F&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;TTKV&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    Clock&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;F&lt;&#x2F;span&gt;&lt;span&gt;].monotonic.map { t &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;      val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; m&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; inner.getOrElse(key, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;LongMap&lt;&#x2F;span&gt;&lt;span&gt;.empty)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; (t.toNanos &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; value)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      TTKV&lt;&#x2F;span&gt;&lt;span&gt;(inner &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; (key &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; m))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; get&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;time&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Long&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Option&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      m &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; inner.get(key)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      keys &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; m.keys.filter(_ &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; time.getOrElse(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Long&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;MaxValue&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      t0 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; keys.maximumOption&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      value &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; m.get(t0)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; yield&lt;&#x2F;span&gt;&lt;span&gt; value&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; times&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; K&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Long&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    inner.get(key).map(_.keys.toList.sorted).getOrElse(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Nil&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; times&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; List&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Long&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    inner.keys.toList.flatMap(times).sorted&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; TTKV&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; empty&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; TTKV&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;K&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;V&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; TTKV&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Map&lt;&#x2F;span&gt;&lt;span&gt;.empty)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;will-it-blend&quot;&gt;Will it Blend?&lt;&#x2F;h1&gt;
&lt;p&gt;In the room, I wrote some simple unit tests for my Python implementation.
For this functional version, I put together some property-based tests with
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.scalacheck.org&quot;&gt;&lt;code&gt;ScalaCheck&lt;&#x2F;code&gt;.&lt;&#x2F;a&gt;
You can find the entirety on
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;tree&#x2F;main&#x2F;ttkv_sc&quot;&gt;GitHub&lt;&#x2F;a&gt;; I’ll
copy some highlights here.&lt;&#x2F;p&gt;
&lt;p&gt;First off, calling &lt;code&gt;get&lt;&#x2F;code&gt; on an empty TTKV should return &lt;code&gt;None&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;property(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;initially empty&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; forAll {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Char&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    TTKV&lt;&#x2F;span&gt;&lt;span&gt;.empty.get(a)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Similarly, calling &lt;code&gt;get&lt;&#x2F;code&gt; immediately after a &lt;code&gt;put&lt;&#x2F;code&gt; should return the same thing
you &lt;code&gt;put&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;property(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;single get&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; forAll {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Char&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    TTKV&lt;&#x2F;span&gt;&lt;span&gt;.empty.put(a, x).map(_.get(a)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(x)).unsafeRunSync&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;More importantly, calling &lt;code&gt;get&lt;&#x2F;code&gt; with any time prior to when you &lt;code&gt;put&lt;&#x2F;code&gt; a thing
should return &lt;code&gt;None&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;property(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;get before time&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; forAll {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Char&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;t&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Long&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    (t &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      TTKV&lt;&#x2F;span&gt;&lt;span&gt;.empty&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .put(a, x)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .map { m &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;          val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; t0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; m.times(a).head&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          m.get(a, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span&gt;(t0 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; t))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }.unsafeRunSync&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And for two &lt;code&gt;puts&lt;&#x2F;code&gt; with the same key, calling &lt;code&gt;get&lt;&#x2F;code&gt; with any time &lt;em&gt;between&lt;&#x2F;em&gt; the
two times should return the value from the &lt;em&gt;first&lt;&#x2F;em&gt; &lt;code&gt;put&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;property(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;middle get&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; forAll {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;a&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Char&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;y&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Int&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;d&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Double&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;=&lt;&#x2F;span&gt;&lt;span&gt; d)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;amp;&amp;amp;&lt;&#x2F;span&gt;&lt;span&gt; (d &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      TTKV&lt;&#x2F;span&gt;&lt;span&gt;.empty&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .put(a, x)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .flatMap(_.put(a, y))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .map { m &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;          val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; ts&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; m.times(a)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;          val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; t0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ts.head&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;          val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; t1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ts.last&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;          val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; δ&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (t1 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; t0)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          m.get(a, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Some&lt;&#x2F;span&gt;&lt;span&gt;(t0 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; δ.toLong))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Some&lt;&#x2F;span&gt;&lt;span&gt;(x)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        .unsafeRunSync&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;appendix&quot;&gt;Appendix&lt;&#x2F;h1&gt;
&lt;p&gt;Here’s the &lt;code&gt;flake.nix&lt;&#x2F;code&gt; file to specify the W O R L D:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;nix&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  description&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Scala TTKV&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:numtide&#x2F;flake-utils&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:NixOS&#x2F;nixpkgs&#x2F;nixos-23.11&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  outputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;lib&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;eachDefaultSystem&lt;&#x2F;span&gt;&lt;span&gt; (system:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; let&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; nixpkgs&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;inherit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; system&lt;&#x2F;span&gt;&lt;span&gt;;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    in&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      packages&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;writeShellApplication&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;ttkv&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        runtimeInputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;sbt&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        text&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;          ${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;sbt&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}&#x2F;bin&#x2F;sbt test&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;build.sbt&lt;&#x2F;code&gt; is similarly minimal:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;lazy val&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; root&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (project in file(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;.&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  .settings(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    name &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;ttkv&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    libraryDependencies &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Seq&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;org.typelevel&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %%&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;cats-effect&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;3.5.4&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;org.typelevel&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %%&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;alleycats-core&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;2.10.0&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;org.scalacheck&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %%&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;scalacheck&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;1.15.4&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; %&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;test&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    scalacOptions &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;++=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Seq&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;-feature&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;-deprecation&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;-unchecked&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;-language:higherKinds&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;      &amp;quot;-Ypartial-unification&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To run the tests, poke around in the code, etc. feel free to copy the whole
&lt;code&gt;ttkv&lt;&#x2F;code&gt; directory from my &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;ttkv&#x2F;tree&#x2F;main&#x2F;ttkv_sc&quot;&gt;programming workbench on
GitHub.&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Slides: Algebraic Structure, Computational Benefits</title>
        <published>2018-02-20T00:00:00+00:00</published>
        <updated>2018-02-20T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/algebraic-structure/"/>
        <id>https://grahamenos.com/algebraic-structure/</id>
        
        <content type="html" xml:base="https://grahamenos.com/algebraic-structure/">&lt;h1 id=&quot;algebraic-structure-computational-benefits&quot;&gt;Algebraic Structure, Computational Benefits&lt;&#x2F;h1&gt;
&lt;p&gt;I recently gave a talk at work titled “Algebraic Structure, Computational
Benefits.”
In it, I defined a few basic (as in fundamental, not as in easy) structures
like semigroups, monoids, and semirings, and I discussed the computational
benefits one can derive by making these structures explicit in code, like:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;the most generic of generic algorithmic code&lt;&#x2F;li&gt;
&lt;li&gt;parallelism for free, a.k.a. associativity implies &lt;code&gt;Map-Reduce&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;associativity + commutativity ⇒ &lt;code&gt;Map-Shuffle-Reduce&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;I also talked a little bit about one of my recent favorites, the
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Tropical_geometry&quot;&gt;&lt;em&gt;Tropical Semiring&lt;&#x2F;em&gt;&lt;&#x2F;a&gt;, and how
it turns shortest-distance problems in graph theory into iterated linear
algebra.&lt;&#x2F;p&gt;
&lt;p&gt;Should any of this spark your interest, slides are available &lt;a href=&quot;&#x2F;pdfs&#x2F;ascb.pdf&quot;&gt;here.&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Fourier Transform in Clojure, Reproducibly with Nix</title>
        <published>2017-12-12T00:00:00+00:00</published>
        <updated>2017-12-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/ft-clojure-nix/"/>
        <id>https://grahamenos.com/ft-clojure-nix/</id>
        
        <content type="html" xml:base="https://grahamenos.com/ft-clojure-nix/">&lt;h1 id=&quot;combining-previous-work-with-new-things&quot;&gt;Combining previous work with new things&lt;&#x2F;h1&gt;
&lt;p&gt;In a &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;group-theory-and-statistics&#x2F;&quot;&gt;previous post&lt;&#x2F;a&gt;,
we explored an example from Persi Diaconis using the Fourier Transform to
change a collection of permutations into a table where the &lt;em&gt;(i, j)&lt;&#x2F;em&gt; entry is
the number of voters who ranked candidate &lt;em&gt;i&lt;&#x2F;em&gt; in place &lt;em&gt;j&lt;&#x2F;em&gt;; this
transformation consists of first mapping each permutation to its matrix
representation, then adding the matrices together.&lt;&#x2F;p&gt;
&lt;p&gt;There’s more to explore here, however; since counts are just integers and
addition of integers is both associative and commutative, this matrix addition
is also commutative—that is, we can reorder and group the summands as much as
we like.
Thus, this problem is well suited to map-reduce-like approaches, since
associativity implies parallelism.
With commutativity, we can include a “shuffle” step, too.
This is what the &lt;code&gt;Clojure&lt;&#x2F;code&gt; library &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;aphyr&#x2F;tesser&quot;&gt;&lt;code&gt;tesser&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;
provides.
Combined with &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;mikera&#x2F;core.matrix&quot;&gt;&lt;code&gt;core.matrix&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for
matrices, &lt;code&gt;tesser&lt;&#x2F;code&gt; allows us to&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;map the vote vectors to their permutation matrices&lt;&#x2F;li&gt;
&lt;li&gt;add them together&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;but, contrasted with core &lt;code&gt;Clojure&lt;&#x2F;code&gt;’s &lt;code&gt;map&lt;&#x2F;code&gt; and &lt;code&gt;reduce&lt;&#x2F;code&gt;, we can take advantage
of the mathematical structure to do all of step 1 in parallel, then all of
step 2 in parallel with arbitrary shuffling.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;clojure-code&quot;&gt;&lt;code&gt;Clojure&lt;&#x2F;code&gt; Code&lt;&#x2F;h1&gt;
&lt;p&gt;Here’s &lt;code&gt;Clojure&lt;&#x2F;code&gt; code that, after simulating a collection of votes, calculates
the desired table in two different ways:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;in serial, with the default &lt;code&gt;map&lt;&#x2F;code&gt; and &lt;code&gt;reduce&lt;&#x2F;code&gt;, and&lt;&#x2F;li&gt;
&lt;li&gt;using &lt;code&gt;tesser&lt;&#x2F;code&gt; to take full advantage of associativity and commutativity.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;It then checks that the two answers (imaginatively called &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt;) are
equal, then prints out &lt;code&gt;a&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;clojure&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;require&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;[tesser.core&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; :as&lt;&#x2F;span&gt;&lt;span&gt; t])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;require&lt;&#x2F;span&gt;&lt;span&gt; &amp;#39;[clojure.core.matrix&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; :as&lt;&#x2F;span&gt;&lt;span&gt; m])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;let&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  [k&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 10000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   zero (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;m&#x2F;zero-matrix&lt;&#x2F;span&gt;&lt;span&gt; k k)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   vecs (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; [_ (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt; n)] (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;shuffle&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt; k)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   a (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;-&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; vecs&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt; m&#x2F;permutation-matrix)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt; m&#x2F;add zero))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   b (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;-&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;t&#x2F;map&lt;&#x2F;span&gt;&lt;span&gt; m&#x2F;permutation-matrix)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;t&#x2F;reduce&lt;&#x2F;span&gt;&lt;span&gt; m&#x2F;add zero)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;t&#x2F;tesser&lt;&#x2F;span&gt;&lt;span&gt; [vecs]))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;   matrices-equal (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; a b)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Yes&amp;quot; &amp;quot;No&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;println&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\n\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;Are a and b equal? &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; matrices-equal&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;m&#x2F;pm&lt;&#x2F;span&gt;&lt;span&gt; a))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which, on one run, gave the following output:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Are a and b equal? Yes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[[1448.000 1419.000 1434.000 1505.000 1444.000 1389.000 1361.000]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; [1428.000 1429.000 1430.000 1404.000 1384.000 1475.000 1450.000]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; [1466.000 1471.000 1367.000 1458.000 1443.000 1408.000 1387.000]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; [1427.000 1369.000 1434.000 1429.000 1452.000 1434.000 1455.000]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; [1393.000 1388.000 1481.000 1444.000 1380.000 1439.000 1475.000]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; [1412.000 1429.000 1447.000 1389.000 1415.000 1486.000 1422.000]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; [1426.000 1495.000 1407.000 1371.000 1482.000 1369.000 1450.000]]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;nix-expressions&quot;&gt;&lt;code&gt;Nix&lt;&#x2F;code&gt; Expressions&lt;&#x2F;h1&gt;
&lt;p&gt;Code demos are fun, but too often they fall victim to bit-rot and stop working.
To combat this, we can strive for reproducibility, an important goal of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.coursera.org&#x2F;learn&#x2F;reproducible-research&quot;&gt;data
science&lt;&#x2F;a&gt;.
One tool that can help is &lt;code&gt;nix&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;nixos.org&#x2F;nix&#x2F;&quot;&gt;&lt;code&gt;Nix&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is a package manager, like &lt;code&gt;homebrew&lt;&#x2F;code&gt; for &lt;code&gt;OS X&lt;&#x2F;code&gt;,
that, through a novel approach, goes a &lt;em&gt;very&lt;&#x2F;em&gt; long way towards reproducibility.
By specifying everything in a declarative, functional language, we can be sure
we’re building the same thing twice.
With &lt;code&gt;nix&lt;&#x2F;code&gt;, we can specify the &lt;em&gt;exact&lt;&#x2F;em&gt; versions of &lt;code&gt;tesser&lt;&#x2F;code&gt; and &lt;code&gt;core.matrix&lt;&#x2F;code&gt;
we used, along with how to build them.
Then, we can provide a function to reproducibly run the above example.
The &lt;code&gt;nix&lt;&#x2F;code&gt; expression to do so is rather long—though that’s a small price to
pay for the assurance we get.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s the content of &lt;code&gt;flake.nix&lt;&#x2F;code&gt;.
Save this in a file called &lt;code&gt;flake.nix&lt;&#x2F;code&gt;, and save the above &lt;code&gt;Clojure&lt;&#x2F;code&gt; code in &lt;code&gt;diaconis.clj&lt;&#x2F;code&gt; in the same directory.
Then run &lt;code&gt;nix run&lt;&#x2F;code&gt; to (eventually) see the results.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;nix&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  description&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;Fourier Transform in Clojure&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  inputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:numtide&#x2F;flake-utils&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;url&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;github:NixOS&#x2F;nixpkgs&#x2F;nixos-22.11&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  outputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    self&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    nixpkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;    flake-utils&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;lib&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;eachDefaultSystem&lt;&#x2F;span&gt;&lt;span&gt; (system:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; let&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; nixpkgs&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;inherit&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; system&lt;&#x2F;span&gt;&lt;span&gt;;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      leinFromGitHub&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        owner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        repo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        rev&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        sha256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ?&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;.&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;        pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;stdenv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;mkDerivation&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;          name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; name&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;          src&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;fetchFromGitHub&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            owner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; owner&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            repo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; repo&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            rev&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; rev&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;            sha256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; sha256&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;          };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;          buildInputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;leiningen&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;          # https:&#x2F;&#x2F;blog.jeaye.com&#x2F;2017&#x2F;07&#x2F;30&#x2F;nixos-revisited&#x2F;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;          buildPhase&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            export LEIN_HOME=$PWD&#x2F;.lein&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            mkdir -p $LEIN_HOME&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            echo &amp;quot;{:user {:local-repo \&amp;quot;$LEIN_HOME\&amp;quot;}}&amp;quot; &amp;gt; $LEIN_HOME&#x2F;profiles.clj&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            cd ${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            LEIN_SNAPSHOTS_IN_RELEASE=1 ${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;leiningen&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}&#x2F;bin&#x2F;lein uberjar&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;          &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;          installPhase&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;            cp target&#x2F;${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;repo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}*-standalone.jar $out&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;          &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      tesser&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; leinFromGitHub&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;tesser&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        owner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;aphyr&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        repo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;tesser&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        rev&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;d82895390264d8f6bf012fa4053a2003a500b574&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; # release 1.0.4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        sha256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;0k2shy6ccig00lc9y6y8lyh7bnd250xxp5nh8fz254f2kq8ib4mn&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;core&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      matrix&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; leinFromGitHub&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;matrix&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        owner&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;mikera&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        repo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;core.matrix&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        rev&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;07cb88b1b43ccc07f3f7e8634e1eccdb6986049b&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; # develop branch as of 2021-11-19&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        sha256&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;1dn5hy5mdgl2bcmav8qll3qxqbscb340c462kkj728sfpk4ch5xd&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    in&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      packages&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;default&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;writeShellApplication&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; rec&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;diaconis.clj&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        runtimeInputs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;clojure pkgs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;jdk&lt;&#x2F;span&gt;&lt;span&gt;];&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;        text&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;          java -cp ${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;tesser&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}:${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;matrix&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;} clojure.main ${&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      };&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    });&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Two Short Python Things</title>
        <published>2017-09-05T00:00:00+00:00</published>
        <updated>2017-09-05T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/two-short-python-things/"/>
        <id>https://grahamenos.com/two-short-python-things/</id>
        
        <content type="html" xml:base="https://grahamenos.com/two-short-python-things/">&lt;h1 id=&quot;sorry&quot;&gt;Sorry&lt;&#x2F;h1&gt;
&lt;p&gt;Apologies, as ever, for taking &lt;em&gt;so&lt;&#x2F;em&gt; incredibly long between posts.
That said if the inimitable Roger Peng can get away with letting some time
lapse while working on a project, perhaps you can forgive me?&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Sometimes it takes a while for a project to get going.&lt;&#x2F;p&gt;
&lt;p&gt;— Roger D. Peng (@rdpeng) August 24, 2017, Twitter&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;back-to-python&quot;&gt;Back to Python&lt;&#x2F;h1&gt;
&lt;p&gt;After a long time in the Scala wilderness, I’ve recently had a chance to write
some Python again for scripting purposes at work.
I had a bit of freedom with regards to process, so I explicitly chose Python
3.6.
I’m here to tell you that Python 3.6 is really nice and to exhibit two things
that made my life much better.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;what-s-up-doc&quot;&gt;What’s up, &lt;code&gt;__doc__&lt;&#x2F;code&gt;?&lt;&#x2F;h1&gt;
&lt;p&gt;Especially for scripts, I’ve gotten into the habit of putting all of the actual
work and logic of my Python program in functions (usually a &lt;code&gt;main&lt;&#x2F;code&gt; function
calling other steps&#x2F;etc.).
I then have an &lt;code&gt;argparse.ArgumentParser&lt;&#x2F;code&gt; at the bottom (wrapped in an &lt;code&gt;if __name__ == &#x27;__main__&#x27;&lt;&#x2F;code&gt; &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;__main__.html&quot;&gt;scripting
statement&lt;&#x2F;a&gt;) that parses
command line arguments and passes them to the &lt;code&gt;main&lt;&#x2F;code&gt; function, like so:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __name__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;__main__&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    P&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ArgumentParser()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    P.add_argument(…)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    …&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    main(P.parse_args())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Given this setup, one can pass the &lt;code&gt;-h&lt;&#x2F;code&gt; or &lt;code&gt;--help&lt;&#x2F;code&gt; flag to the script and
receive some information about the different arguments you’ve added to the
parser.
However, it’s good form to add a &lt;code&gt;description&lt;&#x2F;code&gt; to your &lt;code&gt;ArgumentParser&lt;&#x2F;code&gt; that
explains the purpose and usage of the script.&lt;&#x2F;p&gt;
&lt;p&gt;If you’re following best practices (e.g. by trying to appease
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.pylint.org&quot;&gt;&lt;code&gt;pylint&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;), you’ve already added a docstring to the
top of your file that explains its purpose and usage.
It can be a pain (or at least problematic) to keep this docstring and the
parser’s &lt;code&gt;description&lt;&#x2F;code&gt; in sync.&lt;&#x2F;p&gt;
&lt;p&gt;I recently stumbled across a nice solution to this problem: only write the
docstring, and then use the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;33066383&#x2F;print-doc-in-python-3-script&quot;&gt;&lt;code&gt;__doc__&lt;&#x2F;code&gt; magic
variable&lt;&#x2F;a&gt; as your &lt;code&gt;description&lt;&#x2F;code&gt; like so:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __name__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;__main__&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    P&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; ArgumentParser(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;__doc__&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This approach does limit the length and expressiveness you can fit into the
docstring&#x2F;description due to the constraints of printing a help message, but
I haven’t found it to be a problem since my script docstrings tend to be
exactly what I’d want to print a help message anyway.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;typing-namedtuple-is-my-new-favorite&quot;&gt;&lt;code&gt;typing.NamedTuple&lt;&#x2F;code&gt; is my new favorite&lt;&#x2F;h1&gt;
&lt;p&gt;Even more mindbending is the new (in Python 3.5) &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;typing.html&quot;&gt;&lt;code&gt;typing module&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
Coupled with the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;mypy.readthedocs.io&quot;&gt;&lt;code&gt;mypy&lt;&#x2F;code&gt; static checker&lt;&#x2F;a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;32557920&#x2F;what-are-type-hints-in-python-3-5&quot;&gt;type
hints&lt;&#x2F;a&gt;,
this allows you to write Python with static types and check them &lt;em&gt;prior&lt;&#x2F;em&gt; to
running code in production.
For instance, the &lt;code&gt;main&lt;&#x2F;code&gt; function for my scripts that once looked like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;(args):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Performs the main work&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    Parameters&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    ----------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    args: Namespace&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;        Arguments passed on the command line, specifying what to do&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    Returns&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    -------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    None&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    # stuff and things&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    …&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;now looks like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;(args: Namespace) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Performs the main work&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    Parameters&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    ----------&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    args: Arguments passed on the command line, specifying what to do&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;    # stuff and things&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    …&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Some folks might cry that this doesn’t look like Python anymore, but to them I
counter&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0484&#x2F;&quot;&gt;Python Enhancement Proposal 484&lt;&#x2F;a&gt;
already accepted type hints into the language, and&lt;&#x2F;li&gt;
&lt;li&gt;if you’re &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;sphinxcontrib-napoleon.readthedocs.io&#x2F;&quot;&gt;writing suitably descriptive
docstrings&lt;&#x2F;a&gt; for your functions,
you’re &lt;em&gt;already&lt;&#x2F;em&gt; writing the input and output types anyway; why not statically
check them, too?&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;If I haven’t lost you with the type hinting above, I have something else to
show you: &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;typing.html#typing.NamedTuple&quot;&gt;the new &lt;code&gt;NamedTuple&lt;&#x2F;code&gt;
class.&lt;&#x2F;a&gt;
When writing Python classes, I often get annoyed with having to specify the
typical initialization code one finds in the &lt;code&gt;__init__&lt;&#x2F;code&gt; method, like&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; MyThing&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(self, a, b, c, d):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # this is an integer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; b&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # this is a float&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # this is a string&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.d&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # this is a dictionary&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    … &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# other methods here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unless I &lt;em&gt;need&lt;&#x2F;em&gt; some fancy initialization logic, I’d prefer to have this
attribute setup taken care of by default; enter &lt;code&gt;NamedTuple&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; MyThing&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;NamedTuple&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    a:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    b:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; float&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    c:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; str&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    d:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; dict&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    … &lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# other methods here&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides being shorter (“brevity facilitates reasoning,” as &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.jsoftware.com&#x2F;papers&#x2F;tot.htm&quot;&gt;one of my favorite
papers&lt;&#x2F;a&gt; explains), this version
&lt;em&gt;explicitly&lt;&#x2F;em&gt; lists the attribute types in a way checkable via &lt;code&gt;mypy&lt;&#x2F;code&gt;.
For instance, &lt;code&gt;x = MyThing(1, 2, &#x27;three&#x27;, {&#x27;four&#x27;: 4})&lt;&#x2F;code&gt; is just fine, but
if you try to sneak &lt;code&gt;y = MyThing(1.0, 2, &#x27;three&#x27;, {&#x27;four&#x27;: 4})&lt;&#x2F;code&gt; past &lt;code&gt;mypy&lt;&#x2F;code&gt;
it’ll complain&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;error: Argument 1 to &amp;quot;MyThing&amp;quot; has incompatible type &amp;quot;float&amp;quot;; expected &amp;quot;int&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Perhaps my brain has rotted away after relying on
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;haskell-lang.org&quot;&gt;statically&lt;&#x2F;a&gt; &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.scala-lang.org&quot;&gt;typed&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.rust-lang.org&quot;&gt;languages&lt;&#x2F;a&gt;, but I’m a fan.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Cross Compiling Static Rust Binaries for Linux on OS X</title>
        <published>2016-09-26T00:00:00+00:00</published>
        <updated>2016-09-26T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/rust-osx-linux-musl/"/>
        <id>https://grahamenos.com/rust-osx-linux-musl/</id>
        
        <content type="html" xml:base="https://grahamenos.com/rust-osx-linux-musl/">&lt;p&gt;Recently I wrote a small &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;rust-lang.org&quot;&gt;&lt;code&gt;Rust&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; executable for work.
I wrote it on my Apple laptop, but wanted to run it on our Linux servers.
This note hopes to document how I did it so that I and others may repeat my
experience.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Note: I only managed to pull this off because of the helpful folks on the
&lt;code&gt;#rust&lt;&#x2F;code&gt; and &lt;code&gt;#rust-internals&lt;&#x2F;code&gt; IRC channels. Thanks everyone!&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;set-up&quot;&gt;Set up&lt;&#x2F;h1&gt;
&lt;p&gt;There are plenty of tutorials out there for getting started with &lt;code&gt;Rust&lt;&#x2F;code&gt;, though
it’s tough to beat &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nightly&#x2F;book&#x2F;&quot;&gt;the official
book&lt;&#x2F;a&gt;.
In order to get a working environment going, I recommend using
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.rustup.rs&#x2F;&quot;&gt;rustup&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;First, create a new binary project with &lt;code&gt;cargo&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cargo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; init&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; --bin&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; my_proj&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; my_proj&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to get stuff working later, use the &lt;code&gt;nightly&lt;&#x2F;code&gt; branch of &lt;code&gt;Rust&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;rustup&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; override set nightly&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Write your project as usual, editing &lt;code&gt;src&#x2F;main.rs&lt;&#x2F;code&gt; to your heart’s content.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;musl&quot;&gt;&lt;code&gt;musl&lt;&#x2F;code&gt;&lt;&#x2F;h1&gt;
&lt;p&gt;In order to get your executable to run on the Linux server, you’ll need to
compile &amp;amp; link it for that platform.
I’ve had luck creating an &lt;em&gt;entirely&lt;&#x2F;em&gt; static executable using &lt;code&gt;musl&lt;&#x2F;code&gt;, a rewrite
of the &lt;code&gt;C&lt;&#x2F;code&gt; standard library intended for smaller, embedded targets.
To get the necessary tools installed on OS X, use &lt;code&gt;homebrew&lt;&#x2F;code&gt; with &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;FiloSottile&#x2F;homebrew-musl-cross&quot;&gt;this helpful
tap&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Once you’ve got the linker &amp;amp; other compilation tools installed (as the above
link says, that’ll take a while), you’ll need to let &lt;code&gt;cargo&lt;&#x2F;code&gt; know to use them
when compiling for the Linux target.
Create a subdirectory to the &lt;code&gt;my_proj&lt;&#x2F;code&gt; directory called &lt;code&gt;.cargo&lt;&#x2F;code&gt;; then in
&lt;code&gt;.cargo&#x2F;config&lt;&#x2F;code&gt;, write the following&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;toml&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;target&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;x86_64-unknown-linux-musl&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;linker =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;x86_64-linux-musl-gcc&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You’ll also need to have &lt;code&gt;rustup&lt;&#x2F;code&gt; specify the target:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;rustup&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; target add x86_64-unknown-linux-musl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;compile-deploy&quot;&gt;Compile &amp;amp; Deploy&lt;&#x2F;h1&gt;
&lt;p&gt;To compile your executable for the server, run&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cargo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; build&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; --release --target=x86_64-unknown-linux-musl&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Copy it to the server and you’re good to go:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;scp&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; target&#x2F;x86_64-unknown-linux-musl&#x2F;release&#x2F;my_proj ⟨your_server_name⟩:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;upx-added-2016-10-17&quot;&gt;UPX (added 2016-10-17)&lt;&#x2F;h1&gt;
&lt;p&gt;If you’re concerned about the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.rust-lang.org&#x2F;en-US&#x2F;faq.html#why-do-rust-programs-have-larger-binary-sizes-than-C-programs&quot;&gt;binary
size&lt;&#x2F;a&gt;
of your new executable, check out &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;upx.github.io&quot;&gt;&lt;code&gt;UPX&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
After installing it on my laptop via &lt;code&gt;brew install upx&lt;&#x2F;code&gt;, I ran &lt;code&gt;upx -9&lt;&#x2F;code&gt; on an
executable created with the above instructions.
While the executable was an overly simplistic example, &lt;code&gt;upx&lt;&#x2F;code&gt; compressed it down
to &lt;em&gt;34%&lt;&#x2F;em&gt; of the original size.
Even if you don’t care about the size of the binary once it’s on the server, it
at least made the &lt;code&gt;scp&lt;&#x2F;code&gt; go faster.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>ScalaCheck and the Unkillable Trash</title>
        <published>2016-04-21T00:00:00+00:00</published>
        <updated>2016-04-21T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/scalacheck-unkillable-trash/"/>
        <id>https://grahamenos.com/scalacheck-unkillable-trash/</id>
        
        <content type="html" xml:base="https://grahamenos.com/scalacheck-unkillable-trash/">&lt;h1 id=&quot;well-that-s-weird&quot;&gt;Well that’s weird&lt;&#x2F;h1&gt;
&lt;p&gt;I ran into an interesting problem in a recent
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.scala-lang.org&#x2F;&quot;&gt;&lt;code&gt;Scala&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; project.
Let’s set the scene …&lt;&#x2F;p&gt;
&lt;h1 id=&quot;quickly-checking-in-scala&quot;&gt;Quickly Checking in Scala&lt;&#x2F;h1&gt;
&lt;p&gt;In the project in question, I reached for &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;argonaut-io&#x2F;argonaut&quot;&gt;a
library&lt;&#x2F;a&gt; to do a fair amount of JSON
parsing.
I hadn’t used the library before, so I ended up writing some
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.scalacheck.org&#x2F;&quot;&gt;&lt;code&gt;ScalaCheck&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; tests for the convenience&#x2F;wrapper
functions I made.&lt;&#x2F;p&gt;
&lt;p&gt;One test involved writing data to a temporary file, reading and parsing that
information, then comparing the parsed results to the original data.
If all went well, the parsed and original data should match perfectly.
Either way, there was clean up code at the end of the test to delete the
temporary file.
My mistake was in letting &lt;code&gt;ScalaCheck&lt;&#x2F;code&gt; generate strings of arbitrary characters
for the temporary file’s name, rather than restricting those filenames to
printable or alphanumeric characters only.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;obscure-os-x-obstacles&quot;&gt;Obscure OS X obstacles&lt;&#x2F;h1&gt;
&lt;p&gt;It turns out that the latest version of Mac OS X can’t delete—or
otherwise get a handle on, via GUI or CLI—files with &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;apple.stackexchange.com&#x2F;questions&#x2F;224505&#x2F;is-it-impossible-to-delete-move-a-file-named-on-mac&quot;&gt;certain characters
in the
name&lt;&#x2F;a&gt;.
So, when &lt;code&gt;ScalaCheck&lt;&#x2F;code&gt; created such a temporary file, my clean up code was
unable to &lt;code&gt;rm&lt;&#x2F;code&gt; it.
I tried &lt;em&gt;everything&lt;&#x2F;em&gt; the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;quantifind.com&#x2F;&quot;&gt;Quantifind Dev team&lt;&#x2F;a&gt; and I
could think of, to no avail.
I now have, tucked away in a directory called &lt;code&gt;unkillable_trash&lt;&#x2F;code&gt;, a file that
nothing short of reformatting my hard drive will remove.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;update-2017-04-13&quot;&gt;Update (2017-04-13)&lt;&#x2F;h1&gt;
&lt;p&gt;Since the time of this post, a botched update necessitated reinstalling the OS
on my work computer; the &lt;code&gt;unkillable_trash&lt;&#x2F;code&gt; is, sadly, now gone.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>New Job, Hire in DC, and a Short Revisit of Finite Fields</title>
        <published>2016-04-07T00:00:00+00:00</published>
        <updated>2016-04-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/qf-dc-ff/"/>
        <id>https://grahamenos.com/qf-dc-ff/</id>
        
        <content type="html" xml:base="https://grahamenos.com/qf-dc-ff/">&lt;h1 id=&quot;hola&quot;&gt;Hola&lt;&#x2F;h1&gt;
&lt;p&gt;Is it poor form to start each post with an apology for the time elapsed since
the previous one?
Either way, hello dear reader.
Beyond being the father of a rambunctious young’un, things have changed around
here since we last spoke.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;new-job-plus-why-tech-companies-should-hire-in-dc&quot;&gt;New Job, Plus Why Tech Companies Should Hire in DC&lt;&#x2F;h1&gt;
&lt;p&gt;In January I started a new gig as a Data Scientist for
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;quantifind.com&#x2F;&quot;&gt;Quantifind&lt;&#x2F;a&gt;.
I’m excited about the opportunity, and the balance of faster-paced demands and
more blue-sky research has been great.&lt;&#x2F;p&gt;
&lt;p&gt;This new job has highlighted something for me: if you’re a tech&#x2F;data&#x2F;whatever
company, you should hire people from the D.C. area.
There are a large number of people here who have the technical chops to do data
and software work, and who—given
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.executivegov.com&#x2F;2015&#x2F;12&#x2F;adm-michael-rogers-nsa-to-undergo-reorganization-in-january&#x2F;&quot;&gt;agency&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.washingtonpost.com&#x2F;world&#x2F;national-security&#x2F;national-security-agency-plans-major-reorganization&#x2F;2016&#x2F;02&#x2F;02&#x2F;2a66555e-c960-11e5-a7b2-5a2f824b02c9_story.html&quot;&gt;reorganizations&lt;&#x2F;a&gt;
and general &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.washingtonpost.com&#x2F;news&#x2F;federal-eye&#x2F;wp&#x2F;2015&#x2F;07&#x2F;15&#x2F;new-report-documents-falling-federal-employee-morale&#x2F;&quot;&gt;low
morale&lt;&#x2F;a&gt;—are
probably open to new opportunities.
While some may be able to pick up and move to Silicon Valley, I’d wager that
many are (like myself) unable to uproot their lives and families.
I’m fortunate that Quantifind hired me to work in the D.C. office, even though
they’re based in California.
Other companies should consider tapping the large pool of talent we have in the
region.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;revisiting-finite-fields&quot;&gt;Revisiting Finite Fields&lt;&#x2F;h1&gt;
&lt;p&gt;Now a short technical note.
In a &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;finite-fields&#x2F;&quot;&gt;previous post&lt;&#x2F;a&gt;, I talked about
my difficulties in working with finite fields in software.
I’m starting to think that for parameterized mathematical objects (e.g. by a
prime of a prime and a polynomial, like finite fields) might be good candidates
for a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Dependent_type&quot;&gt;dependently&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;ejenk.com&#x2F;blog&#x2F;why-dependently-typed-programming-will-one-day-rock-your-world.html&quot;&gt;typed&lt;&#x2F;a&gt;
language like &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.idris-lang.org&#x2F;&quot;&gt;&lt;code&gt;Idris&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.
I don’t have any experience with &lt;code&gt;Idris&lt;&#x2F;code&gt; myself, but I’d be curious to see a
finite field implementation in it.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Learning By Building: A Simple Neural Network Implementation</title>
        <published>2015-07-20T00:00:00+00:00</published>
        <updated>2015-07-20T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/feynman-nn/"/>
        <id>https://grahamenos.com/feynman-nn/</id>
        
        <content type="html" xml:base="https://grahamenos.com/feynman-nn/">&lt;h1 id=&quot;small-baby-no-sleep-make-graham-a-dull-blogger&quot;&gt;Small baby, no sleep make Graham a dull blogger&lt;&#x2F;h1&gt;
&lt;p&gt;Apologies, readers, for my long absence.
My family grew this last year, so I’ve had less time for writing.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;feynman-s-blackboard&quot;&gt;Feynman’s Blackboard&lt;&#x2F;h1&gt;
&lt;p&gt;On &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;edwardtufte&#x2F;status&#x2F;566018453169913856&quot;&gt;Richard Feynman’s
blackboard&lt;&#x2F;a&gt; at the
time of his death was &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikiquote.org&#x2F;wiki&#x2F;Richard_Feynman&quot;&gt;the
quote&lt;&#x2F;a&gt; &lt;em&gt;“What I cannot create, I
do not understand.”&lt;&#x2F;em&gt;
Powerful words, and I definitely think there’s some truth to them.&lt;&#x2F;p&gt;
&lt;p&gt;Recently I came across a popular, succinct
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;iamtrask.github.io&#x2F;2015&#x2F;07&#x2F;12&#x2F;basic-python-network&#x2F;&quot;&gt;post&lt;&#x2F;a&gt; on
implementing a &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Artificial_neural_network&quot;&gt;neural
network&lt;&#x2F;a&gt; in Python.
I realized that although I’ve used them before and had a cursory understanding
of how they worked under the hood, I didn’t really &lt;em&gt;grok&lt;&#x2F;em&gt; them; I had always
turned to a library.
Admittedly, turning to a well-developed library is exactly what one should do
in production, but for my own edification I decided to put together &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;nn.py&quot;&gt;a Python
implementation&lt;&#x2F;a&gt;
of my own.
This implementation is rather simplistic, but it has three features I like:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;It’s simple enough that someone as sleep deprived as myself can
understand it.&lt;&#x2F;li&gt;
&lt;li&gt;It feels Pythonic; it relies only upon &lt;code&gt;NumPy&lt;&#x2F;code&gt; and has an API similar to
&lt;code&gt;sklearn&lt;&#x2F;code&gt;’s classifiers&lt;&#x2F;li&gt;
&lt;li&gt;It can handle an arbitrary number of hidden layers of arbitrary size.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h1 id=&quot;lesson-learned&quot;&gt;Lesson Learned&lt;&#x2F;h1&gt;
&lt;p&gt;As awesome as neural nets are, as many varieties and variations there are, and
as amazing as some of the
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;googleresearch.blogspot.com&#x2F;2015&#x2F;06&#x2F;inceptionism-going-deeper-into-neural.html&quot;&gt;applications&lt;&#x2F;a&gt;
may be, I discovered that at their base, they’re rather simple.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
Neural networks boil down to iterated linear algebra—which totally makes
sense, since they’re being implemented on GPUs all over the place.
Though I wouldn’t recommend going back to basics all the time (especially if
you’re on a deadline), I’m definitely glad I took the time to work this
through.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;The vanilla version is, anyway; other variations are admittedly more complex. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Predicting Transformer Affiliation from Motto</title>
        <published>2014-10-26T00:00:00+00:00</published>
        <updated>2014-10-26T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/predicting-transformer-affiliation/"/>
        <id>https://grahamenos.com/predicting-transformer-affiliation/</id>
        
        <content type="html" xml:base="https://grahamenos.com/predicting-transformer-affiliation/">&lt;h1 id=&quot;the-story&quot;&gt;The Story&lt;&#x2F;h1&gt;
&lt;p&gt;To give an example of working with data while still having fun, I’ve decided to
tackle &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Transformers&quot;&gt;Transformers&lt;&#x2F;a&gt;.
Specifically, after finding &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;infochimps.com&#x2F;datasets&#x2F;transformers-character&quot;&gt;this
dataset&lt;&#x2F;a&gt; someone scraped
together from Wikipedia, I thought it’d be interesting to see if we can
predict the affiliation (Autobot vs. Decepticon) of a character using one of
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;scikit-learn.org&#x2F;&quot;&gt;Scikit-Learn&lt;&#x2F;a&gt;’s
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Naive_Bayes_classifier&quot;&gt;naive Bayes
classifiers&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Enough chit-chat; let’s roll up our sleeves and dive in!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;preliminaries&quot;&gt;Preliminaries&lt;&#x2F;h1&gt;
&lt;p&gt;Checking out the data, it looks like the entries are indexed by name (on the
far left of the line), which approximates the Transformer’s name.
Let’s see how many entries we’re dealing with:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -P&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;^\S&amp;#39; infobox_transformers_character.yaml&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; wc&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -l&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;718&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are 718 entries, but it looks like there may be repeats (i.e., either the
data isn’t as clean as we’d hoped, or the names are not a unique enough
identifier).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -P&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;^\S&amp;#39; infobox_transformers_character.yaml&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      uniq&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      awk&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;{print $1}&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      sort&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      uniq&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -c&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;250&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; 85&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; 45&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; 19&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; 12&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;  1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;  8&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So it looks like the majority of names aren’t repeated, but there are a few;
one is even repeated 8 times!&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -P&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;^\S&amp;#39; infobox_transformers_character.yaml&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      uniq&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      sort&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -nr&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;      head&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Prowl_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Transformers&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Bonecrusher_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Transformers&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Ultra_Magnus:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Ironhide:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Thrust_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Transformers&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Swindle_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Transformers&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Snarl_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Transformers&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Skydive_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Transformers&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Scavenger_&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;Transformers&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; Razorclaw:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Prowl is the most popular, eh? Let’s look at the different entries with that
name.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; grep&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -P -A5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;^Prowl&amp;#39; infobox_transformers_character.yaml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# output omitted&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So these certainly seem to be unique entries; we should uniquify these names,
because &lt;code&gt;PyYAML&lt;&#x2F;code&gt; turns YAML into a dictionary (and therefore squashes things
under a single heading).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; cat infobox_transformers_character.yaml&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;    perl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -ple&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;BEGIN{$n = 0;} if (m&#x2F;(^\S+)[:]&#x2F;) { $_ = $1 . ($n++) . &amp;quot;:&amp;quot;; }&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# output omitted&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Looks good; let’s save it to a new YAML file.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; perl&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; -ple&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;BEGIN{$n = 0;} if (m&#x2F;(^\S+)[:]&#x2F;) { $_ = $1 . ($n++) . &amp;quot;:&amp;quot;; }&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;    infobox_transformers_character.yaml&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; unique_names.yaml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;bring-out-the-python&quot;&gt;Bring out the Python&lt;&#x2F;h1&gt;
&lt;p&gt;Let’s start up IPython and start digging.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; import&lt;&#x2F;span&gt;&lt;span&gt; yaml&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; with&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; open&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;unique_names.yaml&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; f:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        raw_data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; yaml.load(f)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Loading that raw data into &lt;code&gt;Pandas&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; import&lt;&#x2F;span&gt;&lt;span&gt; numpy&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; np&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; import&lt;&#x2F;span&gt;&lt;span&gt; pandas&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; pd&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; messy_df&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; pd.DataFrame(raw_data).transpose()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; has_motto&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; messy_df[messy_df.motto.notnull()]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; len&lt;&#x2F;span&gt;&lt;span&gt;(has_motto)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;485&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; any&lt;&#x2F;span&gt;&lt;span&gt;(has_motto.affiliation.isnull())&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are 485 entries that have a motto, and every entry with a motto also has
an affiliation.
As you might expect, though, our data isn’t clean enough yet; there are some
affiliations we don’t expect (i.e. not just “Autobot” or “Decepticon”), and the
mottos aren’t in the cleanest form.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; has_motto.affiliation.unique()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;array([&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;Decepticon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Autobot&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Maximal&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Predacon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;       &amp;#39;Autobot, later Maximal&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Mini-Con&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;       &amp;#39;Predacon, later Maximal then Decepticon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;       &amp;#39;Maximal, later Autobot&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Vehicon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;       &amp;#39;Maximal, &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;n Former Predacon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Mutant&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;       &amp;#39;Decepticon, later Autobot&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;None&#x2F;Decepticon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;       &amp;#39;Decepticon, later Predacon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;botconcron&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;       dtype&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; has_motto.affiliation.value_counts()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Autobot&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                    213&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Decepticon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                 143&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Mini&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;Con&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                    40&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Maximal&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                     28&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Predacon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                    23&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Vehicon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                      9&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Maximal, later Autobot&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                       7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Decepticon, later Autobot&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                    6&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Mutant&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                       4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Autobot, later Maximal&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                       4&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Maximal, \&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B31D28;font-style: italic;&quot;&gt;n Former Predacon                  3&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;Decepticon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                              2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Decepticon, later Predacon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                   1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;botconcron&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                                   1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Predacon, later Maximal then Decepticon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Consulting some &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;&quot;&gt;expert knowledge&lt;&#x2F;a&gt;, it looks like&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Maximals can count as Autobots since they’re descended from them&lt;&#x2F;li&gt;
&lt;li&gt;Predacons can count as Decepticons, since they’re the enemies of the Maximals&lt;&#x2F;li&gt;
&lt;li&gt;Mini-Cons can be either, so we’ll have to ignore them&lt;&#x2F;li&gt;
&lt;li&gt;Vehicons are Decepticons&lt;&#x2F;li&gt;
&lt;li&gt;I was unable to find botconcron; this looks like a transcription error, but
there’s only one so we’ll drop it&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Cleaning up and creating a new variable &lt;code&gt;numeric_affil&lt;&#x2F;code&gt; with 0 = Decepticon and
1 = Autobot:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; normalize_aff&lt;&#x2F;span&gt;&lt;span&gt;(aff):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; any&lt;&#x2F;span&gt;&lt;span&gt;(bad&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; aff&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; bad&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Decepticon Predacon Vehicon&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.split()):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        elif&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; any&lt;&#x2F;span&gt;&lt;span&gt;(good&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; aff&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; good&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;Autobot Maximal&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.split()):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;            return&lt;&#x2F;span&gt;&lt;span&gt; np.nan&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; has_motto[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;numeric_affil&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; has_motto.affiliation.apply(normalize_aff)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Cleaning the mottos:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; clean_motto&lt;&#x2F;span&gt;&lt;span&gt;(m):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; m.lower()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                .replace(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\u2019&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;&amp;#39;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                .replace(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\u2019&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;quot;&amp;#39;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                .replace(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\\&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                .replace(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;&amp;lt;br&amp;gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                .replace(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;&amp;lt;&#x2F;br&amp;gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; has_motto[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;clean_motto&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; has_motto.motto.apply(clean_motto)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This still isn’t perfect, but it’s much better than we started with.&lt;&#x2F;p&gt;
&lt;p&gt;Now, we’ve got our cleaned data:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; data&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; has_motto.dropna()[[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;numeric_affil&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;clean_motto&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; data.columns&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;affiliation motto&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.split()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;scikit-learn&quot;&gt;Scikit-Learn&lt;&#x2F;h1&gt;
&lt;p&gt;Let’s import what we’ll need: two vectorizers for transforming text into
matrices, and the two naive Bayes classifiers we’ll compare.
We’ll also grab the &lt;code&gt;word_tokenize&lt;&#x2F;code&gt; function from &lt;code&gt;NLTK&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; nltk&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; word_tokenize&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; sklearn.feature_extraction.text&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; CountVectorizer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; sklearn.feature_extraction.text&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; TfidfVectorizer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; sklearn.naive_bayes&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; BernoulliNB&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; sklearn.naive_bayes&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt;  GaussianNB&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binary_vectorizer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; CountVectorizer(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;        binary&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; min_df&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; tokenizer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;word_tokenize)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; tfidf_vectorizer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; TfidfVectorizer(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;        min_df&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; tokenizer&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;word_tokenize)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binary_nb, gaussian_nb&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; BernoulliNB(), GaussianNB()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To compare the Bernoulli and the Gaussian naive Bayes classifiers, we will need
to use our vectorizers on them both.
To play fairly, we’ll break out data into train and test groups.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; sklearn.cross_validation&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; train_test_split&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binary_motto&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; binary_vectorizer.fit_transform(data.motto)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; tfidf_motto&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; tfidf_vectorizer.fit_transform(data.motto)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (binary_motto_train,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     binary_motto_test,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     affil_train,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     affil_test)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; train_test_split(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        binary_motto, data.affiliation,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; test_size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; random_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1729&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; (tfidf_motto_train,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     tfidf_motto_test,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     affil_train,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     affil_test)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; train_test_split(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;     tfidf_motto, data.affiliation,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; test_size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0.2&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; random_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1729&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now to build the two models we wish to compare.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binary_nb.fit(binary_motto_train, affil_train)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; gaussian_nb.fit(tfidf_motto_train.toarray(), affil_train)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;comparing-models&quot;&gt;Comparing Models&lt;&#x2F;h1&gt;
&lt;p&gt;If this were a real life task, we may not have test data.
To compare our models, then, we won’t resort to the test data just yet;
instead, we’ll use &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Cross-validation_%28statistics%29&quot;&gt;leave one out
cross-validation&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; sklearn.cross_validation&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; cross_val_score, LeaveOneOut&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binary_cv_score&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; cross_val_score(binary_nb,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                      binary_motto_train,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                      affil_train,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;                                      cv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;LeaveOneOut(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(affil_train)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; gaussian_cv_score&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; cross_val_score(gaussian_nb,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                        tfidf_motto_train.toarray(),&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                        affil_train,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;                                        cv&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;LeaveOneOut(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(affil_train)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Binary CV Score: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:.5f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.format(binary_cv_score.mean()))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Gaussian CV Score: &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;{0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;:.5f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.format(gaussian_cv_score.mean()))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Binary&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; CV&lt;&#x2F;span&gt;&lt;span&gt; Score:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        0.65341&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Gaussian&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; CV&lt;&#x2F;span&gt;&lt;span&gt; Score:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;      0.58523&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Though neither of these two classifiers look great, it looks like the binary
one wins on our training data.
Let’s pretend we’ve made the decision to field that classifier in production
and check out its performance on the test data.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;binary-classifier-results&quot;&gt;Binary Classifier Results&lt;&#x2F;h1&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; from&lt;&#x2F;span&gt;&lt;span&gt; sklearn&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; metrics&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binary_prediction&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; binary_nb.predict(binary_motto_test)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here’s the simplest way of looking at our Binary NB’s performance: a confusion
matrix.
As we can see from this, it looks like our classifier was pretty good at
getting Autobots right, but Decepticons were harder to accurately predict—I
guess this makes sense, since Decepticons are rather sneaky.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; print&lt;&#x2F;span&gt;&lt;span&gt;(metrics.confusion_matrix(affil_test, binary_prediction))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                  Predicted Decepticon    Predicted Autobot&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Was Decepticon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                      8                   28&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Was Autobot&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;                         6                   46&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For a more in-depth measure of the classifier’s performance, we can turn to
&lt;code&gt;sklearn&lt;&#x2F;code&gt;’s &lt;code&gt;classification_report&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; print&lt;&#x2F;span&gt;&lt;span&gt;(metrics.classification_report(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            affil_test,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            binary_prediction,&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;            target_names&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;Decepticon Autobot&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.split()))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;             precision    recall  f1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;score   support&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt; Decepticon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;       0.57      0.22      0.32        36&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Autobot&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;       0.62      0.88      0.73        52&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;avg&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; total&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;       0.60      0.61      0.56        88&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;According to this, we can see that our classifier is best at identifying actual
Autobots (see the Autobot recall score), but isn’t that great at much else.
For more on how to interpret this table, see &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;scikit-learn.org&#x2F;stable&#x2F;modules&#x2F;model_evaluation.html#binary-classification&quot;&gt;Scikit-Learn’s
documentation&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;It looks like we didn’t do that well at classifying Transformer affiliation
from motto; our best classifier only had an F1 score slightly above 50%.
One possibility is that we didn’t have enough data; perhaps a classifier
trained on thousands of samples would do better than ours that had less than
400.
Another possibility is that mottos aren’t that indicative of affiliation to
begin with, and we should look for our predictive power elsewhere.
In any case, it’s kind of fitting to use Machine Learning on Transformers.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Finite Fields: Surprisingly Tricky to Implement Well</title>
        <published>2014-09-09T00:00:00+00:00</published>
        <updated>2014-09-09T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/finite-fields/"/>
        <id>https://grahamenos.com/finite-fields/</id>
        
        <content type="html" xml:base="https://grahamenos.com/finite-fields/">&lt;h1 id=&quot;apologies&quot;&gt;Apologies&lt;&#x2F;h1&gt;
&lt;p&gt;Sorry for taking so long to post again, fearless reader; I’ve recently moved to
a new house, so most of my outside-of-work-energy has been spent dealing with
all that entails.
This also means that this post is probably not as polished as I may have hoped.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;intro&quot;&gt;Intro&lt;&#x2F;h1&gt;
&lt;p&gt;While attempting to port some academic code to a, shall we say, cleaner setup
for work, I had some trouble with implementing a piece of somewhat low-level
code that works with finite fields.
Finite fields are important, and the algorithms involved aren’t too terribly
difficult to understand, as these things go.
That being said, I find that they’re surprisingly tricky to implement well.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;finite-fields-are-important&quot;&gt;Finite Fields are Important&lt;&#x2F;h1&gt;
&lt;p&gt;Finite fields are interesting pieces of math in their own right, and involve
interesting topics and ideas like Euclid’s Algorithm and Galois Theory.
They’re quite important for other work, too, since they tend to form the
foundation upon which other ideas and structures build; akin to, say, how high
school geometry talks about points made of pairs of real numbers.&lt;&#x2F;p&gt;
&lt;p&gt;Finite fields have quite important real-world applications, too.
For one example, they form the underlying primitives upon which much of modern
cryptography is based; bitcoin wouldn’t be possible without finite fields,
since it uses
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.bitcoin.it&#x2F;wiki&#x2F;Elliptic_Curve_Digital_Signature_Algorithm&quot;&gt;ECDSA&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;with-that-said-they-re-hard-to-implement&quot;&gt;With that said, they’re hard to implement&lt;&#x2F;h1&gt;
&lt;p&gt;Finite fields are slightly tricky to implement, though, because they are
basically parameterized by some information (the prime characteristic in the
prime field case, and the polynomial modulus in the general case).
Prior to doing any computation within a given field, you have to know exactly
which field you’re dealing with.&lt;&#x2F;p&gt;
&lt;p&gt;Suppose you wanted to write some software to compute in a prime field; what
approach would you take to storing&#x2F;initializing the characteristic?
Here are some existing options:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.shoup.net&#x2F;ntl&#x2F;&quot;&gt;NTL’s&lt;&#x2F;a&gt; approach: global initialization of
finite field element type, &lt;code&gt;ZZ_p::init(p)&lt;&#x2F;code&gt;; this is what I used for &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;e2c2&quot;&gt;the
code&lt;&#x2F;a&gt; portion of &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;dissert_and_defense&quot;&gt;my
dissertation&lt;&#x2F;a&gt;.
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.flintlib.org&#x2F;&quot;&gt;FLINT’s&lt;&#x2F;a&gt; &lt;code&gt;f_q&lt;&#x2F;code&gt; stuff looks similar, though I
haven’t used it.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;HaskellForMaths-0.4.5&#x2F;docs&#x2F;Math-Core-Field.html&quot;&gt;Haskell For
Maths&lt;&#x2F;a&gt;
approach: use &lt;code&gt;newtype&lt;&#x2F;code&gt;s for some small examples (&lt;code&gt;F2, F3, F25&lt;&#x2F;code&gt;, etc.),
including extension fields.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;msakai&#x2F;finite-field&quot;&gt;Finite&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;finite-field&quot;&gt;Fields&lt;&#x2F;a&gt; Haskell
package: type level numbers to hold the characteristic&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.sagemath.org&#x2F;&quot;&gt;Sage&lt;&#x2F;a&gt; approach: on the surface, there is a
class within which you do the work, like &lt;code&gt;GF(17)(3 * 5)&lt;&#x2F;code&gt;.
Behind the scenes, however, this is implemented by (depending on the version
of Sage and the finite field in question) a package like NTL or FLINT (both
mentioned above)&lt;&#x2F;li&gt;
&lt;li&gt;The elegant &lt;code&gt;with-modulus&lt;&#x2F;code&gt; Scheme macro from &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;programmingpraxis.com&#x2F;2009&#x2F;07&#x2F;07&#x2F;modular-arithmetic&#x2F;&quot;&gt;this Programming
Praxis&lt;&#x2F;a&gt;
exercise.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;All of these approaches have their drawbacks, however:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;NTL requires initializing the field globally &lt;em&gt;before&lt;&#x2F;em&gt; any usage.
I had to chase down crashes early on in writing code for my thesis; it
wasn’t fun&lt;&#x2F;li&gt;
&lt;li&gt;This package only defines a few fields, not a general parameterized way of
easily working over &lt;em&gt;any&lt;&#x2F;em&gt; prime field&lt;&#x2F;li&gt;
&lt;li&gt;This package is quite nice, but only works for prime fields; I have no idea
how to work with, e.g., \(\mathbb{F}_{2^8}\) for
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Advanced_Encryption_Standard&quot;&gt;AES&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;This is nice on the surface, but that nice interface hides a &lt;em&gt;lot&lt;&#x2F;em&gt; of
different ways of doing things.
It also requires the field object to know about the element object and the
element object to simultaneously know about its parent field object…which
can lead to development headaches.&lt;&#x2F;li&gt;
&lt;li&gt;This is a great way to go, since it clearly marks what field you’re working
with and delimits the scope of those computations nicely—but how many
people know how to effectively craft macros?&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;And all of these problems are only regarding working in a single field!
What about a tower of extensions, or working with other isomorphisms between
fields?&lt;&#x2F;p&gt;
&lt;h1 id=&quot;moral&quot;&gt;Moral&lt;&#x2F;h1&gt;
&lt;p&gt;Math is full of useful stuff, but it may not be readily apparent how best to
implement those things in a programming language.
In the finite field case, the issue (for me) boils down to effectively dealing
with the extra information that parameterizes our field.
And a lot of potentially useful mathematical objects similarly require knowing
some parameterizing information.
For instance, to work with, say, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;e2c2&quot;&gt;points on binary Edwards
curves&lt;&#x2F;a&gt;, there are several layers of this going
on: points are parameterized by their coordinates and the curve on which they
lie.
Curves and coordinates, in turn, are embedded in some field; in this case, an
extension field of \(\mathbb{F}_2\).
That extension field, as mentioned above, is parameterized by its
characteristic (2) and its modulus (some polynomial in \(\mathbb{F}_2[x]\)).
My dissertation code is one way of dealing with all of that detail, but I can’t
help but wonder if there is a better way of going about it.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>It&#x27;s Lisp Time</title>
        <published>2014-07-07T00:00:00+00:00</published>
        <updated>2014-07-07T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/its-lisp-time/"/>
        <id>https://grahamenos.com/its-lisp-time/</id>
        
        <content type="html" xml:base="https://grahamenos.com/its-lisp-time/">&lt;h1 id=&quot;porting-academic-code-is-fun-i-swear&quot;&gt;Porting academic code is fun, I swear&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve been chewing on a rather tough programming problem at work recently
involving porting code from a collection of different languages to a C++11
production-ish-looking thing.
Really, the project is just to get a cohesive version of the interesting (but
entirely academic, not remotely production-ready) code from a published paper.
Even with the &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;python-to-cxx&#x2F;&quot;&gt;previously mentioned niceties&lt;&#x2F;a&gt;
that C++11 has to offer, it’s still rough going.
And I’ve realized that it’s not just because the code is, &lt;em&gt;ahem&lt;&#x2F;em&gt;, interesting.
It’s that my tools are a bit limiting.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;everything-else-is-too-limit&quot;&gt;Everything else is too limit&lt;&#x2F;h2&gt;
&lt;p&gt;As it turns out, I’m finding more and more that the things people say singing
the praises of Scheme and Common Lisp might be on to something.
Take, for instance, Peter Seibel’s excellent book &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.gigamonkeys.com&#x2F;book&#x2F;&quot;&gt;Practical Common
Lisp&lt;&#x2F;a&gt;.
In Chapter 9, he puts together a very nice unit testing framework; it’s fully
commented and documented, has great reporting that tells you exactly what went
wrong, and offers a nice little domain specific language for writing tests.
The really awesome part: with comments, indentation, and line breaks, all this
happens in &lt;strong&gt;just 26 lines of code&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now, I know there are other &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.jera.com&#x2F;techinfo&#x2F;jtns&#x2F;jtn002.html&quot;&gt;small unit testing
frameworks&lt;&#x2F;a&gt; out there,&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; but
this is just amazing.
And it’s just the tip of the iceberg.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;all-hail-the-mighty-parentheses&quot;&gt;All hail the mighty parentheses&lt;&#x2F;h1&gt;
&lt;p&gt;My motivation for wanting more Lisp &amp;amp; Scheme in my coding is twofold, I guess;
first, my work programming, while fun and useful, is constraining in some
senses.
Pretty much any project I write will be either in C, C++, or Python.
If it’s a very data-science-y project, I might get to&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; use R.
Those languages are great, and they all have their uses, but there’s other
stuff out there, some of it &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.paulgraham.com&#x2F;avg.html&quot;&gt;wildly more
powerful&lt;&#x2F;a&gt;.
So the first reason is because I want to use more interesting and powerful
tools.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The second reason is my own edification.
Scheme and Lisp have a decently long history (as far as programming languages
are concerned), and definitely have a library of great work for me to ingest.
There’s the &lt;a href=&quot;https:&#x2F;&#x2F;grahamenos.com&#x2F;sicp-is-so-good&#x2F;&quot;&gt;previously mentioned&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;mitpress.mit.edu&#x2F;sicp&#x2F;full-text&#x2F;book&#x2F;book.html&quot;&gt;SICP&lt;&#x2F;a&gt; for Scheme.
I also hope to spend more time on &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;programmingpraxis.com&#x2F;&quot;&gt;Programming
Praxis&lt;&#x2F;a&gt;, though my attendance has been rather
poor lately.
For Lisp, I hope to read Paul Graham’s &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.paulgraham.com&#x2F;onlisp.html&quot;&gt;On
Lisp&lt;&#x2F;a&gt; and Peter Norvig’s
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Paradigms_of_AI_Programming%3A_Case_Studies_in_Common_Lisp&quot;&gt;PAIP&lt;&#x2F;a&gt;.
If I’m feeling particularly adventurous, I hope to read Doug Hoyte’s &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.letoverlambda.com&#x2F;&quot;&gt;Let over
Lambda&lt;&#x2F;a&gt;.
I’ve also been intrigued by Clojure recently,&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-4-1&quot;&gt;&lt;a href=&quot;#fn-4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; and have been paging through some
of Fogus and Houser’s &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;joyofclojure.com&#x2F;&quot;&gt;Joy of Clojure&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;comments-questions-etc&quot;&gt;Comments&#x2F;Questions&#x2F;etc.?&lt;&#x2F;h1&gt;
&lt;p&gt;Does it look like I’ve missed anything, dear reader(s)?
Please let me know if you have other recommendations, etc. for the road to Lisp
enlightenment.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;I’ve actually used &lt;code&gt;minunit&lt;&#x2F;code&gt; before, and it’s great for C code. The point is, however, the framework from Seibel’s book is simultaneously more powerful, more expressive, and easier to write tests in (to strand a preposition). &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;Be forced to? &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;And on one work project I was forced (for reasons I’ll not go into, but not of my choosing) to use &lt;em&gt;Perl&lt;&#x2F;em&gt;. Kidding aside, it can be a useful tool for some things, but I assure you that what I was asked to build with it is &lt;strong&gt;not&lt;&#x2F;strong&gt; one of those things. &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-4&quot;&gt;
&lt;p&gt;I’ll admit I’m a bit reticent about using the JVM, but Clojure has some nice features, so I’m trying to keep an open mind. I’m particularly interested in &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;incanter.org&#x2F;&quot;&gt;Incanter&lt;&#x2F;a&gt; as a possible replacement for R and Python for data work. &lt;a href=&quot;#fr-4-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Unusual Tools in Data Science</title>
        <published>2014-05-28T00:00:00+00:00</published>
        <updated>2014-05-28T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/unusual-tools-in-data-science/"/>
        <id>https://grahamenos.com/unusual-tools-in-data-science/</id>
        
        <content type="html" xml:base="https://grahamenos.com/unusual-tools-in-data-science/">&lt;h1 id=&quot;isn-t-all-science-data-science&quot;&gt;Isn’t &lt;em&gt;all&lt;&#x2F;em&gt; science “Data Science?”&lt;&#x2F;h1&gt;
&lt;p&gt;I’m an applied research mathematician in my day job, which on a day-to-day
basis puts me somewhere between mathematician, software developer, machine
learning practitioner, and statistician.
In other words, I’m probably what’s called a &lt;em&gt;Data Scientist&lt;&#x2F;em&gt; nowadays.
See the image below for how some people feel about that term;&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; I’m a bit
ambivalent about it myself.&lt;&#x2F;p&gt;
&lt;p&gt;It seems to be a bit trendy and not have much of a solid definition.
Some people even think &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;blogs.wsj.com&#x2F;cio&#x2F;2014&#x2F;04&#x2F;30&#x2F;its-already-time-to-kill-the-data-scientist-title&#x2F;&quot;&gt;the title should be killed
off&lt;&#x2F;a&gt;
entirely.
That being said,&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;I’m mostly OK with applying a trendy label to myself&lt;&#x2F;li&gt;
&lt;li&gt;I don’t have a better term for the “person who uses software to do
interesting things with data” right now&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;so I’ll stick with it for the time being.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;d262ilb51hltx0.cloudfront.net&#x2F;max&#x2F;800&#x2F;1*194NFTNMbRww0eTlhHfqlQ.png&quot; alt=&quot;Data Scientist&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;a-series-idea&quot;&gt;A series idea&lt;&#x2F;h1&gt;
&lt;p&gt;In my time working in “data science,” I’ve noticed that there are a number of
interesting tools out there that tend to go unnoticed.
Perhaps it’s because people that fit under the data science umbrella have wide
and varied backgrounds, but there seems to be a lack of agreement of best
practices—or a lack of following best practices even if folks agree.
Moreover, there doesn’t seem to be much awareness about the wide array of new,
interesting, or just plain different (computational) tools that are out
there.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;p&gt;There are a number of tools out there that can solve certain problems much more
cleanly, elegantly, or efficiently than the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.r-project.org&#x2F;&quot;&gt;ones&lt;&#x2F;a&gt;
people
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.kaggle.com&#x2F;wiki&#x2F;GettingStartedWithPythonForDataScience&quot;&gt;usually&lt;&#x2F;a&gt;
reach for.
Though of course the usual tools have a lot of merit—&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;shop.oreilly.com&#x2F;product&#x2F;0636920023784.do&quot;&gt;this
book&lt;&#x2F;a&gt; is particularly
nice—there’s definitely room for more tools in the data science tool belt.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;post-the-first&quot;&gt;Post the first&lt;&#x2F;h1&gt;
&lt;p&gt;The first tool I’ll mention is Haskell for parsing various types of logs and
files.
Several times I’ve ended up having to parse a log or other type of file that,
while it follows a specific format, isn’t one of the usual suspects like SQL or
CSV.
Rather than reaching for regexes,&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; I contend that this is exactly the type
of problem that Haskell—with
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;parsec&quot;&gt;Parsec&lt;&#x2F;a&gt; or (even better)
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;attoparsec&quot;&gt;Attoparsec&lt;&#x2F;a&gt;—can easily
handle.&lt;&#x2F;p&gt;
&lt;p&gt;I won’t make this post into an introduction to parsing with
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;bos&#x2F;attoparsec&quot;&gt;Attoparsec&lt;&#x2F;a&gt;; there are
already
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.fpcomplete.com&#x2F;school&#x2F;starting-with-haskell&#x2F;libraries-and-frameworks&#x2F;text-manipulation&#x2F;attoparsec&quot;&gt;great&lt;&#x2F;a&gt;
intros out there that do a better job than I could.
I’ll just echo the
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;newartisans.com&#x2F;2012&#x2F;08&#x2F;parsing-with-haskell-and-attoparsec&#x2F;&quot;&gt;experiences&lt;&#x2F;a&gt;
of others out there and let you know that parsing ad hoc (or just new) file
formats with Attoparsec leads to solutions that are&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;easier to read than regexes&lt;&#x2F;li&gt;
&lt;li&gt;easier to reason about than regexes&lt;&#x2F;li&gt;
&lt;li&gt;easier to come back to and work on&#x2F;extend later than regexes&lt;&#x2F;li&gt;
&lt;li&gt;… and typically &lt;em&gt;very&lt;&#x2F;em&gt; fast&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Moral of the story: if you’re doing data science, don’t be afraid to use
different tools than you’re used to; they just might lead to better solutions.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;The post from which I &lt;s&gt;stole&lt;&#x2F;s&gt; borrowed that picture is also &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20150309222357&#x2F;http:&#x2F;&#x2F;www.slantedwindows.com&#x2F;why-you-already-are-a-data-scientist-its-just-may-not-be-your-job-title&#x2F;&quot;&gt;a pretty good read.&lt;&#x2F;a&gt; &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;Or perhaps that’s just at my workplace. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;Which I’ve also done because Haskell wasn’t an option (the people I was working with &lt;strong&gt;only&lt;&#x2F;strong&gt; liked &lt;em&gt;Perl&lt;&#x2F;em&gt;), and it lead to some horrific circumstances similar to &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1732348&#x2F;regex-match-open-tags-except-xhtml-self-contained-tags&#x2F;1732454#1732454&quot;&gt;this&lt;&#x2F;a&gt;. &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Group Theory AND Statistics</title>
        <published>2014-02-27T00:00:00+00:00</published>
        <updated>2014-02-27T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/group-theory-and-statistics/"/>
        <id>https://grahamenos.com/group-theory-and-statistics/</id>
        
        <content type="html" xml:base="https://grahamenos.com/group-theory-and-statistics/">&lt;h1 id=&quot;diaconis-does-something-cool-video-at-11&quot;&gt;Diaconis does something cool, video at 11&lt;&#x2F;h1&gt;
&lt;p&gt;&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;statweb.stanford.edu&#x2F;~cgates&#x2F;PERSI&#x2F;&quot;&gt;Persi&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Persi_Diaconis&quot;&gt;Diaconis&lt;&#x2F;a&gt; is an interesting
character. He dropped out of high school at 14 to become a professional
magician, got a Ph.D. from Harvard, won a MacArthur “genius grant,” and is now
a mathematician and statistician at Stanford University.&lt;&#x2F;p&gt;
&lt;p&gt;He also does interesting math; today I’ll talk about an example from
a paper&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; of his that I found particularly clever.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;counting-voter-rankings&quot;&gt;Counting voter rankings&lt;&#x2F;h1&gt;
&lt;p&gt;Suppose we are holding an election between five candidates, and we ask voters
to rank all five in order of preference. We’d like to take those rankings of
the five candidates and turn it into a table where the &lt;em&gt;(i, j)&lt;&#x2F;em&gt; entry is the
number of voters who ranked candidate &lt;em&gt;i&lt;&#x2F;em&gt; in place &lt;em&gt;j&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Note that a ranking of five candidates can be thought of as a permutation of
length five, an element of the group \( S_5 \). That is, the permutation
\((5 4 3 2 1)\) corresponds to ranking candidate 5 first, candidate 4 second,
etc. Next, we use the usual linear representation of \( S_5 \); for any given
permutation \( \pi \), let \( \rho(\pi) \) be the 5 x 5 matrix with &lt;em&gt;(i,
j)&lt;&#x2F;em&gt; entry equal to one if \( \pi(i) = j \), zero otherwise.&lt;&#x2F;p&gt;
&lt;p&gt;Define a function \( f: S_5 \rightarrow \mathbb{N} \) such that
\( f(\pi) \) is the number times the vote \( \pi \) was cast. Hence, if 10
people all voted \((5 4 3 2 1)\), then \( f( (5 4 3 2 1) ) = 10 \). Now,
take the Fourier Transform
\[
\hat{f} = \sum_{\pi} f(\pi) \rho(\pi)
\]
Note that \( \hat{f} \) adds up the matrix for each vote times the number
of times that vote appeared; this means that \( \hat{f} \) gives us
a “table” (really a matrix) where the &lt;em&gt;(i, j)&lt;&#x2F;em&gt; entry is the number of times
voters ranked candidate &lt;em&gt;i&lt;&#x2F;em&gt; in place &lt;em&gt;j&lt;&#x2F;em&gt;. This is exactly what we want!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;pithy-python-program-performs-promptly&quot;&gt;Pithy Python program performs promptly&lt;&#x2F;h1&gt;
&lt;p&gt;Here’s some &lt;code&gt;Python&lt;&#x2F;code&gt; code that computes this.&lt;&#x2F;p&gt;
&lt;p&gt;First off, some useful imports.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; collections&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; Counter&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; itertools&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; permutations&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span&gt; numpy&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; as&lt;&#x2F;span&gt;&lt;span&gt; np&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, we define our representation function \( \rho \).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; rho&lt;&#x2F;span&gt;&lt;span&gt;(p):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span&gt; np.matrix([np.eye(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(p),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; dtype&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;)[:, i]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; p])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After that, some (wicked short!) code to compute the transform
\( \hat{f} \).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; fft&lt;&#x2F;span&gt;&lt;span&gt;(ps):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    c&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; Counter(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;tuple&lt;&#x2F;span&gt;&lt;span&gt;(p)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; p&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; ps)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(rho(p)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; c[p]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; p&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; c)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The following code also works, but it doesn’t technically follow the definition
above since it hides \( f \).&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; fft2&lt;&#x2F;span&gt;&lt;span&gt;(ps):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;(rho, ps))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A quick function to normalize a table of counts to percentages instead:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; table&lt;&#x2F;span&gt;&lt;span&gt;(xss):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 100&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; xss&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &#x2F;&lt;&#x2F;span&gt;&lt;span&gt; np.sum(xss,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; axis&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And a quick check to see it in action:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __name__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;__main__&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    N&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    S_N&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; np.array(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(permutations(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;range&lt;&#x2F;span&gt;&lt;span&gt;(N))))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ps&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; S_N&lt;&#x2F;span&gt;&lt;span&gt;[np.random.randint(np.factorial(N),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; size&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;500&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    fhat&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; fft(ps)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;Permutations:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\n{0}\n\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;FFT:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\n{1}\n\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;Percentages:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;\n{2}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.format(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            ps, fhat, table(fhat)))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;fin&quot;&gt;Fin.&lt;&#x2F;h1&gt;
&lt;p&gt;I’m very excited about this; using group theory to better understand statistics
and machine learning is exactly what I’ve been hoping to do since I finished
grad school.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;“Applications of Group Representations to Statistical Problems,” 1990; available &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.mathunion.org&#x2F;ICM&#x2F;ICM1990.2&#x2F;Main&#x2F;icm1990.2.1037.1048.ocr.pdf&quot;&gt;here.&lt;&#x2F;a&gt; &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>N Queens Problem</title>
        <published>2013-11-20T00:00:00+00:00</published>
        <updated>2013-11-20T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/n-queens/"/>
        <id>https://grahamenos.com/n-queens/</id>
        
        <content type="html" xml:base="https://grahamenos.com/n-queens/">&lt;h1 id=&quot;coding-for-interviews&quot;&gt;Coding for Interviews&lt;&#x2F;h1&gt;
&lt;p&gt;In order to up my programming chops, I’ve signed up for
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;codingforinterviews.com&#x2F;&quot;&gt;this newsletter&lt;&#x2F;a&gt;. It’s been fun,
though I don’t typically have the time&#x2F;energy to answer the questions.
Somewhat serendipitously, though, they sent out a question that I’ve
poked my nose into recently: the
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Eight_queens_puzzle&quot;&gt;N Queens Puzzle&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;my-solution&quot;&gt;My Solution&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve taken the approach this newsletter seems to prefer: writing my
thinking and solution out by hand. See my write up
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;imgur.com&#x2F;a&#x2F;piSjE&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;critiquing-your-work&quot;&gt;Critiquing Your Work&lt;&#x2F;h1&gt;
&lt;p&gt;As I note in my write up, this isn’t the best solution possible. In
fact, my approach is asymptotically just as bad as the naive approach,
basically, by
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Stirling&amp;#x27;s_approximation&quot;&gt;Stirling’s Approximation&lt;&#x2F;a&gt;.
I was going for shorter code that relied more on the Python standard
library, though.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>HyperLogLog is Wicked Cool</title>
        <published>2013-10-16T00:00:00+00:00</published>
        <updated>2013-10-16T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/hyperloglog-is-wicked-cool/"/>
        <id>https://grahamenos.com/hyperloglog-is-wicked-cool/</id>
        
        <content type="html" xml:base="https://grahamenos.com/hyperloglog-is-wicked-cool/">&lt;h1 id=&quot;how-to-count-without-counting&quot;&gt;How to count without counting&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve been messing about with a few different things lately, one of which is the
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;algo.inria.fr&#x2F;flajolet&#x2F;Publications&#x2F;FlFuGaMe07.pdf&quot;&gt;HyperLoglog
algorithm&lt;&#x2F;a&gt;.
Others have
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;blog.aggregateknowledge.com&#x2F;2012&#x2F;10&#x2F;25&#x2F;sketch-of-the-day-hyperloglog-cornerstone-of-a-big-data-infrastructure&#x2F;&quot;&gt;already&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;blog.aggregateknowledge.com&#x2F;tag&#x2F;hyperloglog&#x2F;&quot;&gt;written&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;metamarkets.com&#x2F;2012&#x2F;fast-cheap-and-98-right-cardinality-estimation-for-big-data&#x2F;&quot;&gt;about&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;static.googleusercontent.com&#x2F;external_content&#x2F;untrusted_dlcp&#x2F;research.google.com&#x2F;en&#x2F;us&#x2F;pubs&#x2F;archive&#x2F;40671.pdf&quot;&gt;this&lt;&#x2F;a&gt;
extensively, so I have nothing terribly new to contribute; I just wanted
briefly discuss what it is and show some code.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;in-brief&quot;&gt;In Brief&lt;&#x2F;h1&gt;
&lt;p&gt;While it’s a little &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.101zenstories.com&#x2F;index.php?story=21&quot;&gt;zen&lt;&#x2F;a&gt;
sounding, HyperLogLog is a way of counting the number of unique items—say,
words in a file—without actually counting them.
That is, it gives a (rather good) estimate of the cardinality of the set of
elements in a given multiset while taking up much less memory than
instantiating the whole set (or a hashed version thereof), and it requires only
a single pass through the data with some smaller post-processing on the end for
a runtime of \( O(N) \).&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-larger-picture&quot;&gt;The larger picture&lt;&#x2F;h1&gt;
&lt;p&gt;I think HyperLogLog is
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;bangordailynews.com&#x2F;2012&#x2F;04&#x2F;13&#x2F;living&#x2F;everybodys-heard-about-the-maine-words&#x2F;&quot;&gt;wicked&lt;&#x2F;a&gt;
cool.
More generally, though, I think it’s a great example of a type of algorithm
that deserves more exploration, especially in this era of &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;memegenerator.net&#x2F;instance&#x2F;34328226&quot;&gt;big
data&lt;&#x2F;a&gt;: algorithms that trade
resource efficiency for approximate answers.
A big part of what’s so cool about HyperLogLog is that the authors of
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;algo.inria.fr&#x2F;flajolet&#x2F;Publications&#x2F;FlFuGaMe07.pdf&quot;&gt;this&lt;&#x2F;a&gt; paper give a
highly mathematical and probabilistic description of the possible error and
then mitigate that in the final version of the algorithm.
It’s very impressive.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;code&quot;&gt;Code&lt;&#x2F;h1&gt;
&lt;p&gt;For a (slapdash?) C++11 version of the final algorithm, head
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;hyperloglog.cc&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>SICP is So Good</title>
        <published>2013-10-04T00:00:00+00:00</published>
        <updated>2013-10-04T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/sicp-is-so-good/"/>
        <id>https://grahamenos.com/sicp-is-so-good/</id>
        
        <content type="html" xml:base="https://grahamenos.com/sicp-is-so-good/">&lt;h1 id=&quot;all-shutdown-and-no-work-makes-graham-a-dull-boy&quot;&gt;All shutdown and no work makes Graham a dull boy&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve been reading &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;mitpress.mit.edu&#x2F;sicp&#x2F;&quot;&gt;SICP&lt;&#x2F;a&gt; recently in my quest
to extend my Computer Science knowledge.
This is a task I’ve attempted before but never really had the time to devote a
solid chunk of brain-power to.
However, due to &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.theonion.com&#x2F;articles&#x2F;what-a-government-shutdown-means-for-you,34052&#x2F;&quot;&gt;recent
events&lt;&#x2F;a&gt;, I find myself with more time on my hands than usual.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;h1 id=&quot;sicp-to-the-rescue&quot;&gt;SICP to the rescue&lt;&#x2F;h1&gt;
&lt;p&gt;This post will be short, but I imagine I’ll write another sometime soon as I
work through this amazing book.
Of course I’m not the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.cs.berkeley.edu&#x2F;~bh&#x2F;sicp.html&quot;&gt;only&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.amazon.com&#x2F;review&#x2F;R403HR4VL71K8&quot;&gt;one&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=1273406&quot;&gt;who&lt;&#x2F;a&gt; thinks so, but SICP is a
good book.
I mean, it’s a &lt;em&gt;really&lt;&#x2F;em&gt; good book.
Within the first chapter, you’ve already used the Fibonacci numbers,
primality testing, Newton’s Method, fixed-points and average damping, and
continued fractions to explore the benefits of having functions as &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;steve-yegge.blogspot.com&#x2F;2006&#x2F;03&#x2F;execution-in-kingdom-of-nouns.html&quot;&gt;first class
citizens&lt;&#x2F;a&gt;.
This book is amazing.
I suppose that part of what makes it so good is its use of
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;schemers.org&#x2F;&quot;&gt;Scheme&lt;&#x2F;a&gt; as its programming language; this leads to a
minimal barrier between expressing the ideas and executing them.
Even with that said, I’m still in awe of how good this book is.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;case-in-point&quot;&gt;Case In Point&lt;&#x2F;h1&gt;
&lt;p&gt;As a case in point, I’ll include a &lt;a href=&quot;&#x2F;pdfs&#x2F;continued_fraction.pdf&quot;&gt;quick write up&lt;&#x2F;a&gt; I
did to share with my Dad and his high school students.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
It was inspired by SICP’s handling of an interesting continued fraction.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;It’s not all SICP all the time for me; I have to tackle a truly impressive list of chores as well. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;I really need to get back to work. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Richard Stanley, Catalania, and Haskell</title>
        <published>2013-09-15T00:00:00+00:00</published>
        <updated>2013-09-15T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/stanley-catalan/"/>
        <id>https://grahamenos.com/stanley-catalan/</id>
        
        <content type="html" xml:base="https://grahamenos.com/stanley-catalan/">&lt;h1 id=&quot;richard-stanley-captain-of-counting&quot;&gt;Richard Stanley, Captain of Counting&lt;&#x2F;h1&gt;
&lt;p&gt;I recently received an email about a &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;math.mit.edu&#x2F;stanley70&#x2F;Site&#x2F;Home.html&quot;&gt;math
conference&lt;&#x2F;a&gt; in honor of Richard
Stanley’s 70th birthday.
For those of you who don’t know, Stanley is an expert in combinatorics and
author of the two volume magnum opus &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www-math.mit.edu&#x2F;~rstan&#x2F;ec&#x2F;&quot;&gt;Enumerative
Combinatorics&lt;&#x2F;a&gt; which (among a bunch of
other very interesting math) champions the idea of the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Combinatorial_proof&quot;&gt;combinatorial
proof&lt;&#x2F;a&gt;—a way of proving a
combinatorial identity by showing that both sides of the equation are in
different fact ways of counting the same thing.&lt;&#x2F;p&gt;
&lt;p&gt;Stanley’s Enumerative Combinatorics also goes into great detail about the
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;mathworld.wolfram.com&#x2F;CatalanNumber.html&quot;&gt;Catalan Numbers&lt;&#x2F;a&gt;, a sequence
of numbers that arises in tree enumeration problems.
In fact, Stanley’s interest in these numbers—which he dubs ’‘Catalan
disease’’ or ’‘Catalania’’—goes so far that he offers 66 combinatorial
interpretations of them in EC vol. 2 and dozens more in an addendum published
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www-math.mit.edu&#x2F;~rstan&#x2F;ec&#x2F;catadd.pdf&quot;&gt;online&lt;&#x2F;a&gt;.
See the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;oeis.org&#x2F;wiki&#x2F;Combinatorial_interpretations_of_Catalan_numbers&quot;&gt;OEIS
page&lt;&#x2F;a&gt;
for more.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;catalania-meet-haskell&quot;&gt;Catalania, meet Haskell&lt;&#x2F;h1&gt;
&lt;p&gt;In the interest of learning more Haskell, I decided to take some inspiration
and play with Catalan numbers in that decidedly mathematical programming
language.
To start off with, I have some helpful imports and auxiliary functions:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;import&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; Test.QuickCheck&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;quickCheck&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;-- helpers&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;binom&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Integer -&amp;gt; Integer -&amp;gt; Integer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;binom n k &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; product [k &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;n]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `div`&lt;&#x2F;span&gt;&lt;span&gt; product [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; k]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;factorial&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Integer -&amp;gt; Integer&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;factorial &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; product &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt; enumFromTo &lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;pairs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [a]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; [(a, a)]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;pairs xs &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; [(x, y)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; |&lt;&#x2F;span&gt;&lt;span&gt; x &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; xs, y &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;-&lt;&#x2F;span&gt;&lt;span&gt; xs]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.cse.chalmers.se&#x2F;~rjmh&#x2F;QuickCheck&#x2F;&quot;&gt;QuickCheck&lt;&#x2F;a&gt; import will be
important later.&lt;&#x2F;p&gt;
&lt;p&gt;Next, with the help of &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;rosettacode.org&#x2F;wiki&#x2F;Catalan_numbers#Haskell&quot;&gt;Rosetta
Code&lt;&#x2F;a&gt; and Wikipedia, I
have the following Catalan Number definitions:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;-- Catalan definitions&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cat0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat0 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; map (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;\&lt;&#x2F;span&gt;&lt;span&gt;n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; product [n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; n]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `div`&lt;&#x2F;span&gt;&lt;span&gt; product [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;n]) [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cat1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat1 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :&lt;&#x2F;span&gt;&lt;span&gt; map c [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  where&lt;&#x2F;span&gt;&lt;span&gt; c n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sum &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; zipWith (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;) (reverse (take n cat1)) cat1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cat2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat2 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; scanl (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;\&lt;&#x2F;span&gt;&lt;span&gt;c n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; c &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `div`&lt;&#x2F;span&gt;&lt;span&gt; (n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cat3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat3 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; map c [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  c n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;|&lt;&#x2F;span&gt;&lt;span&gt; n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;      |&lt;&#x2F;span&gt;&lt;span&gt; otherwise &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; binom (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; n) n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt; binom (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; n) (n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cat4&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat4 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; map c [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  where&lt;&#x2F;span&gt;&lt;span&gt; c n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; sum (map (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;\&lt;&#x2F;span&gt;&lt;span&gt;k &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binom n k &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;^&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;) [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;n])&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `div`&lt;&#x2F;span&gt;&lt;span&gt; (n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cat5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat5 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; map (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;\&lt;&#x2F;span&gt;&lt;span&gt;n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; binom (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; n) n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;`div`&lt;&#x2F;span&gt;&lt;span&gt; (n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)) [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;cat6&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ::&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;Integer&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;cat6 &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; map c [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  where&lt;&#x2F;span&gt;&lt;span&gt; c n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; factorial (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; n)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; `div`&lt;&#x2F;span&gt;&lt;span&gt; (factorial (n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; factorial n)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, we’d like to show (or at least, verify for a certain number of test
cases) that these varying definitions all describe the same thing; to do that,
I decided to use a bit of QuickCheck’s magic to test various items from these
lists for equality:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;-- Check that these look the same&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;prop_equal&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: Int -&amp;gt; Bool&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;prop_equal n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; all p &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;$&lt;&#x2F;span&gt;&lt;span&gt; pairs cats&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;  where&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cats &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; [cat0, cat1, cat2, cat3, cat4, cat5, cat6]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    p (xs, ys)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; xs &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;!!&lt;&#x2F;span&gt;&lt;span&gt; n&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;==&lt;&#x2F;span&gt;&lt;span&gt; ys &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;!!&lt;&#x2F;span&gt;&lt;span&gt; n&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    n&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; n &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;`mod`&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1000&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; :: IO&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; ()&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; quickCheck prop_equal&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This isn’t perhaps the most elegant check—note that I restrict the integers
tested to \( \lbrace 0, 1, \ldots, 999 \rbrace
= \phantom{ }^\mathbb{Z} &#x2F;_{1000 \mathbb{Z}} \) to make sure
that indices are nonnegative and we don’t overtax my poor laptop’s CPU.
Of course, I’m open to criticism from any Haskell&#x2F;QuickCheck gurus who’d like
to help me tighten up my code.
Feel free to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;Workbench&#x2F;blob&#x2F;main&#x2F;catalan.hs&quot;&gt;download the
code&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;to-be-continued&quot;&gt;To Be Continued&lt;&#x2F;h1&gt;
&lt;p&gt;That was fun; programming in Haskell almost always is, for me, and I’m by no
means an expert.
However, it leaves a little to be desired.
The program above at best demonstrates that the various infinite lists agree at
several (pseudo-)randomly chosen indices.
I suppose that taking this to the next level would involve some sort of
automatic theorem proving
assistant—&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;wiki.portal.chalmers.se&#x2F;agda&#x2F;pmwiki.php&quot;&gt;Agda&lt;&#x2F;a&gt; comes to
mind, due to its relationship with Haskell.
That being said, using something like Agda will at best give me &lt;em&gt;algebraic&lt;&#x2F;em&gt;
proofs that \( cat_i = cat_j \) by exploiting various mathematical
properties of the definitions of two lists.
I highly doubt that it would be able to produce a &lt;em&gt;combinatorial&lt;&#x2F;em&gt; proof that
two descriptions of the Catalan numbers are the same, though I’d be very
happy to be proven wrong.&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Dear Statistics, Let&#x27;s be Friends</title>
        <published>2013-08-27T00:00:00+00:00</published>
        <updated>2013-08-27T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/dear-stats-lets-be-friends/"/>
        <id>https://grahamenos.com/dear-stats-lets-be-friends/</id>
        
        <content type="html" xml:base="https://grahamenos.com/dear-stats-lets-be-friends/">&lt;p&gt;Dear Statistics,&lt;&#x2F;p&gt;
&lt;p&gt;I apologize for not having taken you seriously until this year.
I said that you aren’t Mathematics; that’s true, but not in the (derisive) way
I meant it.
You aren’t Math in the same way that Physics isn’t Math—you sure use a whole
lot of Math to do some very interesting and important things.
Really, you are the science of data;&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; you enable us to learn and make
decisions in a noisy, uncertain world.&lt;&#x2F;p&gt;
&lt;p&gt;I changed my mind when my eyes were opened to all the useful things you can do,
of which there is a
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;List_of_fields_of_application_of_statistics&quot;&gt;whole&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.slideshare.net&#x2F;Nicolas_Nicolov&#x2F;machine-learning-14528792&quot;&gt;enormous,&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.networkworld.com&#x2F;news&#x2F;tech&#x2F;2013&#x2F;082313-whitelisting-machine-learning-273154.html&quot;&gt;gigantic,&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.scientificamerican.com&#x2F;article.cfm?id=why-bayes-rules&quot;&gt;humongous,&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.tor.com&#x2F;blogs&#x2F;2011&#x2F;06&#x2F;norvig-vs-chomsky-and-the-fight-for-the-future-of-ai&quot;&gt;impressive&lt;&#x2F;a&gt;
list.&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
In my ignorance and arrogance, I had no idea.
Fortunately, I had the honor of taking classes with some
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;inside.uncc.edu&#x2F;news&#x2F;item&#x2F;molchanov-named-ams-fellow&quot;&gt;incredible&lt;&#x2F;a&gt;
professors in graduate school that opened my eyes to what your buddy
Probability had in store; once I stopped hating on Probability, it was only a
matter of time until I gave you a second look.&lt;&#x2F;p&gt;
&lt;p&gt;By and large, though, one of the biggest things that helped me appreciate you
better is my developing programming abilities.
Rather than working by hand, I can now use things like
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.r-project.org&#x2F;&quot;&gt;R&lt;&#x2F;a&gt; to, say, flip a billion unbiased virtual coins
in the blink of an eye.
Let’s be honest, you involve a lot of computation; what better tool could I use
than a computer?&lt;&#x2F;p&gt;
&lt;p&gt;That last thought reminds me: arguably one of the reasons I never took you
seriously is the poor way in which you’re taught in schools.
I think you are subject to the problems mentioned in &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.maa.org&#x2F;external_archive&#x2F;devlin&#x2F;LockhartsLament.pdf&quot;&gt;Lockhart’s
Lament&lt;&#x2F;a&gt; even
more than Math is.
A lot of teachers seem to think you consist solely of blinding applying
formulas without understanding; as a Mathematician who loves learning and
understanding things, I too lament this deplorable idea.
As &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Richard_Hamming&quot;&gt;Richard Hamming&lt;&#x2F;a&gt; once said,
the purpose of computing is insight, not numbers.
I now know it makes sense to use squared distance from the mean for variance
instead of absolute distance, because doing calculus with absolute value is a
pain.
I now know that your best friend Probability is actually
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.kolmogorov.com&#x2F;Foundations.html&quot;&gt;Analysis&lt;&#x2F;a&gt; in disguise.
Of course, there isn’t a need to know graduate level Analysis to successfully
apply you, but I sure feel better knowing why I’m doing what I’m doing.
There has to be a happy medium somewhere closer to developing understanding
than what passes for Math &amp;amp; Stats education in most&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; schools nowadays.&lt;&#x2F;p&gt;
&lt;p&gt;To sum up, Statistics: I apologize. Let’s be friends.&lt;&#x2F;p&gt;
&lt;p&gt;Best,&lt;&#x2F;p&gt;
&lt;p&gt;Graham&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;I’m avoiding calling you “Data Science,” since that’s too trendy, but you really are. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;I claim that if Machine Learning isn’t a proper sub-discipline of Statistics, it at least uses so much statistical knowledge that it might as well be counted as a win. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;Most American schools, perhaps? My perspective is limited to my own country. &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Python to C++; a silly idea?</title>
        <published>2013-08-12T00:00:00+00:00</published>
        <updated>2013-08-12T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/python-to-cxx/"/>
        <id>https://grahamenos.com/python-to-cxx/</id>
        
        <content type="html" xml:base="https://grahamenos.com/python-to-cxx/">&lt;h1 id=&quot;edit&quot;&gt;EDIT&lt;&#x2F;h1&gt;
&lt;p&gt;In the more than ten years since I first wrote this,&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;I have learned a lot about programming, programming languages, compilers, interpreters, and more; and&lt;&#x2F;li&gt;
&lt;li&gt;The ecosystem around this kind of idea has changed a lot! See, for instance, &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Mojo_(programming_language)&quot;&gt;&lt;code&gt;mojo&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;I’ll leave the rest of this post untouched; please forgive past Graham’s naivete!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;python-type-annotations&quot;&gt;Python type annotations&lt;&#x2F;h1&gt;
&lt;p&gt;In the 3.x series of the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;python.org&quot;&gt;Python&lt;&#x2F;a&gt; language, there’s a
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;ceronman.com&#x2F;2013&#x2F;03&#x2F;12&#x2F;a-powerful-unused-feature-of-python-function-annotations&#x2F;&quot;&gt;powerful,
unused&lt;&#x2F;a&gt;
feature in the standard: &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-3107&#x2F;&quot;&gt;type
annotations&lt;&#x2F;a&gt;.
As they currently stand, type annotations don’t seem to do anything except set
some attributes of the function to which they’re applied; however, I’ve been
thinking of a possible use for them: compiling Python to C++.&lt;&#x2F;p&gt;
&lt;p&gt;I’m aware of the amazing &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;cython.org&#x2F;&quot;&gt;Cython&lt;&#x2F;a&gt; project, and
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.swig.org&#x2F;&quot;&gt;various&lt;&#x2F;a&gt; &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;docs.python.org&#x2F;2&#x2F;extending&#x2F;&quot;&gt;other&lt;&#x2F;a&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;cens.ioc.ee&#x2F;projects&#x2F;f2py2e&#x2F;&quot;&gt;options&lt;&#x2F;a&gt; when it comes to compiling code
for faster Python runtimes.
There’s even &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;shedskin&#x2F;&quot;&gt;Shed Skin&lt;&#x2F;a&gt;, a compiler that
takes much of the 2.x Python standard to C++ already.
Thus, this idea is nothing more than an excuse for me to learn more computer
science, specifically compilers (and lexers and parsers, oh my!).
However, besides Python 3.x’s type annotations, there’s one other feature none
of these options use:&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.stroustrup.com&#x2F;C++11FAQ.html&quot;&gt;C++11&lt;&#x2F;a&gt;, the new C++ standard.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;c-11&quot;&gt;C++11&lt;&#x2F;h1&gt;
&lt;p&gt;C++11 is quite interesting; Bjarne Stroustrup, the creator of C++, says&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Surprisingly, C++11 feels like a new language: The pieces just fit together
better than they used to and I find a higher-level style of programming more
natural than before and as efficient as ever.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;There’s a huge amount of things that have changed in the new standard, but some
of my favorites are &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C%2B%2B11#Type_inference&quot;&gt;type
inference&lt;&#x2F;a&gt;, &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C%2B%2B11#Lambda_functions_and_expressions&quot;&gt;lambda
functions&lt;&#x2F;a&gt;,
and &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C%2B%2B11#Range-based_for_loop&quot;&gt;ranged-based for
loops&lt;&#x2F;a&gt;.  These new
features make C++ a much more pleasant language to work with, in my opinion; in
fact, I like it so much that I used it for the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;genos&#x2F;e2c2&quot;&gt;code
portion&lt;&#x2F;a&gt; of my dissertation.&lt;&#x2F;p&gt;
&lt;p&gt;The above-mentioned type inference, coupled with &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;C%2B%2B11#Alternative_function_syntax&quot;&gt;alternative function
syntax&lt;&#x2F;a&gt;,
are what gave me the (probably silly) idea of trying to compile Python 3.x code
to C++11 code.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;suggestive-examples&quot;&gt;Suggestive examples&lt;&#x2F;h1&gt;
&lt;p&gt;Here are some slightly suggestive code examples that got me thinking.
The idea would be to somehow compile (perfectly valid) Python 3 code like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span&gt;(x:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;, y:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;into equivalent&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; C++11 code:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; f&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; y&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; int&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; x &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; y;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Here’s a slightly more involved example: what if we could compile a (&lt;em&gt;very&lt;&#x2F;em&gt;
tiny) Python class&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; R2Point&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;object&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;    __slots__&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;#39;x&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt; &amp;#39;y&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __init__&lt;&#x2F;span&gt;&lt;span&gt;(self, x:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;, y:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; None&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;        self&lt;&#x2F;span&gt;&lt;span&gt;.y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; y&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    def&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; __add__&lt;&#x2F;span&gt;&lt;span&gt;(self, other:R2Point) -&amp;gt; R2Point:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; R2Point(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;.x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; other.x,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; self&lt;&#x2F;span&gt;&lt;span&gt;.y&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; other.y)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;into a C++11 struct&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; R2Point&lt;&#x2F;span&gt;&lt;span&gt; {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    int&lt;&#x2F;span&gt;&lt;span&gt; x, y;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;    auto operator&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;R2Point&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; other&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; R2Point {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;        return&lt;&#x2F;span&gt;&lt;span&gt; R2Point{x &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; other.x, y &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; other.y};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;};&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can even handle Python lambdas&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, to some extent.
This,&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; = lambda&lt;&#x2F;span&gt;&lt;span&gt; x:&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;xs&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; xrange&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ys&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; x&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; xs:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ys.append(f(x))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;sum&lt;&#x2F;span&gt;&lt;span&gt;(ys))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;might become&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;typename&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; auto&lt;&#x2F;span&gt;&lt;span&gt; f &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; [](&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;const&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; T&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;amp;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt; x&lt;&#x2F;span&gt;&lt;span&gt;) {&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; return&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; x; }&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span&gt; xs &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;vector&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;iota&lt;&#x2F;span&gt;&lt;span&gt;(xs.begin, xs.end,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span&gt; ys &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::vector&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;decltype&lt;&#x2F;span&gt;&lt;span&gt;(xs.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;front&lt;&#x2F;span&gt;&lt;span&gt;())&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span&gt; x: xs) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    ys.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;push_back&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span&gt;(x));&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;accumulate&lt;&#x2F;span&gt;&lt;span&gt;(ys.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;begin&lt;&#x2F;span&gt;&lt;span&gt;(), ys.&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;(),&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Admittedly, these aren’t very involved examples; the code is quite short, and
they don’t involve translating Python idioms like list comprehensions into C++.
I still find them suggestive, though (at least, the first two).&lt;&#x2F;p&gt;
&lt;h1 id=&quot;something-else&quot;&gt;Something else&lt;&#x2F;h1&gt;
&lt;p&gt;Like I said, these aren’t terribly involved examples and don’t demonstrate
changing idiomatic Python code into idiomatic C++11 code, really.
If the Python to C++ idea doesn’t pan out, though, there’s always the option of
something analogous to the relationship between
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;coffeescript.org&#x2F;&quot;&gt;CoffeeScript&lt;&#x2F;a&gt; and
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;JavaScript&quot;&gt;JavaScript&lt;&#x2F;a&gt;.
Since one of the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;gigamonkeys.wordpress.com&#x2F;2009&#x2F;10&#x2F;16&#x2F;coders-c-plus-plus&#x2F;&quot;&gt;common
criticisms&lt;&#x2F;a&gt; of
C++ is that it is too complex, a simpler front end that compiles to a C++
back end might be interesting.
That being said, I think that starting fresh with idiomatic C++11 might be
enough of a simplification in some cases.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;in-the-long-run&quot;&gt;In the long run&lt;&#x2F;h1&gt;
&lt;p&gt;In the long run, I doubt this idea is really going to take off; Pythonistas
like Python for their reasons, and C++ coders like their language for their
own.
More than anything, any potential transpiler of Python to C++ I develop would
be a chance for me to learn more about how programming languages are
implemented and how compilation works.
I had a lot of fun reading &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;createyourproglang.com&#x2F;&quot;&gt;this&lt;&#x2F;a&gt; stellar book,
and might take its approach to this problem.
I thought it was interesting how similar the syntax of the two languages
can look in their latest versions, and that got me wondering.
If I take this idea anywhere, I’ll be sure to write about it.&lt;&#x2F;p&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;As far as my cursory research has shown; please feel free to correct me if I’m wrong! &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;Well, largely equivalent; Python has arbitrarily large integers, which C++ can’t match natively (even using &lt;code&gt;intmax_t&lt;&#x2F;code&gt;). &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;Which are &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;c2.com&#x2F;cgi&#x2F;wiki?PythonProblems&quot;&gt;weak&lt;&#x2F;a&gt;—some might say &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;math.andrej.com&#x2F;2009&#x2F;04&#x2F;09&#x2F;pythons-lambda-is-broken&#x2F;&quot;&gt;broken&lt;&#x2F;a&gt;—compared to C++11’s (or Lisp’s, Scheme’s, Haskell’s, etc.). &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Find X[i] = i in an array (Programming Praxis)</title>
        <published>2013-08-06T00:00:00+00:00</published>
        <updated>2013-08-06T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/programming-praxis-find-array/"/>
        <id>https://grahamenos.com/programming-praxis-find-array/</id>
        
        <content type="html" xml:base="https://grahamenos.com/programming-praxis-find-array/">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;p&gt;The inimitable &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;programmingpraxis.com&quot;&gt;Programming Praxis&lt;&#x2F;a&gt;, to which I
owe much of my programming abilities&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-1-1&quot;&gt;&lt;a href=&quot;#fn-1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;,
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;programmingpraxis.com&#x2F;2013&#x2F;07&#x2F;26&#x2F;find-xi-i-in-an-array&#x2F;&quot;&gt;posted&lt;&#x2F;a&gt; an
exercise that was seemingly simple but has some surprising depth to it.
Since it’s been a little while since the problem was posted, I have few qualms
about discussing it here.
However, don’t read on if you prefer to solve the problem yourself first.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;the-question&quot;&gt;The question&lt;&#x2F;h1&gt;
&lt;p&gt;Programming Praxis asks,&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Given an array &lt;code&gt;X[0 .. n - 1]&lt;&#x2F;code&gt; of integers sorted into ascending order with
no duplicates, find an array item that is also its index, so that &lt;code&gt;X[i] = i&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For example, &lt;code&gt;X[3] = 3&lt;&#x2F;code&gt; in the array shown below:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;i     0 1 2 3 4 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;X[i] -3 0 1 3 5 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;&#x2F;blockquote&gt;
&lt;h1 id=&quot;official-solutions&quot;&gt;Official solutions&lt;&#x2F;h1&gt;
&lt;p&gt;The first official solution is a simple linear search through the array; start
at index &lt;code&gt;0&lt;&#x2F;code&gt; and walk up until you either find an &lt;code&gt;i&lt;&#x2F;code&gt; such that &lt;code&gt;X[i] = i&lt;&#x2F;code&gt; or
hit the end of the array.
This solution, however, is dismissed in favor of a better one that uses a
binary search.
Since the array was presumed to be sorted, a binary search finds the answer in
logarithmic time instead of linear time.
The author points out that if you only stick with the linear answer in an
interview, chances are you won’t make the cut.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;a-different-approach&quot;&gt;A different approach&lt;&#x2F;h1&gt;
&lt;p&gt;I’ve been thinking about a different interview scenario in which you probably
still won’t make the cut if you stick with the linear solution.
Suppose you’re trying to claim competency in the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.r-project.org&#x2F;&quot;&gt;R statistical programming
language&lt;&#x2F;a&gt;.
If you submit the linear solution in R, you’d still fail (or at least, not
succeed with flying colors).
However, I think that the binary search approach is wrong here as well; much of
R depends on thinking over a whole collection at a time, through
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikibooks.org&#x2F;wiki&#x2F;R_Programming&#x2F;Control_Structures&quot;&gt;vectorization&lt;&#x2F;a&gt;.
For example, one can quickly ask which elements of the vector &lt;code&gt;V&lt;&#x2F;code&gt; are less than
ten by typing &lt;code&gt;V &amp;lt; 10&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Here’s my R solution in full:&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-2-1&quot;&gt;&lt;a href=&quot;#fn-2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;r&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; &amp;lt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; c&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 3&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 7&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt; seq&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;length&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #E36209;&quot;&gt;X&lt;&#x2F;span&gt;&lt;span&gt;))&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The first line simply assigns the example vector to the variable &lt;code&gt;X&lt;&#x2F;code&gt;, as we
might expect; the &lt;code&gt;c()&lt;&#x2F;code&gt; function combines or concatenates its arguments.
Next, let’s talk about the sequence function; &lt;code&gt;seq(n)&lt;&#x2F;code&gt; returns the vector &lt;code&gt;1 2 3 ... n&lt;&#x2F;code&gt;; this means that &lt;code&gt;seq(length(X))&lt;&#x2F;code&gt; gives the sequence of integers from
&lt;code&gt;1&lt;&#x2F;code&gt; to the length of our vector &lt;code&gt;x&lt;&#x2F;code&gt;, and subtracting one from it yields &lt;code&gt;0 1 2 ...  length(X) - 1&lt;&#x2F;code&gt;.
That subtraction was the first time we took advantage of R’s implicit loops;
the second time comes when we compare &lt;code&gt;X&lt;&#x2F;code&gt; to this new vector we’ve created.
The expression &lt;code&gt;X == seq(length(X)) - 1&lt;&#x2F;code&gt; yields a boolean vector which is
&lt;code&gt;True&lt;&#x2F;code&gt; wherever &lt;code&gt;X[i] == i&lt;&#x2F;code&gt; and false wherever they differ, just as we wish.
Finally, the outer &lt;code&gt;X[]&lt;&#x2F;code&gt; returns those entries in &lt;code&gt;X&lt;&#x2F;code&gt; indexed by the inner
expression, i.e. only those &lt;code&gt;X&lt;&#x2F;code&gt; values corresponding to a &lt;code&gt;True&lt;&#x2F;code&gt; value.&lt;&#x2F;p&gt;
&lt;p&gt;Though I’m not an R expert, I think this is pretty idiomatic R programming and
would be a more acceptable answer in an interview geared toward demonstrating
knowledge of the statistical programming language—I’m trying to learn,
though, so if anyone has a better solution, please let me know!
What’s more, it will probably end up being faster than the logarithmic binary
search just due to the way R is designed; it’s optimized for these vectorized
expressions.
While explicit looping and conditionals can be done in R, they tend to be much
slower.
If you reach the point where the logarithmic solution in slower R code catches
up with the linear implicit looping one, chances are your problem is big enough
that R isn’t the language to use anyway.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;brevity-and-elegance&quot;&gt;Brevity and elegance&lt;&#x2F;h1&gt;
&lt;p&gt;I also offered a solution in the &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;J_programming_language&quot;&gt;J programming
language&lt;&#x2F;a&gt; which operates
in the same way but is probably harder to read—harder for the uninitiated, at
least.
Per the Wikipedia page, J is an array-oriented functional programming language
that grew out of Kenneth Iverson’s
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;APL_(programming_language)&quot;&gt;APL&lt;&#x2F;a&gt;.
As a mathematician, I find these languages fascinating; they endeavor to be
both “executable math” and &lt;em&gt;better&lt;&#x2F;em&gt; math.
I’ll try to explain both of these points a bit, but I’m sure I won’t do them
justice.&lt;&#x2F;p&gt;
&lt;p&gt;As far as “executable math” is concerned, suppose we wanted, say, the sum of
the first 17 natural numbers.
In plain ol’ C, we’d go with something like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; sum &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; i &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;; i &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 17&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; ++&lt;&#x2F;span&gt;&lt;span&gt;i) {&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    sum &lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span&gt; i;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6F42C1;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;%d\n&lt;&#x2F;span&gt;&lt;span style=&quot;color: #032F62;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, sum);&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Expressing the same thing in J is much more declarative.
In J, &lt;code&gt;i.17&lt;&#x2F;code&gt; is the first &lt;code&gt;17&lt;&#x2F;code&gt; natural numbers.
Next, &lt;code&gt;+&#x2F;&lt;&#x2F;code&gt; is sum, since it’s plus
&lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fold_(higher-order_function)&quot;&gt;folded&lt;&#x2F;a&gt; over its
argument.
Hence, &lt;code&gt;+&#x2F;i.17&lt;&#x2F;code&gt; is the sum we want; compare that to the brevity of
\[ \sum_{i = 0}^{16} i \]
The J code says what the C loop tries to say, but much more succinctly; there’s
very little in the way of taking the mathematical expression—which in
un-typeset LaTeX reads something like “the sum from &lt;code&gt;i = 0&lt;&#x2F;code&gt; to &lt;code&gt;16&lt;&#x2F;code&gt; of
&lt;code&gt;i&lt;&#x2F;code&gt;”—and having the computer execute it.&lt;&#x2F;p&gt;
&lt;p&gt;As for the “better math” claim, APL originally grew out of a mathematical
notation that Kenneth Iverson created.
I won’t go into much detail here, but will give one example stolen from &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.vaxman.de&#x2F;publications&#x2F;apl_slides.pdf&quot;&gt;this
presentation&lt;&#x2F;a&gt;.
In APL, all evaluation proceeds from right to left; there is no order of
operations, so no need for parentheses to insure that, e.g., an addition
happens before a multiplication.
As such, Horner’s rule for polynomial evaluation of
\( a + bx + cx^2 + dx^3 + ex^4 \)
goes from this:
\[ a + x \cdot(b + x \cdot (c + x \cdot (d + x \cdot e))) \]
to this:
\[ a + x \cdot b + x \cdot c + x \cdot d + x \cdot e \]
In his 1979 Turing Award Lecture titled &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.jsoftware.com&#x2F;papers&#x2F;tot.htm&quot;&gt;Notation as a Tool of
Thought&lt;&#x2F;a&gt;, Iverson makes a strong
argument for improving mathematical and programming notation by combining them
to better serve our needs.
Though I may not be entirely sold on this just yet, it certainly is a very
interesting idea and I will definitely explore it more.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;a-j-solution&quot;&gt;A J solution&lt;&#x2F;h1&gt;
&lt;p&gt;Now for my J solution to the original question:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;(I.@:=i.&amp;amp;#) _3 0 1 3 5 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;or, if we assign the array the name &lt;code&gt;X&lt;&#x2F;code&gt;,&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;X =. _3 0 1 3 5 7&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;I.X=i.#X&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I won’t discuss the first one here (to be honest, I’m very new to J and don’t
understand it well enough).
However, the second one should look an awful lot like the R solution we saw
earlier.
Taking it in pieces, &lt;code&gt;#X&lt;&#x2F;code&gt; returns the length of the vector &lt;code&gt;X&lt;&#x2F;code&gt;.
As we saw above, &lt;code&gt;i.&lt;&#x2F;code&gt; counts from &lt;code&gt;0&lt;&#x2F;code&gt; up to one less than its argument, so
&lt;code&gt;i.#X&lt;&#x2F;code&gt; yields the vector \(\langle 0, 1, 2, \cdots \# X - 1\rangle\)
Next, &lt;code&gt;X=i.#X&lt;&#x2F;code&gt; returns a vector of ones and zeros indicating where &lt;code&gt;X&lt;&#x2F;code&gt; is equal
to this vector (i.e. where &lt;code&gt;X[i]&lt;&#x2F;code&gt; equals &lt;code&gt;i&lt;&#x2F;code&gt;).
Finally, the operator &lt;code&gt;I.X&lt;&#x2F;code&gt; uses that boolean vector to select values from &lt;code&gt;X&lt;&#x2F;code&gt;,
much as &lt;code&gt;X[]&lt;&#x2F;code&gt; did in the R solution.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;summing-up&quot;&gt;Summing up&lt;&#x2F;h1&gt;
&lt;p&gt;I think Programming Praxis is right in saying that in a typical programming
interview, the linear approach to this problem just won’t cut it.
However in a stats-geared interview, I think the logarithmic answer might be
less than stellar, too.
Though my R solution doesn’t make use of the fact that &lt;code&gt;X&lt;&#x2F;code&gt; was supposed to be
sorted (nor does my J solution), it is much more idiomatic and demonstrates a
better understanding of what R is supposed to do and what it does well.
Both of my solutions also have the added advantage of finding all possible
answers in an array, not just one.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;postscript&quot;&gt;Postscript&lt;&#x2F;h1&gt;
&lt;p&gt;In the discussion section at the end of &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;www.jsoftware.com&#x2F;papers&#x2F;FPL.htm&quot;&gt;Formalism in
Programming Languages&lt;&#x2F;a&gt;, Iverson
answers a question from Dijkstra that sounds very familiar:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Dijkstra&lt;&#x2F;em&gt;: How would you represent a more complex operation, for example,
the sum of all elements of a matrix &lt;strong&gt;M&lt;&#x2F;strong&gt; which are equal to the sum of the
corresponding row and column indices?&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;Iverson&lt;&#x2F;em&gt;:&lt;sup class=&quot;footnote-reference&quot; id=&quot;fr-3-1&quot;&gt;&lt;a href=&quot;#fn-3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; \( ++&#x2F;(\mathbf{M} = \iota^1 {\Box \atop +} \iota^1) &#x2F;&#x2F;
\mathbf{M} \)&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;section class=&quot;footnotes&quot;&gt;
&lt;ol class=&quot;footnotes-list&quot;&gt;
&lt;li id=&quot;fn-1&quot;&gt;
&lt;p&gt;Such as they are. &lt;a href=&quot;#fr-1-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-2&quot;&gt;
&lt;p&gt;I’ve changed all &lt;code&gt;xs&lt;&#x2F;code&gt; in my submitted solution to &lt;code&gt;X&lt;&#x2F;code&gt; to be more in line with the question. &lt;a href=&quot;#fr-2-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li id=&quot;fn-3&quot;&gt;
&lt;p&gt;The “box atop plus” symbol above isn’t quite right, but regretfully I don’t know how to make the appropriate APL symbol in the limited LaTeX afforded to me by MathJax &amp;amp; \( \KaTeX \). &lt;a href=&quot;#fr-3-1&quot;&gt;↩&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;&#x2F;section&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Initial Post</title>
        <published>2013-07-30T00:00:00+00:00</published>
        <updated>2013-07-30T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Graham Enos
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://grahamenos.com/initial-post/"/>
        <id>https://grahamenos.com/initial-post/</id>
        
        <content type="html" xml:base="https://grahamenos.com/initial-post/">&lt;p&gt;So… here’s my first post.
I’ve been inspired to keep a digital notebook&#x2F;record of sorts, mostly to keep
me honest about continuing my education; the theory is that if I have something
to keep me accountable, some place to post my work, then I’ll keep working.&lt;&#x2F;p&gt;
&lt;p&gt;Since this is my first post, I’m going to use this space for two things:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;as a play sandbox to see how things render&lt;&#x2F;li&gt;
&lt;li&gt;to dicuss some of the things I hope to write about&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;h1 id=&quot;sandbox&quot;&gt;Sandbox&lt;&#x2F;h1&gt;
&lt;p&gt;My father the math-teacher-extraordinaire says that Euler’s
identity \( e^{i\pi} + 1 = 0 \) is proof of the existence of God.
Here’s some &lt;code&gt;Python&lt;&#x2F;code&gt; code that tries to verify this fact:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #24292E; background-color: #FFFFFF;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;from&lt;&#x2F;span&gt;&lt;span&gt; cmath&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; import&lt;&#x2F;span&gt;&lt;span&gt; exp, pi&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(exp((&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt;j&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; pi)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #D73A49;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span style=&quot;color: #005CC5;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Due precision issues, this probably won’t come out to zero.
However, it sure looks darn close.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;serious-note&quot;&gt;Serious Note&lt;&#x2F;h1&gt;
&lt;p&gt;My dissertation focused on applying a specific type of abstract algebra to
cryptography (for the technically inclined: the use of Edwards curves,
specifically binary Edwards curves, in elliptic curve cryptography; if you have
trouble sleeping, I can provide a copy of my dissertation).
As such, I have a soft spot for computational group theory, abstract algebra in
general, and cryptography.
Though I have no problem geeking out on these topics at the drop of a hat, I’m
hoping to broaden my horizons a bit—writing a dissertation is a bit draining.
I’m particularly interested in learning more probability, statistics, and
machine learning; though as of this writing I have a passing understanding of
these topics, I hope to dive into them much more deeply.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
