<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Mathias Jean Johansen</title>
    <description>My personal website and blog.</description>    
    <link>https://mjj.io</link>
    <atom:link href="https://mjj.io/rss.xml" rel="self" type="application/rss+xml" />
    
      <item>
        <title>Type-Level Programming in TypeScript</title>
        <description>&lt;p&gt;The &lt;a href=&quot;https://www.typescriptlang.org/&quot;&gt;TypeScript&lt;/a&gt; type system is immensely
powerful, and while the language itself is, of course, Turing complete, &lt;a href=&quot;https://github.com/Microsoft/TypeScript/issues/14833&quot;&gt;so is
the type system itself&lt;/a&gt;.
This allows us to implement types that are far more sophisticated than what is
possible in most popular programming languages, as we can do computations on a
type-level which is, essentially, what characterizes type-level programming.&lt;/p&gt;

&lt;p&gt;In the following, I will demonstrate how we can utilize features of the
TypeScript type system such as &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/2/conditional-types.html&quot;&gt;conditional
types&lt;/a&gt;,
&lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/2/indexed-access-types.html&quot;&gt;indexed array
types&lt;/a&gt;,
and more to implement a type that computes a given &lt;a href=&quot;https://en.wikipedia.org/wiki/Fibonacci_number&quot;&gt;Fibonacci
number&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It should be pointed out that while it is possible to implement this, you should
consider it merely an academic exercise and not a recommendation for how to use
the type system in your production applications.&lt;/p&gt;

&lt;p&gt;Before we can implement anything, we need to have a notion of what a number is.
In type-level programming, this is most commonly modeled using &lt;a href=&quot;https://en.wikipedia.org/wiki/Peano_axioms&quot;&gt;Peano
numbers&lt;/a&gt;. We can define our Peano
numbers in the following manner:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;zero&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First, we define the number 0 with our type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;. Here, it is a type alias for
the string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;zero&quot;&lt;/code&gt;, but it could be anything. Next, we define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&lt;/code&gt; which is a
parameterized type (or &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/2/generics.html&quot;&gt;generic
type&lt;/a&gt;) that we
will use for any number above 0. With these two types, we can now define 1 as
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&amp;lt;Zero&amp;gt;&lt;/code&gt;, 2 as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt;&lt;/code&gt;, and so forth.&lt;/p&gt;

&lt;p&gt;For convenience, we will create the following types for the numbers 1 through 10
which we will use later:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;One&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Two&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;One&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Three&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Two&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Four&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Three&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Five&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Four&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Six&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Five&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Seven&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Six&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Eight&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Seven&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Nine&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Eight&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Ten&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Nine&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Correspondingly, we will define variables that match our types above. We will
use the variables later when we are going to test that our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&lt;/code&gt; type
works.&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;zero&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;One&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;zero&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Two&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;three&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Three&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;four&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Four&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;three&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;five&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Five&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;four&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// ...&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;thirteen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Ten&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;twelve&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We now have a notion of what a number is which allows us to move on to the next
part of our implementation.&lt;/p&gt;

&lt;p&gt;To implement our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&lt;/code&gt; type, we need a few essential building blocks. We
will start by defining the simplest types first.&lt;/p&gt;

&lt;p&gt;For addition to work, we need to be able to &lt;em&gt;decrement&lt;/em&gt; numbers. It will shortly
become clearer why decrementing a number is important when we want to sum two
numbers. We can define a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Decrement&lt;/code&gt; type in this way:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Decrement&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;infer&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;TypeScript supports &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#inferring-within-conditional-types&quot;&gt;inferring within conditional
types&lt;/a&gt;
which means that we can extract types from parameterized types. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; extends
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&lt;/code&gt;, we extract the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; and return it, and, otherwise, we return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Decrement&amp;lt;Succ&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; would thus be equivalent to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&amp;lt;Zero&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Similarly, to check if a type is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;, we will need a type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IsZero&lt;/code&gt; that
we define as such:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;IsZero&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With this type, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const typeChecks: IsZero&amp;lt;Zero&amp;gt; = true&lt;/code&gt; would type check while
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;const doesNotTypeCheck: IsZero&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt; = true&lt;/code&gt; would not.&lt;/p&gt;

&lt;p&gt;Next, we need to have a way to do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if/else&lt;/code&gt; expressions so we can return types
based on a given condition:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;IfElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;boolean&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; can be either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;, and if it is true, we return the type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;, and, otherwise, the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;. So a variable with the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IfElse&amp;lt;true,
&quot;foo&quot;, &quot;bar&quot;&amp;gt;&lt;/code&gt; would only type check if it was assigned to the value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;foo&quot;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Three types are missing at this point: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Equals&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add&lt;/code&gt;, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&lt;/code&gt;
type. We define &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Equals&lt;/code&gt; this way:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;infer&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;SA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;infer&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;SB&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;SA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;SB&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Checking for equality on a type-level in TypeScript is difficult to do
elegantly, so the type definition above may be less clear than the types defined
earlier in the blog post.&lt;/p&gt;

&lt;p&gt;With this definition, two types are only equal if they are both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; extends &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&lt;/code&gt;, then we recursively use our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Equals&lt;/code&gt; type with the
extracted types &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SA&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SB&lt;/code&gt; until both of the extracted types are &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;, or
one of them does not extend &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&lt;/code&gt; meaning that the two types are not equal.&lt;/p&gt;

&lt;p&gt;Lastly, we need the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add&lt;/code&gt; type to make computing Fibonacci numbers possible. The
definition for this type is:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;B&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;A&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;infer&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Decrement&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;never&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;IfElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;IsZero&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;A&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add&lt;/code&gt; works by recursively decrementing the first number, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;, and incrementing
the second number, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt;, until the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;. So to add 2 and 3, the steps
would be:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Step 1. Add&amp;lt;Succ&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt;, Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt;&amp;gt;&amp;gt; // 2 + 3
Step 2. Add&amp;lt;Succ&amp;lt;Zero&amp;gt;, Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; // 1 + 4
Step 3. Add&amp;lt;Zero, Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; // 0 + 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Once we reach &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;, we return the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; by indexing the property &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;acc&quot;&lt;/code&gt;
which is our result. In this case, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Succ&amp;lt;Zero&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/code&gt; or
simply 5.&lt;/p&gt;

&lt;p&gt;All that is left now is to implement the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&lt;/code&gt; type which is defined as
follows:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;One&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F0&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Succ&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;infer&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Decrement&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Add&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;F0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;F1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;never&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;IfElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In our type parameter list, we first have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; which represents the nth number in
the Fibonacci sequence we want to find. This type parameter is then followed by
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F1&lt;/code&gt; which have the default types &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;One&lt;/code&gt;, respectively. We
use these two types to match the formal definition of the Fibonacci sequence
which states that the first two numbers in the sequence should be 0 and 1.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&lt;/code&gt; type is similar to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Add&lt;/code&gt; type in that it returns the result
stored in our accumulator &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;acc&quot;&lt;/code&gt; once a type is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt; (in this case, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;).
Until we reach our base case, we keep recursing by indexing the property &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;n&quot;&lt;/code&gt;.
For every step, we decrement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;, replace &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F0&lt;/code&gt; with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F1&lt;/code&gt;, and calculate a new
type for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F1&lt;/code&gt; by adding &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F1&lt;/code&gt; together. Eventually, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; will become
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Zero&lt;/code&gt;, and we have calculated our nth Fibonacci number.&lt;/p&gt;

&lt;p&gt;Now we can finally put our newly defined &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&lt;/code&gt; type to use and check that
it works:&lt;/p&gt;

&lt;div class=&quot;language-typescript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Zero&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;zero&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;One&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Two&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;one&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Three&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;two&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Four&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;three&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Five&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;five&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Six&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;eight&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;fib7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Fibonacci&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Seven&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;thirteen&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If you are testing this out yourself, you will see that the code only type
checks if the value assigned to the variable matches to corresponding Fibonacci
number. So if we change the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fib4&lt;/code&gt; to, say, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eight&lt;/code&gt;, our code will no
longer type check.&lt;/p&gt;

&lt;p&gt;Two things are worth pointing out here, though: a) TypeScript gives us an
error stating that the “type instantiation is excessively deep and possibly
infinite” once we try to compute any Fibonacci number larger than eight, and, b)
any number from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&amp;lt;Five&amp;gt;&lt;/code&gt; and above will type check as long it is above
five. Admittedly, it is unclear to me whether this is a limitation of the
TypeScript type checker or an issue with my implementation, but it clearly shows
that while it is possible to implement this, issues will likely occur sooner or
later.&lt;/p&gt;

&lt;p&gt;I hope you have found this blog post insightful. If you are curious and want to
play around with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fibonacci&lt;/code&gt; type, you can find the code in a TypeScript
playground &lt;a href=&quot;https://www.typescriptlang.org/play?#code/C4TwDgpgBAWhBOB7KBeKAiAXgx6BQeokUAygK4DGFAPAHIB8qUA3lAHYBcUtUAvgUWgB5NtDTkq1OEnqFw0ACoB3ZOMo0REWYKgKAFvAhjS66ssTb5UAGKIy8JhJr7DWucWsBLAG7Gn1W3tLYhJPAA9HUy9fYOgSCF82SMlQsNioAFFPAHM9YGSaeMT02k9RAuos3OB0hQgktUlS0Vk8CkQ2AGd87CQuaVUMXtw2ju6oDoguTSZWTihhvlGu-OAVLnNZ9i5WADp9yb4l9pWoYAMjDYvjOZ2ofd215F5jsfyAMzt4LkCHNFuWPd9udXEd+Cdxu8fFMbNCtvM9vtPvYwctxp1wlxUvC7g8ob5URD8p0EvUsaSGoCEUDdhiIi9wW8oBAcnkuFU8jjAQ8SYlCUy2GUYc0bttufsWdV+adgGTdPUuYjdoLygy0fkIAAbClY0x1NiMf5ipWypJqolnJRa3y6lJ6+r0Q1U3ES7V881M86eeCyuX+f32g2OxU0tbW6Bq9zQAAiEAohgAtvVgHQnTwIGFTQATTomSRld4IKAAJUYAH4S1B+jgBFYAJKdAappjpzP1HOwHBQCvAeBkaBcd4AQ01JNrxDr7wyo4g1AAwsy22wOwAjRCIbVDtgAGl0u+sToXGezud7-e7uirNnH0AyAEcyCPOtQAIK7gBChrwUCgL8XJ7zGgCyLEgX1kH8K3ff921zfxgIcEhP2-H8L3vR9R2oUDd0Q8CUMHJ8IGQrg-2PGDOyQZCIKgKDSOXXMBkolCez7QiULwqBhxnRj8K4qNfyzLNXw-J1mGQocqC4d9kPmEilw7OC2ELBwAH1y34wTY3jCAkzYFMwOw0xP0YLhRF8eA8F4ABtSdpxJagGybfSMHEih0F3dA2HQegAF0b1hNc2Bczw6H3AAGJgBn3ABGJhNBEsSJJsULpK4VsAIUpSoFUi8vACoLqE0xNk1TaLdxfASAlC6LgxM0lzKsmyZ0qB8nykHBdwYdyXLcjBPJ8ggLShFdQp+Tw8qoYKBidYZ1Q4saotG8aKGCuKmEmWahoAJkWjp8vMJ11sGsaAGYdsCiazGuJ0ng2saABYzvy35ruuW6VwAVkei7oi0Jh8UIo6VwANi+5bMPCJ1JTyN6AHZQeCooHSYL0fSMNggA&quot;&gt;here&lt;/a&gt;
and a repository with more examples &lt;a href=&quot;https://github.com/majjoha/typology&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</description>
        <pubDate>Mon, 29 Mar 2021 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2021/03/29/type-level-programming-in-typescript/</link>
        <guid isPermaLink="true">https://mjj.io/2021/03/29/type-level-programming-in-typescript/</guid>
      </item>
    
      <item>
        <title>Building a Passphrase Generator in Haskell</title>
        <description>&lt;p&gt;During the recent holiday season, I decided to put my admittedly limited Haskell
skills to use. While the &lt;a href=&quot;https://www.seas.upenn.edu/~cis194/spring13/&quot;&gt;CIS 194&lt;/a&gt;
course exercises are excellent for learning and practicing Haskell, and I enjoy
working through them when time permits, I wanted to work on a more structured
“real world” project this time.&lt;/p&gt;

&lt;p&gt;I had a few goals in mind for this particular project. First and foremost, it
had to be a well-defined and relatively simple problem that I needed to solve.
Secondly, I wanted it to require me to handle IO and side effects, and, lastly,
I wanted it to offer me an opportunity to integrate third-party libraries in my
app.&lt;/p&gt;

&lt;p&gt;Only a couple of weeks prior to starting this project, I had been reading the
&lt;a href=&quot;https://www.eff.org/dice&quot;&gt;guide&lt;/a&gt; by the &lt;a href=&quot;https://www.eff.org&quot;&gt;Electronic Frontier
Foundation&lt;/a&gt; on how to generate strong six-word
&lt;a href=&quot;https://en.m.wikipedia.org/wiki/Diceware&quot;&gt;Diceware&lt;/a&gt; passphrases. Turning their
suggested method into a small command-line utility seemed to suit my three goals
nicely, so I decided to give it the old college try. In this blog post, I will
elaborate on how this method of generating passphrases works and how it can be
implemented in Haskell. The entire project can be found on GitHub
&lt;a href=&quot;https://github.com/majjoha/passphrase&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The steps needed to generate a passphrase using the directions provided by EFF
are fairly simple, and you only need five dice and a
&lt;a href=&quot;https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt&quot;&gt;wordlist&lt;/a&gt;:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Roll five dice at the same time and write down the five numbers. The numbers
might be something like 4, 3, 4, 6, 3.&lt;/li&gt;
  &lt;li&gt;In your wordlist, find the word that matches the number 43463 (in this case,
panoramic), and write it down.&lt;/li&gt;
  &lt;li&gt;Repeat these two steps until you have (at least) six words. Once you have
repeated the steps, you might have a passphrase such as “whinny daffodil
aerobics upheld bankable niece.”&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before detailing the implementation, it should be noted that the project uses
&lt;a href=&quot;https://github.com/commercialhaskell/rio&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rio&lt;/code&gt;&lt;/a&gt; as a prelude replacement by
setting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{-# LANGUAGE NoImplicitPrelude #-}&lt;/code&gt; and importing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RIO&lt;/code&gt; in all files.
With our project description in place, we are now ready to implement it.&lt;/p&gt;

&lt;p&gt;In order to solve the first step, we need to be able to roll five dice and join
the numbers into a single digit. To do so, we define a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rollDice&lt;/code&gt; function:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rollDice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rollDice&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rolls&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replicateM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rolls&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;generateBetween&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;start&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rollDice&lt;/code&gt; takes three arguments: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rolls&lt;/code&gt; which describes how many dice we want
to roll, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end&lt;/code&gt; which respectively describe the start and end of
the range of numbers, we will be working with. Here, we rely on
&lt;a href=&quot;https://hackage.haskell.org/package/cryptonite-0.27/docs/Crypto-Number-Generate.html#v:generateBetween&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;generateBetween&lt;/code&gt;&lt;/a&gt;
from the &lt;a href=&quot;https://hackage.haskell.org/package/cryptonite&quot;&gt;Cryptonite&lt;/a&gt; library to
generate a random number within our range. Since our result is wrapped in a
monad, we need to use
&lt;a href=&quot;https://hackage.haskell.org/package/base-4.14.1.0/docs/Control-Monad.html#v:replicateM&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicateM&lt;/code&gt;&lt;/a&gt;
to repeat this process N times.&lt;/p&gt;

&lt;p&gt;Next, we need to be able to join these digits into one. To do this, we will
define a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joinDigits&lt;/code&gt; in the following manner:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;joinDigits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;joinDigits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;foldl&apos;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This function takes a list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Integer&lt;/code&gt;s and uses
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-List.html#v:foldl-39-&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L.foldl&apos;&lt;/code&gt;&lt;/a&gt;
to combine them into one.&lt;/p&gt;

&lt;p&gt;For every element in the list, we multiply our accumulator with 10 and then add
the first element in our list to the result. This process is repeated until our
list is empty. For instance, given the numbers 5, 2, 3, 1, 6, and 0 as our
initial accumulator value, this is how our result is calculated:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0                  # Initial accumulator value
0*10+5    = 5      # The list is now [2, 3, 1, 6]
5*10+2    = 52     # The list is now [3, 1, 6]
52*10+3   = 523    # The list is now [1, 6]
523*10+1  = 5231   # The list is now [6]
5231*10+6 = 52316  # The list is now []
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;With these two functions defined, we now have the necessary parts for generating
the indices which we will need later when pairing dice rolls with words. To pair
indices with words, we will need a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toTuple&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;toTuple&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;toTuple&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;toTuple&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fromMaybe&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fromMaybe&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;index&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readMaybe&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Maybe&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;word&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headMaybe&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;word&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toTuple&lt;/code&gt; takes a list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;s and returns a tuple of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(Integer, String)&lt;/code&gt;
where the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Integer&lt;/code&gt; represents an index, and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; represents the
corresponding word from the wordlist. We will call this function later when we
map over each line in our wordlist.&lt;/p&gt;

&lt;p&gt;Since a line in our wordlist follows the format “&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11111 abacus&lt;/code&gt;”, we want to
read the first string as an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Integer&lt;/code&gt; and take the first word of the remaining
words in our line. As
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Partial.html#v:read&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;read&lt;/code&gt;&lt;/a&gt;
and
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-List-Partial.html#v:head&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;head&lt;/code&gt;&lt;/a&gt;
can throw exceptions, we use the safe alternatives
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Prelude.html#v:readMaybe&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readMaybe&lt;/code&gt;&lt;/a&gt;
and
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-List.html#v:headMaybe&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L.headMaybe&lt;/code&gt;&lt;/a&gt;
instead. To unwrap our
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Prelude-Types.html#t:Maybe&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Maybe&lt;/code&gt;&lt;/a&gt;
values, we call
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Prelude.html#v:fromMaybe&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fromMaybe&lt;/code&gt;&lt;/a&gt;
and use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;&lt;/code&gt; as the default values for the index and word, respectively.
Lastly, to avoid non-exhaustive patterns, we pattern match on the empty list and
return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(0, &quot;&quot;)&lt;/code&gt; for good measure.&lt;/p&gt;

&lt;p&gt;Now we can finally move on to writing the logic for generating passphrases which
is handled by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;passphrase&lt;/code&gt; function:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;passphrase&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;String&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;passphrase&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordlist&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;unwords&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flip&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;findWithDefault&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordMap&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;indices&lt;/span&gt;
    &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;indices&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;joinDigits&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;wordMap&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fromList&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toTuple&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lines&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordlist&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To generate a passphrase, we need to pass our wordlist as the first argument and
then a list containing a list of our dice rolls as the second argument. The
first argument will look like this:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;11111 abacus
11112 abdomen
11113 abdominal
11114 abide
11115 abiding
11116 ability
11121 ablaze
11122 able
11123 abnormal
11124 abrasion
11125 abrasive
11126 abreast
11131 abridge
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;While the second argument, i.e., our dice rolls, might look like this:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;For all the lists in our list of dice rolls, we use our previously defined
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joinDigits&lt;/code&gt; function to join the numbers in the list into single digits. We
then store these digits, or indices, in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;indices&lt;/code&gt; variable, so we can use
them later when we need to look up words in our wordlist.&lt;/p&gt;

&lt;p&gt;As shown above, the wordlist follows the format &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;INDEX WORD&lt;/code&gt;, so we split our
wordlist on line breaks using 
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Prelude.html#v:lines&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lines&lt;/code&gt;&lt;/a&gt;
which gives us a list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;s containing elements such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;11111 abacus&quot;&lt;/code&gt;
and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;11112 abdomen&quot;&lt;/code&gt;. We want to further break these elements down so we will
end up with a list of tuples. To do so, we map over all the lines in our
wordlist and call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(toTuple . words)&lt;/code&gt; on them. Now that we have a list of
tuples, we can convert it to a
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Map.html#t:Map&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map&lt;/code&gt;&lt;/a&gt;
using
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Map.html#v:fromList&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map.fromList&lt;/code&gt;&lt;/a&gt;
so we can perform efficient lookups later.&lt;/p&gt;

&lt;p&gt;For all our indices, we want to find the corresponding word in our wordlist
which we do with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map (flip (Map.findWithDefault &quot;&quot;) wordMap) indices&lt;/code&gt;. Since
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Map.html#v:findWithDefault&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map.findWithDefault&lt;/code&gt;&lt;/a&gt;
expects the index of the map to be the second argument and the map to be the
last, we use two useful techniques here to make our code less convoluted. First,
we leverage currying by applying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;&lt;/code&gt; as the first argument to
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Map.html#v:findWithDefault&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Map.findWithDefault&lt;/code&gt;&lt;/a&gt;,
and then we use
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Prelude.html#v:flip&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;flip&lt;/code&gt;&lt;/a&gt;
to reverse the order of the last two arguments.&lt;/p&gt;

&lt;p&gt;By flipping the order, we can now do a simple map over our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;indices&lt;/code&gt; variable
and perform a lookup in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordMap&lt;/code&gt; in a slightly more elegant fashion. Once
we have all our words, we can join them into a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; using
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-List.html#v:unwords&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unwords&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With the fundamental logic now defined, we only need to read our wordlist from a
file, throw five dice six times, and pass these two values to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;passphrase&lt;/code&gt;
function. We do so by defining the entry point of our app in
&lt;a href=&quot;https://github.com/majjoha/passphrase/blob/main/app/Main.hs&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Main.hs&lt;/code&gt;&lt;/a&gt; as
such:&lt;/p&gt;

&lt;div class=&quot;language-haskell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;wordlist&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;$&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readFileUtf8&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;data/eff-large-wordlist.txt&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replicateM&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rollDice&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;runSimpleApp&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;logInfo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;display&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;passphrase&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wordlist&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dice&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This largely resembles the steps listed earlier. To retrieve our wordlist, we
call
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO.html#v:readFileUtf8&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readFileUtf8&lt;/code&gt;&lt;/a&gt;
with the path to our wordlist, and then
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Text.html#v:unpack&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T.unpack&lt;/code&gt;&lt;/a&gt;
to convert the
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Text.html#t:Text&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt;&lt;/a&gt;
value into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we roll our five dice six times by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;replicateM 6 $ rollDice 5 1
6&lt;/code&gt;, and pass our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wordlist&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dice&lt;/code&gt; to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;passphrase&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;After calling our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;passphrase&lt;/code&gt; function, we now have a passphrase that we need
to output in our terminal. Following the conventional structure for how to build
apps using &lt;a href=&quot;https://github.com/commercialhaskell/rio&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rio&lt;/code&gt;&lt;/a&gt;, we run our app as
a
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.15.0/docs/RIO-Prelude-Simple.html#t:SimpleApp&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SimpleApp&lt;/code&gt;&lt;/a&gt;
which provides a convenient default configuration. We convert the result from
our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;passphrase&lt;/code&gt; function into a
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.19.0/docs/RIO-Text.html#t:Text&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Text&lt;/code&gt;&lt;/a&gt;
value, then a
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.15.0/docs/RIO.html#t:Utf8Builder&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Utf8Builder&lt;/code&gt;&lt;/a&gt;,
and, lastly, print it in our terminal with
&lt;a href=&quot;https://hackage.haskell.org/package/rio-0.1.15.0/docs/RIO.html#v:logInfo&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;logInfo&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With everything in place, we have now reached our goal and can finally build our
project by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stack build&lt;/code&gt; and generate a passphrase:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;% stack exec -- passphrase
polar geek nimble okay appealing trim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In my experience, the Haskell community is great at sharing their knowledge on
both Twitter and in various blog posts. With this blog post, I hope to
contribute back to the community and help especially the people newer to the
language who might want to move beyond contrived exercises and learn how to
build smaller apps. If you are interested, you can find the project on GitHub
&lt;a href=&quot;https://github.com/majjoha/passphrase&quot;&gt;here&lt;/a&gt;, and if you have any feedback, I
am all ears.&lt;/p&gt;
</description>
        <pubDate>Tue, 26 Jan 2021 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2021/01/26/building-a-passphrase-generator-in-haskell/</link>
        <guid isPermaLink="true">https://mjj.io/2021/01/26/building-a-passphrase-generator-in-haskell/</guid>
      </item>
    
      <item>
        <title>Common Pitfalls in Ruby</title>
        <description>&lt;p&gt;Last week, I stumbled upon a document covering a list of common pitfalls in Ruby
that I assembled in the beginning of 2015. Browsing through the various gotchas,
I decided to rework the list, and publish it here for future reference.
Hopefully, others will find it useful too.&lt;/p&gt;

&lt;h2 id=&quot;and-or-and-not&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;and&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;or&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Since Ruby is often considered a readable language, many novices tend to believe
that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;and&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;or&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not&lt;/code&gt; are more appealing alternatives to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt;, and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;, respectively. They differ, however, in their behavior. Consider the
following example:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;foo&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Similarly, this example below reveals the issue with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not&lt;/code&gt; versus &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the first example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo = true and false&lt;/code&gt; will be interpreted as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(foo =
true) and false&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo = true &amp;amp;&amp;amp; false&lt;/code&gt; will be interpreted as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo = (true
&amp;amp;&amp;amp; false)&lt;/code&gt;, as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;and&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;or&lt;/code&gt; have lower precedence than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt;.  Thus,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;and&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;or&lt;/code&gt; are meant for flow control, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;||&lt;/code&gt; are for boolean
logic.&lt;/p&gt;

&lt;p&gt;Correspondingly, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; also behave differently due to their precedence.
As expected, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not&lt;/code&gt; has lower precedence than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As shown in the second example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not 3 == 4&lt;/code&gt; will be interpreted as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;not (3 ==
4)&lt;/code&gt; which evaluates to true, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!3 == 4&lt;/code&gt; will be interpreted as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(!3) == 4&lt;/code&gt;,
i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false == 4&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;equal-eql--and-&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equal?&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eql?&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;===&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;==&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;These four methods of determining equality all behave widely different. The
first method, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equal?&lt;/code&gt;, is an identity comparison, so it will only return true
in situations where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; is the same object as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;. One can think of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equal?&lt;/code&gt; as
a pointer comparison.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;equal?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;equal?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;It is worth nothing that if we enable immutable strings in the example above
with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;# frozen_string_literal: true&lt;/code&gt;, or the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--enable-frozen-string-literal &lt;/code&gt;
flag, the last example will also evaluate to true.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eql?&lt;/code&gt; is essentially for hash comparisons. It returns true when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;
refer to the same hash key. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash&lt;/code&gt; uses this to test for equality. It is common
to alias &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;eql?&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;==&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;===&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;==&lt;/code&gt; are for case equality, and generic equality,
respectively. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;===&lt;/code&gt; is typically overridden to provide meaningful semantics in
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;case&lt;/code&gt; statements for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Range&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Regex&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;==&lt;/code&gt; is the most common
comparison operator, and therefore this is usually overridden to provide
class-specific meaning.&lt;/p&gt;

&lt;h2 id=&quot;to_s-to_str-and-string&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_s&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_str&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_s&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; are more or less equivalent. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; will check the class
of its parameter, and if it is not already a string, it will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_s&lt;/code&gt; on it.
Calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_s&lt;/code&gt; obviously means it will be called regardless.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_str&lt;/code&gt; is different from the two, however. It should only be implemented in
situations where your object acts like a string as opposed to being
representable by a string meaning you should only implement &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_str&lt;/code&gt; in your
classes for objects that are interchangeable with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt; objects.&lt;/p&gt;

&lt;h2 id=&quot;any&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any?&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enumerable&lt;/code&gt; module in Ruby defines an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any?&lt;/code&gt; method. When I initially
learned Ruby, I expected that it would return true if the collection was
non-empty (as a negated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;empty?&lt;/code&gt;). Nevertheless, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;any?&lt;/code&gt; (without a provided
block) returns true if at least one of the collection members is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; or
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nil&lt;/code&gt;. The following example demonstrates this behavior:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;any?&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;any?&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:truthy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;any?&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;super-and-super&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super()&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;In Ruby, we learn that we can omit parentheses in method calls without any
arguments, as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo()&lt;/code&gt; returns the same result, and abandoning
unnecessary parentheses is normally what most style guides advocate for.&lt;/p&gt;

&lt;p&gt;Consequently, it might be rather tempting to leave out parentheses when calling
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super()&lt;/code&gt; but calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super()&lt;/code&gt; is not entirely the same in Ruby.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;super&lt;/code&gt; (without parentheses) will call the parent method with exactly the same
arguments that were passed to the original method, while the latter will call
the parent method without any arguments at all.&lt;/p&gt;

&lt;h2 id=&quot;size-count-and-length&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;size&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Similar to other methods in this blog post, people may be tempted to think
that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;size&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length&lt;/code&gt; are simply aliases for the same operation
but this is yet another quirk of Ruby.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;size&lt;/code&gt; are identical, and they usually run in constant time, so
they are faster than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;. Unlike &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;, they are not a part of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enumerable&lt;/code&gt; but rather a part of a concrete class (such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;, or
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;String&lt;/code&gt;). Normally, I tend to use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;length&lt;/code&gt; for strings, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;size&lt;/code&gt; for
collections.&lt;/p&gt;

&lt;p&gt;As mentioned, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt; is a part of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enumerable&lt;/code&gt;, and it is usually meant to be
used with a block, although this is not mandatory.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:even?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;hashnew-vs-hashnew-h-k-hk---&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new([])&lt;/code&gt; vs. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new {|h, k| h[k] = [] }&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new([])&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new {|h,k| h[k] = [] }&lt;/code&gt; may look similar but they
behave slightly different. When accessing an unknown element, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new([])&lt;/code&gt;
will always return the same array where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new {|h, k| h[k] = [] }&lt;/code&gt; creates a
new array. A quick benchmark reveals that accessing an unknown element from a
hash initialized with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new([])&lt;/code&gt; is approximately twice as fast as
accessing an unknown element from a hash initialized with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Hash.new {|h,k| h[k]
= [] }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This behavior can also be seen in arrays where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.new(42) { Foo.new }&lt;/code&gt; will
initialize a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo&lt;/code&gt; every time, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array.new(42, Foo.new)&lt;/code&gt; will refer to
the same &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Foo&lt;/code&gt; object for each element.&lt;/p&gt;

&lt;h2 id=&quot;the-flip-flop-operator-&quot;&gt;The flip-flop operator (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;..&lt;/code&gt;)&lt;/h2&gt;

&lt;p&gt;In Ruby, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;..&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;...&lt;/code&gt; are most often used for ranges. It allows us to
succinctly express ranges from A to Z as such &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;a&apos;..&apos;z&apos;&lt;/code&gt;. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;..&lt;/code&gt; operator
always includes the last element where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;...&lt;/code&gt; will skip the last element in the
range.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;z&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;size&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;26&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;a&apos;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;z&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;size&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;25&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can also conveniently express a date range as such:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;date&apos;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;DateTime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#&amp;lt;DateTime: 2016-12-15T22:36:38+01:00 ((2457738j,77798s,446146000n), +3600s, 2299161j)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;last_month&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;now&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#&amp;lt;DateTime: 2016-11-15T22:36:38+01:00 ((2457708j,77798s,446146000n),+3600s,2299161j)&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_month&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;size&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;..&lt;/code&gt; operator can, however, lead to a bit of confusion since it has a different
behavior in other situations.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The condition in the loop above evaluates to false every time it is evaluated
until the first part, i.e. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x == 5&lt;/code&gt;, evaluates to true. Then it evaluates to
true until the second part evaluates to true. In the above example, the
flip-flop is turned on when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x == 5&lt;/code&gt; and stays on until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x == 10&lt;/code&gt;, so the
numbers from 5 to 10 are printed.&lt;/p&gt;

&lt;p&gt;The flip-flop operator only works inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;s and ternary conditions.
Everywhere else, Ruby considers it to be the range operator. With the flip-flop
operator, we now conclude our whirlwind tour of the various pitfalls in the Ruby
programming language.&lt;/p&gt;

&lt;p&gt;As demonstrated in this blog post, Ruby has quite a few quirks. In order to
avoid many of these pitfalls, I usually advocate for using
&lt;a href=&quot;https://github.com/bbatsov/rubocop&quot;&gt;Rubocop&lt;/a&gt; either locally on your own
machine, or ideally as a part of the CI pipeline. While it may not detect all
the issues at hand, it is at most times extremely good at reporting problems in
your code.&lt;/p&gt;
</description>
        <pubDate>Mon, 02 Jan 2017 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2017/01/02/common-pitfalls-in-ruby/</link>
        <guid isPermaLink="true">https://mjj.io/2017/01/02/common-pitfalls-in-ruby/</guid>
      </item>
    
      <item>
        <title>Curry On: A Retrospective</title>
        <description>&lt;p&gt;This year’s &lt;a href=&quot;http://curry-on.org/2016/&quot;&gt;Curry On&lt;/a&gt; conference in Rome ended last
night, and it was certainly worth the trip. While I saw a fair amount of
presentations throughout the conference, I’d like to briefly touch upon a few of
my favorite ones in this blog post.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://curry-on.org/2016/sessions/the-racket-manifesto.html&quot;&gt;The Racket
Manifesto&lt;/a&gt; by
&lt;a href=&quot;http://www.ccs.neu.edu/home/matthias/&quot;&gt;Matthias Felleisen&lt;/a&gt; was a good reminder
that I need to spend more time with &lt;a href=&quot;http://racket-lang.org/&quot;&gt;Racket&lt;/a&gt;. It is a
deeply flexible language, and it is clear that countless hours have been spent
refining it. Both &lt;a href=&quot;http://www0.cs.ucl.ac.uk/staff/p.ohearn/&quot;&gt;Peter O’Hearn’s&lt;/a&gt;
talk &lt;a href=&quot;http://curry-on.org/2016/sessions/move-fast-to-fix-more-things.html&quot;&gt;Move Fast to Fix More
Things&lt;/a&gt;,
and &lt;a href=&quot;https://twitter.com/littlecalculist&quot;&gt;Dave Herman&lt;/a&gt;’s
&lt;a href=&quot;http://curry-on.org/2016/sessions/building-an-open-source-research-lab.html&quot;&gt;talk&lt;/a&gt;
thoroughly examined their transitions from academia to industry, and how
Facebook, and Mozilla have benefited from research with
&lt;a href=&quot;http://fbinfer.com/&quot;&gt;Infer&lt;/a&gt;, and &lt;a href=&quot;https://www.rust-lang.org/en-US/&quot;&gt;Rust&lt;/a&gt;,
respectively. It is projects like these that demonstrate how cooperation between
the industry, and academia can truly lead to advancements in our field.&lt;/p&gt;

&lt;p&gt;I also found &lt;a href=&quot;https://twitter.com/cristalopes&quot;&gt;Crista Lopes&lt;/a&gt;’s presentation on
&lt;a href=&quot;http://curry-on.org/2016/sessions/exercises-in-programming-style.html&quot;&gt;Exercises in Programming
Style&lt;/a&gt;
extremely entertaining. In the presentation, she essentially went through a
large range of different programming styles from imperative to
continuation-passing style, and it seemed somewhat reminiscent of &lt;a href=&quot;https://www.willamette.edu/~fruehr/haskell/evolution.html&quot;&gt;The Evolution
of a Haskell
Programmer&lt;/a&gt; by Fritz
Ruehr from Willamette University which was also a joy to read.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://curry-on.org/2016/sessions/rascal-the-swiss-army-knife-of-metaprogramming.html&quot;&gt;Rascal: the Swiss Army Knife of Meta
Programming&lt;/a&gt;
by &lt;a href=&quot;https://twitter.com/tvdstorm&quot;&gt;Tijs van der Storm&lt;/a&gt; was an interesting
presentation too. Although, I found the syntax to be a bit peculiar, it was
obvious that &lt;a href=&quot;http://www.rascal-mpl.org/&quot;&gt;Rascal&lt;/a&gt; truly enables non-trivial
metaprogramming tasks to be solved almost effortlessly. The source-to-source
Java transformation in the presentation impressed me, and certainly made me
interested in the language. Finally, &lt;a href=&quot;https://labs.oracle.com/pls/apex/f?p=labs:bio:0:21&quot;&gt;Cristina
Cifuentes&lt;/a&gt;’s presentation
&lt;a href=&quot;http://curry-on.org/2016/sessions/are-we-ready-for-secure-languages.html&quot;&gt;Are We Ready for Secure
Languages?&lt;/a&gt;
greatly illustrated how we should start thinking security into our languages
rather than adding security measures as an afterthought.&lt;/p&gt;

&lt;p&gt;In addition to the numerous marvelous talks, I also found the conference to be
extremely sympathetic. It was quite clear that the organizers had put an effort
into encouraging diversity which I greatly appreciated, and I think the
conference succeeded tremendously in enabling a conversation between academia,
and the industry. Finally, it did not hurt that this was the venue:&lt;/p&gt;

&lt;div&gt;
  &lt;img src=&quot;/images/pontificia-universitas-gregoriana.jpg&quot; class=&quot;img-with-shadow&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Next year, Curry On will be held in Barcelona, and I hope I’ll be able to attend
again.&lt;/p&gt;
</description>
        <pubDate>Wed, 20 Jul 2016 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2016/07/20/curry-on-a-retrospective/</link>
        <guid isPermaLink="true">https://mjj.io/2016/07/20/curry-on-a-retrospective/</guid>
      </item>
    
      <item>
        <title>2015 in Review</title>
        <description>&lt;p&gt;This post is simply a reminder to myself of all the great things that have
happened during 2015.&lt;/p&gt;

&lt;h2 id=&quot;highlights&quot;&gt;Highlights&lt;/h2&gt;
&lt;ul&gt;
  &lt;li&gt;In many ways, 2015 was a year of JavaScript to me. I visited my first
conference, &lt;a href=&quot;https://www.react-europe.org&quot;&gt;ReactEurope&lt;/a&gt;, and went to a React
hackathon at the Mozilla office in Paris. In addition, I spent countless hours
learning &lt;a href=&quot;http://flowtype.org/&quot;&gt;Flow&lt;/a&gt;, and implementing it into our existing
code base at work. I also familiarized myself with the wonders of
&lt;a href=&quot;https://babeljs.io/&quot;&gt;Babel&lt;/a&gt;, and ECMAScript 2015, and I learned to apply, and
appreciate the ideas behind &lt;a href=&quot;https://brandur.org/elegant-apis&quot;&gt;JSON schemas&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;I submitted, and successfully defended my &lt;a href=&quot;https://www.dropbox.com/s/g138w9klyneuajy/Mads-Mathias-Bachelor-Thesis.pdf?dl=0&quot;&gt;bachelor’s
thesis&lt;/a&gt;.
This is possibly the most challenging work I’ve accomplished so far. It is an
implementation of a lazy, functional programming language which can be
executed on two, separate stack machines that perform lazy evaluation using
two different strategies. It consists of a compiler written in OCaml, and the
aforementioned stack machines written in C. The source code is available on
&lt;a href=&quot;https://github.com/majjoha/sloth&quot;&gt;GitHub&lt;/a&gt;, although sufficient documentation
is still severely lacking.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;books-read&quot;&gt;Books read&lt;/h2&gt;
&lt;p&gt;Compared to 2014, I’ve read far fewer books in 2015 than I wanted to both in
terms of fiction, and technical books. In 2016, I hope to delve into Tropic of
Capricorn, Flaubert, and ideally also SICP which has been on my to-read list
since the beginning of 2014. These are the books I completed in 2015 in
chronological order:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/272642.Rip_it_Up_and_Start_Again&quot;&gt;Simon Reynolds: Rip it Up and Start Again&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/38501.Ham_on_Rye&quot;&gt;Charles Bukowski: Ham on Rye&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/3652866-the-implementation-of-functional-programming-languages&quot;&gt;Simon L. Peyton Jones: The Implementation Of Functional Programming
Languages&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/486623.The_New_York_Trilogy&quot;&gt;Paul Auster: The New York Trilogy&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/760.Memories_of_My_Melancholy_Whores&quot;&gt;Gabriel García Márquez: Memories of My Melancholy Whores&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/4517763-junky&quot;&gt;William S. Burroughs: Junky&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.goodreads.com/book/show/9282423-learn-you-a-haskell-for-great-good&quot;&gt;Miran Lipovača: Learn You a Haskell for Great Good!&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;favorite-records-of-the-year&quot;&gt;Favorite records of the year&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://www.last.fm/user/bouleau/journal&quot;&gt;I’ve listed&lt;/a&gt; my favorite records of
the year for quite some years now. This year is no different.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/have-you-in-my-wilderness-mw0002860296&quot;&gt;Julia Holter: Have You In My
Wilderness&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/ii-mw0002869328&quot;&gt;Horsebeach: II&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/i-love-you-honeybear-mw0002777404&quot;&gt;Father John Misty: I Love You, Honeybear&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/simple-songs-mw0002835921&quot;&gt;Jim O’Rourke: Simple
Songs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/beyond-belief-mw0002884480&quot;&gt;Mark McGuire: Beyond Belief&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/depression-cherry-mw0002860587&quot;&gt;Beach House: Depression Cherry&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/dream-all-over-mw0002877974&quot;&gt;Gun Outfit: Dream All Over&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/summoning-suns-mw0002812289&quot;&gt;James Blackshaw: Summoning Suns&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/blues-the-dark-paintings-of-mark-rothko-mw0002807887&quot;&gt;Loren Connors: Blues: The Dark Paintings of “Mark Rothko”&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.allmusic.com/album/vertigo-mw0002877481&quot;&gt;The Necks: Vertigo&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I have a slight feeling that 2016 will include working with
&lt;a href=&quot;https://facebook.github.io/relay/&quot;&gt;Relay&lt;/a&gt;, and
&lt;a href=&quot;https://graphql.org&quot;&gt;GraphQL&lt;/a&gt;. If time permits it, it would be
interesting to dabble in &lt;a href=&quot;https://www.rust-lang.org/&quot;&gt;Rust&lt;/a&gt;,
&lt;a href=&quot;http://www.idris-lang.org/&quot;&gt;Idris&lt;/a&gt;, &lt;a href=&quot;http://racket-lang.org/&quot;&gt;Racket&lt;/a&gt;, and
&lt;a href=&quot;https://elixir-lang.org&quot;&gt;Elixir&lt;/a&gt;, or even notably older programming languages
as &lt;a href=&quot;https://en.wikipedia.org/wiki/Prolog&quot;&gt;Prolog&lt;/a&gt;, and
&lt;a href=&quot;https://en.wikipedia.org/wiki/Forth_(programming_language)&quot;&gt;Forth&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hopefully, I’ll have more time to practice music too, and perhaps travel a bit
as well. I’d also be fun to revisit my bachelor’s thesis, and refine the
language. I might give an update on the state of affairs next year. We’ll see.&lt;/p&gt;

&lt;p&gt;Here’s to 2016!&lt;/p&gt;
</description>
        <pubDate>Fri, 01 Jan 2016 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2016/01/01/2015-in-review/</link>
        <guid isPermaLink="true">https://mjj.io/2016/01/01/2015-in-review/</guid>
      </item>
    
      <item>
        <title>Ruby Extensions in C</title>
        <description>&lt;p&gt;Occasionally, we come across particular sections in our programs that need to be
exceptionally fast. Ruby allows us to write extensions in C, so that we can
delegate the heavy lifting. In this post, I’ll show you how easy it is to extend
Ruby with C by writing a trivial factorial function in C which we’ll be able to
call from Ruby.&lt;/p&gt;

&lt;p&gt;We start out by creating the relevant directories and files.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mkdir &lt;/span&gt;fact
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;fact
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mkdir &lt;/span&gt;ext
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;touch &lt;/span&gt;ext/extconf.rb ext/fact.c
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ext/extconf.rb&lt;/code&gt; file, we require the
&lt;a href=&quot;https://ruby-doc.org/stdlib-2.0.0/libdoc/mkmf/rdoc/MakeMakefile.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkmf&lt;/code&gt;&lt;/a&gt;
module which allows us to generate an applicable Makefile that compiles our C
code.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;mkmf&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;create_makefile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;fact&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Afterwards, we write the actual C program in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ext/fact.c&lt;/code&gt; file. It should
look like this:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cp&quot;&gt;#include&lt;/span&gt; &lt;span class=&quot;cpf&quot;&gt;&quot;ruby.h&quot;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;
&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Init_fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Init_fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Fact&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rb_define_module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Fact&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;rb_define_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fact&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;VALUE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;NUM2INT&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;factorial&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;factorial&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INT2NUM&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;factorial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;First, we include the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ruby.h&lt;/code&gt; header file, so that we can access the necessary
macros and functions. Ruby will by default execute our initializing function
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Init_fact&lt;/code&gt;, so we define our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fact&lt;/code&gt; module in here with the function
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rb_define_module&lt;/code&gt;. Additionally, we define a method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fact&lt;/code&gt; in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fact&lt;/code&gt;
module with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rb_define_method&lt;/code&gt; function. It takes a class, a method name, a
function and the number of arguments.&lt;/p&gt;

&lt;p&gt;In the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VALUE fact(VALUE self, VALUE n)&lt;/code&gt;, we pass both &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VALUE self&lt;/code&gt; and
the number we want to take the factorial of. We convert the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VALUE n&lt;/code&gt; to an
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;int&lt;/code&gt; with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;NUM2INT&lt;/code&gt; macro, and convert the integer back to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fixnum&lt;/code&gt; when
returning the result.&lt;/p&gt;

&lt;p&gt;We can then compile our program, and open &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;irb&lt;/code&gt; to verify that it works as
expected:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;ruby ext/extconf.rb
creating Makefile
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;make
compiling ext/fact.c
linking shared-object fact.bundle
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;irb
irb&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;main&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:001:0&amp;gt; require_relative &lt;span class=&quot;s2&quot;&gt;&quot;fact&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true
&lt;/span&gt;irb&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;main&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:002:0&amp;gt; include Fact
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; Object
irb&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;main&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;:003:0&amp;gt; fact&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;5&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; 120
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Utilizing C in our Ruby programs can be incredibly useful both in order to
achieve higher performance for specific parts of our application, but also if we
want interfacing with other C code. If you want to go further, I highly
recommend that you take a look at the
&lt;a href=&quot;http://docs.ruby-lang.org/en/2.2.0/README_EXT.html&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;README.EXT&lt;/code&gt;&lt;/a&gt; documentation.&lt;/p&gt;
</description>
        <pubDate>Sat, 07 Feb 2015 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2015/02/07/ruby-extensions-in-c/</link>
        <guid isPermaLink="true">https://mjj.io/2015/02/07/ruby-extensions-in-c/</guid>
      </item>
    
      <item>
        <title>Encrypting Files With GPG and Vim</title>
        <description>&lt;p&gt;I do most of my writing in Vim whether it is programming or editing ordinary
documents. The only two exceptions are journals and emails. For journals, I’ve
been using &lt;a href=&quot;http://dayoneapp.com&quot;&gt;Day One&lt;/a&gt; for quite an extensive amount of time
now, but I’ve been considering to replace it with Vim. In order to replace Day
One, I need a way to effortlessly encrypt and decrypt text files. In this post,
I’ll show you how I’ve set up an environment that enables me to do so.&lt;/p&gt;

&lt;p&gt;By default, Vim provides you with the ability to encrypt and decrypt files in a
quite simple manner. You open a file with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim -x my-top-secret-document.md&lt;/code&gt; or
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:X&lt;/code&gt; command, and then Vim prompts you for an encryption key. When you’ve
entered the encryption key twice, you are able to edit the document. If you try
to print the document with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt; afterwards, you’ll see gibberish like
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;VimCrypt~01!gd)�/�:�-(%)&lt;/code&gt;, but if you open the file with Vim, the editor will
prompt you for the key phrase, and after entering the correct encryption key you
can read and edit the file. There is a caveat to this approach, however.
According to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:X&lt;/code&gt; help page, Vim has not been tested for robustness, and we
do not want swap files, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;viminfo&lt;/code&gt; file or any other files for that matter to
expose our file contents, so instead we are going to rely on
&lt;a href=&quot;https://www.gnupg.org/&quot;&gt;GPG&lt;/a&gt; and
&lt;a href=&quot;http://learnvimscriptthehardway.stevelosh.com/chapters/12.html&quot;&gt;autocommands&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What we need to include in our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt; is the following autocommand group:&lt;/p&gt;

&lt;div class=&quot;language-viml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;augroup encrypted
  autocmd&lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;
  autocmd &lt;span class=&quot;nb&quot;&gt;BufReadPre&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;FileReadPre&lt;/span&gt; *&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;gpg &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;viminfo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;=&lt;/span&gt;
  autocmd &lt;span class=&quot;nb&quot;&gt;BufReadPre&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;FileReadPre&lt;/span&gt; *&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;gpg &lt;span class=&quot;k&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;noswapfile&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;noundofile&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nobackup&lt;/span&gt;
  autocmd &lt;span class=&quot;nb&quot;&gt;BufReadPost&lt;/span&gt; *&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;gpg &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;%&lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;gpg &lt;span class=&quot;p&quot;&gt;--&lt;/span&gt;decrypt &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;sr&quot;&gt;/dev/&lt;/span&gt;null
  autocmd &lt;span class=&quot;nb&quot;&gt;BufWritePre&lt;/span&gt; *&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;gpg &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;%&lt;span class=&quot;p&quot;&gt;!&lt;/span&gt;gpg &lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;ae &lt;span class=&quot;p&quot;&gt;--&lt;/span&gt;default&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;recipient&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;self
  autocmd &lt;span class=&quot;nb&quot;&gt;BufWritePost&lt;/span&gt; *&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;gpg &lt;span class=&quot;k&quot;&gt;u&lt;/span&gt;
augroup END
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Essentially, we disable auto-saving the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.viminfo&lt;/code&gt; file, and then we disable
swap files, undo files and backup files. After the buffer is read, we decrypt
it with GPG, so that we are able to read the content in Vim.  Before we
eventually save our file, we encrypt the entire file with the user ID of the
default key as the recipient of our message, and finally after writing the file
we undo the last action, so that the file is still readable to us.&lt;/p&gt;
</description>
        <pubDate>Tue, 27 Jan 2015 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/</link>
        <guid isPermaLink="true">https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/</guid>
      </item>
    
      <item>
        <title>Curry Your Procs</title>
        <description>&lt;p&gt;Recently, I discovered that Ruby provides a rather esoteric &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#curry&lt;/code&gt; method for
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt;s that I’d like to examine in this post.&lt;/p&gt;

&lt;p&gt;Currying basically means taking one function with multiple arguments and
converting it into a function that takes only one argument and returns another
function. The concept was originally coined by Moses Schönfinkel, and later
developed by Haskell Curry.&lt;/p&gt;

&lt;p&gt;In Ruby, we might have a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; taking multiple arguments:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; with all of its arguments by saying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f[1,2,3]&lt;/code&gt; which would
evaluate to 6 in our case.&lt;/p&gt;

&lt;p&gt;Currying &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; by hand would look like this:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;curried_f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;Proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;c&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can now evaluate our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Proc&lt;/code&gt; by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curried_f[1][2][3]&lt;/code&gt; which would
evaluate to 6 exactly as in our previous example.&lt;/p&gt;

&lt;p&gt;The ingenious reader have probably already guessed that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#curry&lt;/code&gt; will take our
original &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; and turn it into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curried_f&lt;/code&gt;. We can curry &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; in the following way
instead: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curried_f = f.curry&lt;/code&gt;, and then finally call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curried_f[1][2][3]&lt;/code&gt; which
will unsurprisingly return 6 as before.&lt;/p&gt;
</description>
        <pubDate>Tue, 04 Nov 2014 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2014/11/04/curry-your-procs/</link>
        <guid isPermaLink="true">https://mjj.io/2014/11/04/curry-your-procs/</guid>
      </item>
    
      <item>
        <title>What We Talk About When We Talk About Rack</title>
        <description>&lt;p&gt;I was encouraged by &lt;a href=&quot;https://twitter.com/jamiemhodge&quot;&gt;Jamie Hodge&lt;/a&gt; to give a
talk at this week’s &lt;a href=&quot;http://www.copenhagenrb.dk/&quot;&gt;Copenhagen Ruby Brigade&lt;/a&gt;
meetup about Rack, and in this post, I’d like to give a recap on the subject.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/rack/rack&quot;&gt;Rack&lt;/a&gt; is essentially a minimal web server
interface that &lt;a href=&quot;http://rubyonrails.org/&quot;&gt;Rails&lt;/a&gt;,
&lt;a href=&quot;http://www.sinatrarb.com/&quot;&gt;Sinatra&lt;/a&gt;, &lt;a href=&quot;http://lotusrb.org/&quot;&gt;Lotus&lt;/a&gt;,
&lt;a href=&quot;http://cuba.is/&quot;&gt;Cuba&lt;/a&gt;, &lt;a href=&quot;https://github.com/camping/camping&quot;&gt;Camping&lt;/a&gt; and
friends all heavily rely on. They do so in order not to interact directly with
the lower levels of the socket communication, and instead they distribute this
particular work for Rack, so they can focus on other parts of the architecture.
The main benefit of Rack is that you can write your applications once, and run
them everywhere.  Almost all Ruby servers support Rack, so you can easily power
up your application without having to tailor it to a specific platform.&lt;/p&gt;

&lt;p&gt;A Rack application is basically an object that responds to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#call&lt;/code&gt;, accepts
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt; as its only argument and returns an array containing the HTTP status code,
the headers and an object that responds to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#each&lt;/code&gt;. Using a stabby lambda, a
simple Rack application printing “Hello, world” to its users could look like
this:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;rack&apos;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Content-type&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;text/html&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Rack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;app: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;At some point, we probably want to extract our logic into its own class. This is
quite manageable to do, since we simply need an object that responds to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#call&lt;/code&gt;
and takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt; as its only argument. We could for instance end up with the
following:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;rack&apos;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SuperAdvancedWebApp&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Content-type&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;text/html&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Rack&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;app: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;SuperAdvancedWebApp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Except from being extracted into a class, the aforementioned application is
slightly different in that it actually returns the result of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt; to its
visitors, so that we can investigate the contents of the current environment
from the browser.&lt;/p&gt;

&lt;p&gt;For learning purposes, I’ve built a small web framework utilizing Rack that
discovers how we can effortlessly build a framework similar to Rails, Sinatra
and so forth which I’ve named Dolphy, and you can study the source code on
&lt;a href=&quot;https://github.com/majjoha/dolphy&quot;&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is possible to avoid the dependency of Rack by using TCP sockets as I
presented in my previous post, “&lt;a href=&quot;/2014/09/17/investigating-sockets/&quot;&gt;Investigating
sockets&lt;/a&gt;”. The incredibly small framework
&lt;a href=&quot;https://github.com/pachacamac/busker&quot;&gt;busker&lt;/a&gt; is an attempt at building a web
framework without the dependency of Rack. I keep a branch called
&lt;a href=&quot;https://github.com/majjoha/dolphy/tree/majjoha/rackless&quot;&gt;majjoha/rackless&lt;/a&gt; in
the Dolphy repository where I try to remove Rack from the dependencies of the
project in a similar manner.&lt;/p&gt;

&lt;p&gt;Furthermore, Rack itself is also in an exciting state at this moment, as &lt;a href=&quot;https://twitter.com/tenderlove&quot;&gt;Aaron
Patterson&lt;/a&gt; who is also a Rails core contributor
recently took over the development of the project. This is what he said about
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt; hash back in August:&lt;/p&gt;

&lt;blockquote class=&quot;twitter-tweet&quot; data-conversation=&quot;none&quot; lang=&quot;en&quot;&gt;&lt;p&gt;&lt;a href=&quot;https://twitter.com/jcoglan&quot;&gt;@jcoglan&lt;/a&gt; not just that, the mutable env hash passed everywhere is the bane of my existence.&lt;/p&gt;&amp;mdash; Aaron Patterson (@tenderlove) &lt;a href=&quot;https://twitter.com/tenderlove/status/502479098975764480&quot;&gt;August 21, 2014&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async=&quot;&quot; src=&quot;//platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;

&lt;p&gt;I am thrilled to see that we’ll finally get rid of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt; hash from our Rack
applications. In the repository,
&lt;a href=&quot;https://github.com/tenderlove/the_metal&quot;&gt;the_metal&lt;/a&gt;, he keeps a spike for
thoughts about Rack 2.0, and according to the examples in the project, it’ll
slightly change how we use Rack to build our web applications:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Application&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;req&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write_head&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;Content-Type&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;text/plain&apos;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;write&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello World&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;finish&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;the_metal/puma&apos;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TheMetal&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create_server&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;listen&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9292&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;0.0.0.0&apos;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I highly welcome the change, and I find this new way of interacting with the
request and response directly much more elegant than fiddling with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;env&lt;/code&gt;
hash as we’re used to. It is going to be interesting to see how these changes
will affect all the existing frameworks that we use today.&lt;/p&gt;
</description>
        <pubDate>Sat, 27 Sep 2014 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2014/09/27/what-we-talk-about-when-we-talk-about-rack/</link>
        <guid isPermaLink="true">https://mjj.io/2014/09/27/what-we-talk-about-when-we-talk-about-rack/</guid>
      </item>
    
      <item>
        <title>Investigating Sockets</title>
        <description>&lt;p&gt;During this summer, I’ve revisited an old side project of mine which is a
Ruby-based micro framework for web development. Like most web frameworks in
Ruby, this heavily depends on Rack which can be extremely helpful in a lot
of ways, but you do not learn all the lower levels of how the server handles
requests from clients and so forth that I find somewhat intriguing. This
ultimately led me to investigate the different sockets that the Ruby standard
library provides and in general improve my understanding on sockets. This post
covers some of my findings from the process.&lt;/p&gt;

&lt;p&gt;Sockets are the endpoints of bidirectional communication channels. The Ruby
standard library has six different socket classes: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UNIXSocket&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDPSocket&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TCPSocket&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Socket&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPSocket&lt;/code&gt;, and they all inherit from the sixth socket
class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BasicSocket&lt;/code&gt;. The class hierarchy for the sockets in Ruby looks like
this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/sockets-class-hierarchy.png&quot; alt=&quot;Class hierarchy&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Starting from the bottom, TCP is the most commonly used protocol on the Internet
as it offers error correction, and like UDP it belongs to the transport layer.
Furthermore, TCP sockets guarantee delivery which means that it will resend
packets if needed and stop the data flow until packets are successfully
transferred, so TCP sockets are considered very reliable.&lt;/p&gt;

&lt;p&gt;Implementing a small single-threaded web server that prints the current time to
the user could be done this way utilizing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TCPServer&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;socket&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;TCPServer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;localhost&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2345&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kp&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;accept&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;gets&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Time is &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Accessing &lt;a href=&quot;http://localhost:2345&quot;&gt;http://localhost:2345&lt;/a&gt; in the browser would
then return the current time to the user.  This is actually what I started out
with when I was replacing Rack with sockets, and from here you need to figure
out how to match URLs from the user, handle multiple users, send/receive POST
and GET parameters, send the necessary headers to the browser and so on. This
process reveals how much work Rack actually does for you, but I enjoy seeing how
much I can do without it as well.&lt;/p&gt;

&lt;p&gt;Moving on to UDP, this protocol, on the other hand, is considered unreliable,
but it offers speed why it is often used for streaming purposes. Also, UDP
packets are remarkably smaller than TCP packets. Unlike TCP, UDP does not
provide any error correction or flow control, so errors will be present.&lt;/p&gt;

&lt;p&gt;Since UDP does not do retransmissions, we might also lose content, or content
might be ordered the wrong way as UDP does not guarantee in-order delivery.
Thus, TCP is quite obviously the most appropriate protocol of the two to rely on
when implementing a web framework without Rack.&lt;/p&gt;

&lt;p&gt;Implementing a simple chat server and client using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDPSocket&lt;/code&gt; could be done the
following way:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# udp-server.rb&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;socket&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;UDPSocket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;localhost&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kp&quot;&gt;loop&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;recvfrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;From address: &apos;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&apos;,&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&apos;, message: &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# udp-client.rb&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;socket&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;UDPSocket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;gets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;chomp&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;127.0.0.1&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33333&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;close&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The server creates a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDPSocket&lt;/code&gt; and binds it to localhost on port 3333.
From here, it will simply run a loop listening for messages and print them.  The
client works in a similar fashion. It creates a new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDPSocket&lt;/code&gt;, runs a loop
that receives messages from the standard input which it then sends to our
server.&lt;/p&gt;

&lt;p&gt;Moving up the class hierarchy, we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UNIXSocket&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPSocket&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Socket&lt;/code&gt;
where the latter will not be elaborated. A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UNIXSocket&lt;/code&gt; basically represents a
UNIX domain stream client, and what characterizes a UNIX domain stream is that
it does not use any underlying network protocol for communication, so it has
less overhead. It solely exists inside a single computer, and processes
communicating with a UNIX domain stream needs to be on the same computer too.
For these reasons, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UNIXSocket&lt;/code&gt; is obviously not suited for a web framework,
but the &lt;a href=&quot;https://gist.github.com/ryanlecompte/1619490&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UnixSocketForker&lt;/code&gt;&lt;/a&gt; by
Ryan LeCompte is an interesting usage of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UNIXSocket&lt;/code&gt; in a Ruby context.
Finally, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPSocket&lt;/code&gt; which is also the superclass of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TCPSocket&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UDPSocket&lt;/code&gt; is not so exciting on its own and therefore a little difficult to
leverage, but one common usage for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPSocket&lt;/code&gt; is looking up the IP address
of the host which we could do in the following way:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;socket&apos;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;IPSocket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getaddress&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;gethostname&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ip&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; &quot;192.168.0.100&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Investigating sockets have been an interesting journey so far, and I’ll
certainly need to investigate them even more for this project in particular. I
hope you’ve found this whirlwind tour of the Ruby sockets useful, and feel free
to ping me if you’ve any questions.&lt;/p&gt;
</description>
        <pubDate>Wed, 17 Sep 2014 00:00:00 +0000</pubDate>
        <link>https://mjj.io/2014/09/17/investigating-sockets/</link>
        <guid isPermaLink="true">https://mjj.io/2014/09/17/investigating-sockets/</guid>
      </item>
    
  </channel>
</rss>
