<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pete Corey</title>
    <description>The latest articles on DEV Community by Pete Corey (@petecorey).</description>
    <link>https://dev.to/petecorey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F229084%2F3ec3f9fc-18df-4038-9587-e00c12f7c335.png</url>
      <title>DEV Community: Pete Corey</title>
      <link>https://dev.to/petecorey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/petecorey"/>
    <language>en</language>
    <item>
      <title>MongoDB Object Array Lookup Aggregation</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Wed, 29 Jan 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/mongodb-object-array-lookup-aggregation-1e3n</link>
      <guid>https://dev.to/petecorey/mongodb-object-array-lookup-aggregation-1e3n</guid>
      <description>&lt;p&gt;As part of an ongoing quest to speed up an application I’m working on, I found myself tasked with writing a fairly complicated &lt;a href="https://docs.mongodb.com/manual/aggregation/"&gt;MongoDB aggregation pipeline&lt;/a&gt;. I found no existing documentation on how to accomplish the task at hand, so I figured I should pay it forward and document my solution for future generations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Widgets and Icons
&lt;/h2&gt;

&lt;p&gt;Imagine we have two MongoDB collections. Our first collection holds information about &lt;code&gt;widgets&lt;/code&gt; in our system:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').insert({
    _id: 1,
    name: 'Name',
    info: [
        {
            iconId: 2,
            text: 'Text'
        }
    ]
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Every widget has a &lt;code&gt;name&lt;/code&gt; and a list of one or more &lt;code&gt;info&lt;/code&gt; objects. Each &lt;code&gt;info&lt;/code&gt; object has a &lt;code&gt;text&lt;/code&gt; field and an associated icon referenced by an &lt;code&gt;iconId&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;icons&lt;/code&gt; collection holds some basic information about each icon:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('icons').insert({
    _id: 2,
    name: 'Icon',
    uri: 'https://...'
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The goal is to write an aggregation that returns our widgets with the associated icon documents attached to each corresponding &lt;code&gt;info&lt;/code&gt; object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
    _id: 1,
    name: 'Name',
    info: [
        {
            iconId: 2,
            text: 'Text',
            icon: {
                _id: 2,
                name: 'Icon',
                uri: 'https://...'
            }
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Working Through the Pipeline
&lt;/h2&gt;

&lt;p&gt;The aggregation that accomplishes this goal operates in six stages. Let’s work through each stage one by one. We’ll start by &lt;a href="https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/"&gt;&lt;code&gt;$unwind&lt;/code&gt;&lt;/a&gt;ing our &lt;code&gt;info&lt;/code&gt; array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').aggregate([
    { $unwind: '$info' }
]);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This creates a new document for every widget/info pair:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
    _id: 1,
    name: 'Name',
    info: {
        iconId: 2,
        text: 'Text',
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we’ll &lt;a href="https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/"&gt;&lt;code&gt;$lookup&lt;/code&gt;&lt;/a&gt; the icon associated with the given &lt;code&gt;iconId&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').aggregate([
    ...
    {
        $lookup: {
            from: 'icons',
            localField: 'info.iconId',
            foreignField: '_id',
            as: 'info.icon'
        }
    }
]);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our resulting document will now have a list of icons in the &lt;code&gt;info.icon&lt;/code&gt; field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
    _id: 1,
    name: 'Name',
    info: {
        iconId: 2,
        text: 'Text',
        icon: [
            {
                _id: 2,
                name: 'Icon',
                uri: 'https://...'
            }
        ]
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is a step in the right direction, but we know that the &lt;code&gt;info&lt;/code&gt; to &lt;code&gt;icons&lt;/code&gt; relationship will always be a one to one relationship. We’ll always receive exactly one icon as a result of our &lt;code&gt;$lookup&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Armed with this knowledge, we know we can &lt;code&gt;$unwind&lt;/code&gt; on &lt;code&gt;info.icon&lt;/code&gt; and safely turn our &lt;code&gt;info.icon&lt;/code&gt; array into an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').aggregate([
    ...
    { $unwind: '$info.icon' }
]);



{
    _id: 1,
    name: 'Name',
    info: {
        iconId: 2,
        text: 'Text',
        icon: {
            _id: 2,
            name: 'Icon',
            uri: 'https://...'
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But now we need to roll our &lt;code&gt;info&lt;/code&gt; back up into an array. We can accomplish this by &lt;a href="https://docs.mongodb.com/manual/reference/operator/aggregation/group/"&gt;&lt;code&gt;$group&lt;/code&gt;&lt;/a&gt;ing our widgets together based on their &lt;code&gt;_id&lt;/code&gt;. However, we need to be careful to preserve the original document to avoid clobbering the entire widget:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').aggregate([
    ...
    {
        $group: {
            _id: '$_id',
            root: { $mergeObjects: '$$ROOT' },
            info: { $push: '$info' }
        }
    }
]);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our resulting document contains our &lt;code&gt;info&lt;/code&gt; array and the original, pre-&lt;code&gt;$group&lt;/code&gt; widget document in the &lt;code&gt;root&lt;/code&gt; field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
    root: {
        _id: 1,
        name: 'Name',
        info: {
            iconId: 2,
            text: 'Text',
            icon: {
                _id: 2,
                name: 'Icon',
                uri: 'https://...'
            }
        }
    },
    info: [
        {
            iconId: 2,
            text: 'Text',
            icon: {
                _id: 2,
                name: 'Icon',
                uri: 'https://...'
            }
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The next step in our pipeline is to replace our root document with the &lt;code&gt;root&lt;/code&gt; object merged with the actual root document. This will override the &lt;code&gt;info&lt;/code&gt; object in &lt;code&gt;root&lt;/code&gt; with our newly grouped together &lt;code&gt;info&lt;/code&gt; array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').aggregate([
    ...
    {
        $replaceRoot: {
            newRoot: {
                $mergeObjects: ['$root', '$$ROOT']
            }
        }
    }
]);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We’re getting close to our goal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
    _id: 1,
    name: 'Name',
    info: [
        {
            iconId: 2,
            text: 'Text',
            icon: {
                _id: 2,
                name: 'Icon',
                uri: 'https://...'
            }
        }
    ],
    root: {
        _id: 1,
        name: 'Name',
        info: {
            iconId: 2,
            text: 'Text',
            icon: {
                _id: 2,
                name: 'Icon',
                uri: 'https://...'
            }
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;An unfortunate side effect of this merger is that our resulting document still has a &lt;code&gt;root&lt;/code&gt; object filled with superfluous data. As a final piece of housecleaning, let’s remove that field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').aggregate([
    ...
    {
        $project: {
            root: 0
        }
    }
]);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And with that we’re left with our original goal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
    _id: 1,
    name: 'Name',
    info: [
        {
            iconId: 2,
            text: 'Text',
            icon: {
                _id: 2,
                name: 'Icon',
                uri: 'https://...'
            }
        }
    ]
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Success!&lt;/p&gt;

&lt;h2&gt;
  
  
  All Together
&lt;/h2&gt;

&lt;p&gt;For posterity, here’s the entire aggregation pipeline in its entirety:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
db.getCollection('widgets').aggregate([
    { $unwind: '$info' },
    {
        $lookup: {
            from: 'icons',
            localField: 'info.iconId',
            foreignField: '_id',
            as: 'info.icon'
        }
    },
    { $unwind: '$info.icon' },
    {
        $group: {
            _id: '$_id',
            root: { $mergeObjects: '$$ROOT' },
            info: { $push: '$info' }
        }
    },
    {
        $replaceRoot: {
            newRoot: {
                $mergeObjects: ['$root', '$$ROOT']
            }
        }
    },
    {
        $project: {
            root: 0
        }
    }
]);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I’ll be the first to say that I’m not a MongoDB expert, and I’m even less knowledgeable about building aggregation pipelines. There may be other, better ways of accomplishing this same task. If you know of a better, more efficient pipeline that gives the same results, please let me know!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>Timing Streams in Node.js</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Sat, 11 Jan 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/timing-streams-in-node-js-5enf</link>
      <guid>https://dev.to/petecorey/timing-streams-in-node-js-5enf</guid>
      <description>&lt;p&gt;On a current client project, I was tasked with optimizing a very large, very slow, very CPU-bound &lt;a href="https://nodejs.org/api/stream.html"&gt;stream&lt;/a&gt;-based &lt;a href="https://nodejs.org/api/stream.html#stream_stream_pipeline_streams_callback"&gt;pipeline&lt;/a&gt;. Before I even started to think about optimizing this pipeline, I needed an objective way to measure the execution time of each step of the pipeline.&lt;/p&gt;

&lt;p&gt;Imagine the pipeline in question looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
pipeline(
    httpStream,
    decodeStream,
    parseStream,
    batchStream,
    processStream
);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We’re reading in a stream of JSON-encoded events (&lt;code&gt;httpStream&lt;/code&gt;), making sure they’re appropriately decoded (&lt;code&gt;decodeStream&lt;/code&gt;), JSON parsing each incoming event (&lt;code&gt;parseStream&lt;/code&gt;), batching events together (&lt;code&gt;batchStream&lt;/code&gt;), and finally processing each batch of events (&lt;code&gt;processStream&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Ideally I’d like to measure any or all of these individual steps.&lt;/p&gt;

&lt;p&gt;However, many of these stream implementations are out of our hands. We can’t easily reach in and add timing code. Thankfully, we can easily write a function that decorates a provided stream with a simple runtime calculation.&lt;/p&gt;

&lt;p&gt;Let’s call our decorator function &lt;code&gt;time&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const time = (stream, name) =&amp;gt; {
    return stream;
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;time&lt;/code&gt; function accepts and returns the stream we’ll be decorating, along with a name that describes the provided stream. It should be noted that it’s assumed that &lt;code&gt;stream&lt;/code&gt; implements the &lt;code&gt;Readable&lt;/code&gt; interface.&lt;/p&gt;

&lt;p&gt;What we’re trying to accomplish here is relatively simple. We want to measure the amount of time that elapses between data emission events on our stream. We can use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Console/time"&gt;&lt;code&gt;console.time&lt;/code&gt;/&lt;code&gt;console.timeEnd&lt;/code&gt;&lt;/a&gt; and an event listener to make short work of this task:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const time = (stream, name) =&amp;gt; {
    let timing = false;
    stream.on('data', () =&amp;gt; {
        if (timing) {
            console.timeEnd(name);
        }
        console.time(name);
        timing = true;
    });
    return stream;
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Every time we receive a &lt;code&gt;'data'&lt;/code&gt; event on our stream, we log the duration since the last received &lt;code&gt;'data'&lt;/code&gt; event, and start a new timer. We’re using a &lt;code&gt;timing&lt;/code&gt; flag to ensure that &lt;code&gt;console.timeEnd&lt;/code&gt; isn’t called the first time we receive a &lt;code&gt;'data'&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;Notice that we’re also using the provided &lt;code&gt;name&lt;/code&gt; as the label in our &lt;code&gt;console.time&lt;/code&gt;/&lt;code&gt;console.timeEnd&lt;/code&gt; calls. This keeps us from getting confused when we start measuring multiple stages of our pipeline.&lt;/p&gt;

&lt;p&gt;This solution mostly works. Unfortunately, a &lt;code&gt;data&lt;/code&gt; event isn’t fired when the stream starts processing its first chunk of data. This means that we’re missing a measurement for this first chunk of execution time. Thankfully, we can capture that missing metric by also listening for a &lt;code&gt;'resume'&lt;/code&gt; event, which is called when the stream starts processing its first chunk of data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const time = (stream, name) =&amp;gt; {
    stream.on('resume', () =&amp;gt; {
        console.time(name);
    });
    stream.on('data', () =&amp;gt; {
        console.timeEnd(name);
        console.time(name);
    });
    return stream;
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that we’re no longer concerned about wrapping our &lt;code&gt;console.timeEnd&lt;/code&gt; call in a guard in our &lt;code&gt;'data'&lt;/code&gt; event listener. We know that the &lt;code&gt;'resume'&lt;/code&gt; event handler will always call &lt;code&gt;console.time&lt;/code&gt; before we reach our &lt;code&gt;'data'&lt;/code&gt; event handler, so we have no need for the &lt;code&gt;timing&lt;/code&gt; guard anymore.&lt;/p&gt;

&lt;p&gt;We can use our &lt;code&gt;time&lt;/code&gt; function by decorating any or all of the stages of our pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
await pipeline(
    httpStream,
    decodeStream,
    parseStream,
    time(batchStream, 'batch'),
    time(processStream, 'process')
);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that our runtime durations are finding their way to the logs, we can either use them as-is, or take things a step further and aggregate them for more in-depth data analysis:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
batch: 258.213ms
process: 512.493ms
batch: 239.112ms
process: 475.293ms
...

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As a warning to the reader, I’ll be the first to admit that I’m no &lt;a href="https://github.com/substack/stream-handbook"&gt;stream expert&lt;/a&gt;. That said, this utility function proved invaluable to me, so I thought I’d record what I learned and pass it along for posterity.&lt;/p&gt;

&lt;p&gt;Stream on.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>streams</category>
    </item>
    <item>
      <title>Random Seeds, Lodash, and ES6 Imports</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Wed, 01 Jan 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/random-seeds-lodash-and-es6-imports-2e3a</link>
      <guid>https://dev.to/petecorey/random-seeds-lodash-and-es6-imports-2e3a</guid>
      <description>&lt;p&gt;&lt;a href="http://davidbau.com/archives/2010/01/30/random_seeds_coded_hints_and_quintillions.html"&gt;David Bau’s &lt;code&gt;seedrandom&lt;/code&gt; Javascript library&lt;/a&gt; is an excellent tool for introducing deterministic random values into your Javascript project. After setting a fixed seed, &lt;code&gt;Math.random&lt;/code&gt; will produce a stream of random values. Those same random values will be produced again, in order, the next time you run your program. This is very important when creating generative art or procedurally generated game content.&lt;/p&gt;

&lt;p&gt;However, there’s a small problem when trying to combine &lt;code&gt;seedrandom&lt;/code&gt; with a library like &lt;a href="https://lodash.com/docs/4.17.15"&gt;Lodash&lt;/a&gt;. Ideally, we’d like Lodash to respect our random seed, so methods like &lt;code&gt;shuffle&lt;/code&gt; would always produce a deterministic shuffling. Unfortunately, with a setup like the one described below, this won’t be the case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import _ from "lodash";
import seedrandom from "seedrandom";

seedrandom("seed", { global: true });

_.shuffle([1, 2, 3]); // Ignores our random seed.

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;seedrandom&lt;/code&gt; library &lt;a href="https://github.com/davidbau/seedrandom/blob/f38648743a80fef0319d0d66c2cd37bec116a5a7/seedrandom.js#L94"&gt;wholesale replaces &lt;code&gt;Math.random&lt;/code&gt; with a new pseudo-random number generator&lt;/a&gt;. Because we’re importing &lt;code&gt;lodash&lt;/code&gt; before we initialize &lt;code&gt;seedrandom&lt;/code&gt;, Lodash defines all of its functions, &lt;code&gt;shuffle&lt;/code&gt; included, to use the original reference to &lt;code&gt;Math.random&lt;/code&gt;. We need to initialize &lt;code&gt;seedrandom&lt;/code&gt; before importing Lodash.&lt;/p&gt;

&lt;p&gt;Unfortunately, this won’t work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import seedrandom from "seedrandom";
seedrandom("seed", { global: true });

import _ from "lodash";

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Node.js requires all import statements to be at the top of a module. We can’t initialize &lt;code&gt;seedrandom&lt;/code&gt; before importing Lodash.&lt;/p&gt;

&lt;p&gt;Thankfully, a simple solution exists. We’ll make a new module called &lt;code&gt;seed.js&lt;/code&gt; that simply imports &lt;code&gt;seedrandom&lt;/code&gt; and then initializes it with our seed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import seedrandom from "seedrandom";

seedrandom("seed", { global: true });

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next we can import our local &lt;code&gt;"./seed.js"&lt;/code&gt; module before importing Lodash:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import "./seed.js";
import _ from "lodash";

_.shuffle([1, 2, 3]); // Produces deterministic shufflings!

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And with that small change &lt;code&gt;seedrandom&lt;/code&gt;, Lodash, and ES6-style imports all play nicely together. Our &lt;code&gt;shuffle&lt;/code&gt; function will now product deterministic shufflings based on the seed we pass into &lt;code&gt;seedrandom&lt;/code&gt;!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>generative</category>
      <category>lodash</category>
    </item>
    <item>
      <title>Count the Divisible Numbers</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Mon, 25 Nov 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/count-the-divisible-numbers-3pn4</link>
      <guid>https://dev.to/petecorey/count-the-divisible-numbers-3pn4</guid>
      <description>&lt;p&gt;Let’s try our hand at using a property test driven approach to solving a Codewars code kata. The kata we’ll be solving today is &lt;a href="https://www.codewars.com/kata/count-the-divisible-numbers/train/javascript"&gt;“Count the Divisible Numbers”&lt;/a&gt;. We’ll be solving this kata using Javascript, and using &lt;a href="https://github.com/dubzzz/fast-check/blob/master/README.md"&gt;&lt;code&gt;fast-check&lt;/code&gt;&lt;/a&gt; alongside &lt;a href="https://jestjs.io/"&gt;Jest&lt;/a&gt; as our property-based testing framework.&lt;/p&gt;

&lt;p&gt;The kata’s prompt is as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Complete the [&lt;code&gt;divisibleCount&lt;/code&gt;] function that takes 3 numbers &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;k&lt;/code&gt; (where &lt;code&gt;x ≤ y&lt;/code&gt;), and returns the number of integers within the range &lt;code&gt;[x..y]&lt;/code&gt; (both ends included) that are divisible by &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Writing Our Property Test
&lt;/h2&gt;

&lt;p&gt;We could try to translate this prompt directly into a property test by generating three integers, &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;k&lt;/code&gt;, and verifying that the result of &lt;code&gt;divisibleCount(x, y, k)&lt;/code&gt; matches our expected result, but we’d have to duplicate our implementation of &lt;code&gt;divisibleCount&lt;/code&gt; to come up with that “expected result.” Who’s to say our test’s implementation wouldn’t be flawed?&lt;/p&gt;

&lt;p&gt;We need a more obviously correct way of generating test cases.&lt;/p&gt;

&lt;p&gt;Instead of generating three integers, &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;k&lt;/code&gt;, we’ll generate our starting point, &lt;code&gt;x&lt;/code&gt;, the number we’re testing for divisibility, &lt;code&gt;k&lt;/code&gt;, and the number of divisible numbers we expect in our range, &lt;code&gt;n&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
test("it works", () =&amp;gt; {
  fc.assert(
    fc.property(fc.integer(), fc.integer(), fc.nat(), (x, k, n) =&amp;gt; {
      // TODO ...
    })
  );
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Armed with &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, and &lt;code&gt;n&lt;/code&gt;, we can compute the end of our range, &lt;code&gt;y&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let y = x + n * k;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we’ll pass &lt;code&gt;x&lt;/code&gt;, our newly commuted &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;k&lt;/code&gt; into &lt;code&gt;divisibleCount&lt;/code&gt; and assert that the result matches our expected value of &lt;code&gt;n&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
return n === divisibleCount(x, y, k);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our final property test looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
test("it works", () =&amp;gt; {
  fc.assert(
    fc.property(fc.integer(), fc.integer(), fc.nat(), (x, k, n) =&amp;gt; {
      let y = x + n * k;
      return n === divisibleCount(x, y, k);
    })
  );
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Beautiful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our First Solution
&lt;/h2&gt;

&lt;p&gt;Coming up with a solution to this problem is fairly straight-forward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const divisibleCount = (x, y, k) =&amp;gt; {
  return _.chain(y - x)
    .range()
    .map(n =&amp;gt; x + n)
    .reject(n =&amp;gt; n % k)
    .size()
    .value();
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We generate an array of integers from &lt;code&gt;x&lt;/code&gt; to &lt;code&gt;y&lt;/code&gt;, reject those that aren’t divisible by &lt;code&gt;k&lt;/code&gt;, and return the size of the resulting array.&lt;/p&gt;

&lt;p&gt;Unfortunately, this simple solution doesn’t work as expected. Our property test reports a failing counterexample of &lt;code&gt;[0, 0, 1]&lt;/code&gt; values for &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, and &lt;code&gt;n&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
$ jest
 FAIL ./index.test.js
  ✕ it works (10ms)

  ● it works

    Property failed after 1 tests
    { seed: 1427202042, path: "0:0:0:1:0:0:0", endOnFailure: true }
    Counterexample: [0,0,1]
    Shrunk 6 time(s)
    Got error: Property failed by returning false

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Looking at our solution, this makes sense. The result of &lt;code&gt;n % 0&lt;/code&gt; is undefined. Unfortunately, the kata doesn’t specify what the behavior of our solution should be when &lt;code&gt;k&lt;/code&gt; equals &lt;code&gt;0&lt;/code&gt;, so we’re left to figure that out ourselves.&lt;/p&gt;

&lt;p&gt;Let’s just set up a precondition in our test that &lt;code&gt;k&lt;/code&gt; should never equal &lt;code&gt;0&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
test("it works", () =&amp;gt; {
  fc.assert(
    fc.property(fc.integer(), fc.integer(), fc.nat(), (x, k, n) =&amp;gt; {
      fc.pre(k !== 0);
      let y = x + n * k;
      return n === divisibleCount(x, y, k);
    })
  );
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great!&lt;/p&gt;

&lt;p&gt;Unfortunately, there’s another problem. Without putting an upper bound on the size of &lt;code&gt;n * k&lt;/code&gt;, our solution will generate potentially massive arrays. This will quickly eat through the memory allocated to our process and result in a crash.&lt;/p&gt;

&lt;p&gt;Let’s add some upper and lower bounds to our generated &lt;code&gt;k&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
test("it works", () =&amp;gt; {
  fc.assert(
    fc.property(fc.integer(), fc.integer(-100, 100), fc.nat(100), (x, k, n) =&amp;gt; {
      fc.pre(k !== 0);
      let y = x + n * k;
      return n === divisibleCount(x, y, k);
    })
  );
});

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Perfect. Our starting integer, &lt;code&gt;x&lt;/code&gt;, can be any positive or negative integer, but our generated values of &lt;code&gt;k&lt;/code&gt; are clamped between &lt;code&gt;-100&lt;/code&gt; and &lt;code&gt;100&lt;/code&gt;, and &lt;code&gt;n&lt;/code&gt; ranges from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;100&lt;/code&gt;. These values should be large enough to thoroughly test our solution, and small enough to prevent memory issues from arising.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our Second Solution
&lt;/h2&gt;

&lt;p&gt;In hindsight, our solution seems to be making inefficient use of both time and memory. If we consider the fact that our property test is computing &lt;code&gt;y&lt;/code&gt; in terms of &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;n&lt;/code&gt;, and &lt;code&gt;k&lt;/code&gt;, it stands to reason that we should be able to compute &lt;code&gt;n&lt;/code&gt;, our desired result, in terms of &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;k&lt;/code&gt;. If we can manage this, our solution will run in both constant time and constant space.&lt;/p&gt;

&lt;p&gt;Let’s use some algebra and work our way backwards from calculating &lt;code&gt;y&lt;/code&gt; to calculating &lt;code&gt;n&lt;/code&gt;. If &lt;code&gt;y = x + n * k&lt;/code&gt;, that means that &lt;code&gt;y - x = n * k&lt;/code&gt;. Dividing by &lt;code&gt;k&lt;/code&gt; gives us our equation for computing &lt;code&gt;n&lt;/code&gt;: &lt;code&gt;n = (y - x) / k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let’s replace our original &lt;code&gt;divisibleCount&lt;/code&gt; solution with this equation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const divisibleCount = (x, y, k) =&amp;gt; (y - x) / k;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And rerun our test suite:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
$ jest
 PASS ./index.test.js
  ✓ it works (8ms)

  Test Suites: 1 passed, 1 total
  Tests: 1 passed, 1 total

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Wonderful!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codewars</category>
      <category>kata</category>
      <category>testing</category>
    </item>
    <item>
      <title>Setting Apollo Context from HTTP Headers in a Meteor Application</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Wed, 13 Nov 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/setting-apollo-context-from-http-headers-in-a-meteor-application-2e25</link>
      <guid>https://dev.to/petecorey/setting-apollo-context-from-http-headers-in-a-meteor-application-2e25</guid>
      <description>&lt;p&gt;I was recently tasked with modifying an &lt;a href="https://www.apollographql.com/"&gt;Apollo&lt;/a&gt;-based GraphQL endpoint served by a &lt;a href="https://www.meteor.com/"&gt;Meteor&lt;/a&gt; application to set a field in the resolvers’ context based on a HTTP header pulled out of the incoming GraphQL request.&lt;/p&gt;

&lt;p&gt;This should be an easy, well-documented ask.&lt;/p&gt;

&lt;p&gt;Unfortunately, Meteor’s Apollo integration seems to exist in an awkward, undocumented state. The &lt;code&gt;createApolloServer&lt;/code&gt; function exposed by &lt;a href="https://atmospherejs.com/meteor/apollo"&gt;the &lt;code&gt;meteor/apollo&lt;/code&gt; package&lt;/a&gt; doesn’t seem to have any real documentation anywhere I could find, and &lt;a href="https://github.com/apollographql/meteor-integration"&gt;the Github repository&lt;/a&gt; linked to by the package doesn’t seem to relate to the code in question.&lt;/p&gt;

&lt;p&gt;How can I access the current HTTP request when building my resolvers’ context?&lt;/p&gt;

&lt;p&gt;With no documentation to guide me, I dove into the package’s source code on my machine to find answers. The source for the package in question lives in &lt;code&gt;~/.meteor/packages/apollo/&amp;lt;version&amp;gt;/os/src/main-server.js&lt;/code&gt; on my machine. The &lt;code&gt;createApolloServer&lt;/code&gt; function accepts a &lt;code&gt;customOptions&lt;/code&gt; object as its first argument. I quickly learned after digging through the source that if &lt;code&gt;customOptions&lt;/code&gt; is a function, &lt;code&gt;createApolloServer&lt;/code&gt; will call that function with the current request (&lt;code&gt;req&lt;/code&gt;) as its only argument, and use the function call’s result as the value of &lt;code&gt;customOptions&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const customOptionsObject =
  typeof customOptions === 'function'
    ? customOptions(req) 
    : customOptions;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This means we need to change our current call to &lt;code&gt;createApolloServer&lt;/code&gt; from something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import { createApolloServer } from "meteor/apollo";

createApolloServer({ schema }, { path });

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
createApolloServer(
  req =&amp;gt; ({
    context: {
      someValue: req.headers['some-value']
    },
    schema
  }),
  { path }
);

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This information is probably only pertinent to myself and an ever-shrinking number of people, but if you’re one of those people, I hope this helps.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>meteor</category>
      <category>apollo</category>
      <category>graphql</category>
    </item>
    <item>
      <title>The Collatz Sequence in J</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Wed, 06 Nov 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/the-collatz-sequence-in-j-47c1</link>
      <guid>https://dev.to/petecorey/the-collatz-sequence-in-j-47c1</guid>
      <description>&lt;p&gt;I’ve been playing around quite a bit with the &lt;a href="https://en.wikipedia.org/wiki/Collatz_conjecture"&gt;Collatz Conjecture&lt;/a&gt; after watching &lt;a href="https://www.youtube.com/watch?v=EYLWxwo1Ed8"&gt;the latest Coding Train video&lt;/a&gt; on the subject. In an effort to keep my J muscles from atrophying, I decided to use &lt;a href="https://www.jsoftware.com/#/"&gt;the language&lt;/a&gt; to implement a Collatz sequence generator.&lt;/p&gt;

&lt;p&gt;At its heart, the Collatz sequence is a conditional expression. If the current number is even, the next number in the sequence is the current number divided by two. If the current number is odd, the next number in the sequence is one plus the current number multiplied by three:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LQT_nWTT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-11-06-the-collatz-sequence-in-j/formula.svg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LQT_nWTT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-11-06-the-collatz-sequence-in-j/formula.svg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can write each of these branches with some straight-forward J code. Our even case is simply the divide (&lt;a href="https://www.jsoftware.com/help/dictionary/d130.htm"&gt;&lt;code&gt;%&lt;/code&gt;&lt;/a&gt;) verb bonded (&lt;a href="https://www.jsoftware.com/help/dictionary/d630n.htm"&gt;&lt;code&gt;&amp;amp;&lt;/code&gt;&lt;/a&gt;) to &lt;code&gt;2&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   even =: %&amp;amp;2

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And our odd case is a &lt;a href="https://www.jsoftware.com/help/jforc/forks_hooks_and_compound_adv.htm"&gt;“monadic noun fork”&lt;/a&gt; of &lt;code&gt;1&lt;/code&gt; plus (&lt;a href="https://www.jsoftware.com/help/dictionary/d100.htm"&gt;&lt;code&gt;+&lt;/code&gt;&lt;/a&gt;) &lt;code&gt;3&lt;/code&gt; bonded (&lt;a href="https://www.jsoftware.com/help/dictionary/d630n.htm"&gt;&lt;code&gt;&amp;amp;&lt;/code&gt;&lt;/a&gt;) to multiply (&lt;a href="https://www.jsoftware.com/help/dictionary/d110.htm"&gt;&lt;code&gt;*&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   odd =: 1 + 3&amp;amp;*

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can tie together those two verbs and use agenda (&lt;a href="https://www.jsoftware.com/help/dictionary/d621.htm"&gt;&lt;code&gt;@.&lt;/code&gt;&lt;/a&gt;) to pick which one to call based on whether the argument is even:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   next =: even`odd@.pick

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;pick&lt;/code&gt; verb is testing for even numbers by checking if &lt;a href="https://www.jsoftware.com/help/dictionary/dconsf.htm"&gt;&lt;code&gt;1:&lt;/code&gt;&lt;/a&gt; equals (&lt;a href="https://www.jsoftware.com/help/dictionary/d000.htm"&gt;&lt;code&gt;=&lt;/code&gt;&lt;/a&gt;) &lt;code&gt;2&lt;/code&gt; bonded to the residue verb (&lt;a href="https://www.jsoftware.com/help/dictionary/d230.htm"&gt;&lt;code&gt;|&lt;/code&gt;&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   pick =: 1:=2&amp;amp;|

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can test our &lt;code&gt;next&lt;/code&gt; verb by running it against numbers with known next values. After &lt;code&gt;3&lt;/code&gt;, we’d expect &lt;code&gt;10&lt;/code&gt;, and after &lt;code&gt;10&lt;/code&gt;, we’d expect &lt;code&gt;5&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   next 3
10
   next 10
5

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Fantastic!&lt;/p&gt;

&lt;p&gt;J has an amazing verb, power (&lt;a href="https://www.jsoftware.com/help/dictionary/d202n.htm"&gt;&lt;code&gt;^:&lt;/code&gt;&lt;/a&gt;), that can be used to find the “limit” of a provided verb by continuously reapplying that verb to its result until a repeated result is encountered. If we pass power boxed infinity (&lt;code&gt;&amp;lt;_&lt;/code&gt;) as an argument, it’ll build up a list of all the intermediate results.&lt;/p&gt;

&lt;p&gt;This is exactly what we want. To construct our Collatz sequence, we’ll find the limit of &lt;code&gt;next&lt;/code&gt; for a given input, like &lt;code&gt;12&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   next^:(&amp;lt;_) 12

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But wait, there’s a problem!&lt;/strong&gt; A loop exists in the Collatz sequences between &lt;code&gt;4&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, and &lt;code&gt;1&lt;/code&gt;. When we call &lt;code&gt;next&lt;/code&gt; on &lt;code&gt;1&lt;/code&gt;, we’ll receive &lt;code&gt;4&lt;/code&gt;. Calling &lt;code&gt;next&lt;/code&gt; on &lt;code&gt;4&lt;/code&gt; returns &lt;code&gt;2&lt;/code&gt;, and calling &lt;code&gt;next&lt;/code&gt; on &lt;code&gt;2&lt;/code&gt; returns &lt;code&gt;1&lt;/code&gt;. Our &lt;code&gt;next&lt;/code&gt; verb never converges to a single value.&lt;/p&gt;

&lt;p&gt;To get over this hurdle, we’ll write one last verb, &lt;code&gt;collatz&lt;/code&gt;, that checks if the argument is &lt;code&gt;1&lt;/code&gt; before applying &lt;code&gt;next&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   collatz =: next`]@.(1&amp;amp;=)

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Armed with this new, converging &lt;code&gt;collatz&lt;/code&gt; verb, we can try finding our limit again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   collatz^:(&amp;lt;_) 12
12 6 3 10 5 16 8 4 2 1

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Success! We’ve successfully implemented a Collatz sequence generator using the J programming langauge. Just for fun, let’s plot the sequence starting with &lt;code&gt;1000&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
   require 'plot'
   plot collatz^:(&amp;lt;_) 1000

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7Tu6diSD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-11-06-the-collatz-sequence-in-j/plot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7Tu6diSD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-11-06-the-collatz-sequence-in-j/plot.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>j</category>
      <category>codingtrain</category>
    </item>
    <item>
      <title>Rendering a React Application Across Multiple Containers</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Mon, 14 Oct 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/rendering-a-react-application-across-multiple-containers-m2b</link>
      <guid>https://dev.to/petecorey/rendering-a-react-application-across-multiple-containers-m2b</guid>
      <description>&lt;p&gt;A few of my recent articles have been embedding limited builds of &lt;a href="https://www.gloriousvoiceleader.com/"&gt;Glorious Voice Leader&lt;/a&gt; directly into the page. At first, this presented an interesting challenge. How could I render a single React application across multiple container nodes, while maintaining shared state between all of them?&lt;/p&gt;

&lt;p&gt;While the solution I came up with probably isn’t &lt;em&gt;best practice&lt;/em&gt;, it works!&lt;/p&gt;

&lt;p&gt;As a quick example, imagine you have a simple React component that manages a single piece of state. The user can change that state by pressing one of two buttons:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const App = () =&amp;gt; {
  let [value, setValue] = useState("foo");
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setValue("foo")}&amp;gt;
        Value is "{value}". Click to change to "foo"!
      &amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setValue("bar")}&amp;gt;
        Value is "{value}". Click to change to "bar"!
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Normally, we’d render our &lt;code&gt;App&lt;/code&gt; component into a container in the DOM using &lt;code&gt;ReactDOM.render&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
ReactDOM.render(&amp;lt;App /&amp;gt;, document.getElementById('root'));

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But what if we want to render our buttons in two different &lt;code&gt;div&lt;/code&gt; elements, spread across the page? Obviously, we could build out two different components, one for each button, and render these components in two different DOM containers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const Foo = () =&amp;gt; {
  let [value, setValue] = useState("foo");
  return (
    &amp;lt;button onClick={() =&amp;gt; setValue("foo")}&amp;gt;
      Value is "{value}". Click to change to "foo"!
    &amp;lt;/button&amp;gt;
  );
};

const Bar = () =&amp;gt; {
  let [value, setValue] = useState("foo");
  return (
    &amp;lt;button onClick={() =&amp;gt; setValue("bar")}&amp;gt;
      Value is "{value}". Click to change to "bar"!
    &amp;lt;/button&amp;gt;
  );
};

ReactDOM.render(&amp;lt;Foo /&amp;gt;, document.getElementById('foo'));
ReactDOM.render(&amp;lt;Bar /&amp;gt;, document.getElementById('bar'));

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But this solution has a problem. Our &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt; components maintain their own versions of &lt;code&gt;value&lt;/code&gt;, so a change in one component won’t affect the other.&lt;/p&gt;

&lt;p&gt;Amazingly, it turns out that we can create an &lt;code&gt;App&lt;/code&gt; component which maintains our shared state, render that component into our &lt;code&gt;#root&lt;/code&gt; container, and within &lt;code&gt;App&lt;/code&gt; we can make additional calls to &lt;code&gt;ReactDOM.render&lt;/code&gt; to render our &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt; components. When we call &lt;code&gt;ReactDOM.render&lt;/code&gt; we can pass down our state value and setters for later use in &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const App = () =&amp;gt; {
  let [value, setValue] = useState("foo");
  return (
    &amp;lt;&amp;gt;
      {ReactDOM.render(
        &amp;lt;Foo value={value} setValue={setValue} /&amp;gt;,
        document.getElementById("foo")
      )}
      {ReactDOM.render(
        &amp;lt;Bar value={value} setValue={setValue} /&amp;gt;,
        document.getElementById("bar")
      )}
    &amp;lt;/&amp;gt;
  );
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt; components can now use the &lt;code&gt;value&lt;/code&gt; and &lt;code&gt;setValue&lt;/code&gt; props provided to them instead of maintaining their own isolated state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const Foo = ({ value, setValue }) =&amp;gt; {
  return (
    &amp;lt;button onClick={() =&amp;gt; setValue("foo")}&amp;gt;
      Value is "{value}". Click to change to "foo"!
    &amp;lt;/button&amp;gt;
  );
};

const Bar = ({ value, setValue }) =&amp;gt; {
  return (
    &amp;lt;button onClick={() =&amp;gt; setValue("bar")}&amp;gt;
      Value is "{value}". Click to change to "bar"!
    &amp;lt;/button&amp;gt;
  );
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And everything works! Our &lt;code&gt;App&lt;/code&gt; is “rendered” to our &lt;code&gt;#root&lt;/code&gt; DOM element, though nothing actually appears there, and our &lt;code&gt;Foo&lt;/code&gt; and &lt;code&gt;Bar&lt;/code&gt; components are rendered into &lt;code&gt;#foo&lt;/code&gt; and &lt;code&gt;#bar&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;Honestly, I’m amazed this works at all. I can’t imagine this is an intended use case of React, but the fact that it’s still a possibility made my life much easier.&lt;/p&gt;

&lt;p&gt;Happy hacking.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Animating a Canvas with Phoenix LiveView: An Update</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Tue, 01 Oct 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/animating-a-canvas-with-phoenix-liveview-an-update-b95</link>
      <guid>https://dev.to/petecorey/animating-a-canvas-with-phoenix-liveview-an-update-b95</guid>
      <description>&lt;p&gt;In my previous post on &lt;a href="https://dev.to/blog/2019/09/02/animating-a-canvas-with-phoenix-liveview"&gt;animating an HTML5 canvas using Phoenix LiveView&lt;/a&gt;, we used both a &lt;code&gt;phx-hook&lt;/code&gt; attribute and a &lt;code&gt;phx-update="ignore"&lt;/code&gt; attribute simultaneously on a single DOM element. The goal was to ignore DOM updates (&lt;code&gt;phx-update="ignore"&lt;/code&gt;), while still receiving updated data from our server (&lt;code&gt;phx-hook&lt;/code&gt;) via our &lt;code&gt;data-particles&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;Unfortunately, the technique of using both &lt;code&gt;phx-hook&lt;/code&gt; and &lt;code&gt;phx-update="ignore"&lt;/code&gt; on a single component no longer works as of &lt;code&gt;phoenix_live_view&lt;/code&gt; version &lt;code&gt;0.2.0&lt;/code&gt;. The &lt;code&gt;"ignore"&lt;/code&gt; update rule causes our hook’s &lt;code&gt;updated&lt;/code&gt; callback to not be called with updates. In hindsight, the previous behavior doesn’t even make sense, and the new behavior seems much more consistent with the metaphors in play.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/joxyandsuch"&gt;Joxy&lt;/a&gt; pointed this issue out to me, and helped me come up with a workaround. The solution we landed on is to wrap our &lt;code&gt;canvas&lt;/code&gt; component in another DOM element, like a &lt;code&gt;div&lt;/code&gt;. We leave our &lt;code&gt;phx-update="ignore"&lt;/code&gt; on our canvas to preserve our computed width and height attributes, but move our &lt;code&gt;phx-hook&lt;/code&gt; and data attributes to the wrapping &lt;code&gt;div&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&amp;lt;div
  phx-hook="canvas"
  data-particles="&amp;lt;%= Jason.encode!(@particles) %&amp;gt;"
&amp;gt;
  &amp;lt;canvas phx-update="ignore"&amp;gt;
    Canvas is not supported!
  &amp;lt;/canvas&amp;gt;
&amp;lt;/div&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;mounted&lt;/code&gt; callback of our &lt;code&gt;canvas&lt;/code&gt; hook, we need to look to the first child of our &lt;code&gt;div&lt;/code&gt; to find our &lt;code&gt;canvas&lt;/code&gt; element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
mounted() {
  let canvas = this.el.firstElementChild;
  ...
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Finally, we need to pass a reference to a Phoenix &lt;code&gt;Socket&lt;/code&gt; directly into our &lt;code&gt;LiveSocket&lt;/code&gt; constructor to be compatible with our new version of &lt;code&gt;phoenix_live_view&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import { Socket } from "phoenix";
let liveSocket = new LiveSocket("/live", Socket, { hooks });

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that’s all there is to it! Our LiveView-powered confetti generator is back up and running with the addition of a small layer of markup.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DRVmN8tM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/particles.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DRVmN8tM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/particles.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more information on this update, &lt;a href="https://github.com/phoenixframework/phoenix_live_view/issues/388"&gt;be sure to check out this issue I filed&lt;/a&gt; to try to get clarity on the situation. And I’d like to give a huge thanks to Joxy for doing all the hard work in putting this fix together!&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>phoenix</category>
      <category>liveview</category>
    </item>
    <item>
      <title>Apollo Quirks: Polling After Refetching with New Variables</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Mon, 23 Sep 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/apollo-quirks-polling-after-refetching-with-new-variables-3b9d</link>
      <guid>https://dev.to/petecorey/apollo-quirks-polling-after-refetching-with-new-variables-3b9d</guid>
      <description>&lt;p&gt;While working on a recent client project, &lt;a href="https://twitter.com/msestellemarie"&gt;Estelle&lt;/a&gt; and I ran into &lt;em&gt;a fun &lt;a href="https://www.apollographql.com/"&gt;Apollo&lt;/a&gt; quirk&lt;/em&gt;. It turns out that an Apollo query with an active &lt;code&gt;pollInterval&lt;/code&gt; won’t respect new variables provided by calls to &lt;code&gt;refetch&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To demonstrate, imagine we’re rendering a paginated table filled with data pulled from the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Table = () =&amp;gt; {
    let { data } = useQuery(gql`
        query items($page: Int!) {
            items(page: $page) {
                pages
                results {
                    _id
                    result
                }
            }
        }
    `, {
        pollInterval: 5000
    });

    return (
        &amp;lt;&amp;gt;
            &amp;lt;table&amp;gt;
                {data.items.results.map(({ _id, result }) =&amp;gt; (
                    &amp;lt;tr key={_id}&amp;gt;
                        &amp;lt;td&amp;gt;{result}&amp;lt;/td&amp;gt;
                    &amp;lt;/tr&amp;gt;
                ))}
            &amp;lt;/table&amp;gt;
        &amp;lt;/&amp;gt;
    );
};

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The items in our table change over time, so we’re polling our query every five seconds.&lt;/p&gt;

&lt;p&gt;We also want to give the user buttons to quickly navigate to a given page of results. Whenever a user presses the “Page 2” button, for example, we want to &lt;code&gt;refetch&lt;/code&gt; our query with our &lt;code&gt;variables&lt;/code&gt; set to &lt;code&gt;{ page: 2 }&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const Table = () =&amp;gt; {
- let { data } = useQuery(gql`
+ let { data, refetch } = useQuery(gql`
         query items($page: Int!) {
             items(page: $page) {
                 pages
                 results {
                     _id
                     result
                 }
             }
         }
     `, {
         pollInterval: 5000
     });

+ const onClick = page =&amp;gt; {
+ refetch({ variables: { page } });
+ };

     return (
         &amp;lt;&amp;gt;
             &amp;lt;table&amp;gt;
                 {data.items.results.map(({ _id, result }) =&amp;gt; (
                     &amp;lt;tr key={_id}&amp;gt;
                         &amp;lt;td&amp;gt;{result}&amp;lt;/td&amp;gt;
                     &amp;lt;/tr&amp;gt;
                 ))}
             &amp;lt;/table&amp;gt;
+ {_.chain(data.items.pages)
+ .map(page =&amp;gt; (
+ &amp;lt;Button onClick={() =&amp;gt; onClick(page)}&amp;gt;
+ Page {page + 1}
+ &amp;lt;/Button&amp;gt;
+ ))
+ .value()}
         &amp;lt;/&amp;gt;
     );
 };

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This works… for a few seconds. But then we’re unexpectedly brought back to the first page. What’s happening here?&lt;/p&gt;

&lt;p&gt;It turns out that our polling query will always query the server with the variables it was given at the time polling was initialized. So in our case, even though the user advanced to page two, our polling query will fetch page one and render those results.&lt;/p&gt;

&lt;p&gt;So how do we deal with this? &lt;a href="https://github.com/apollographql/apollo-client/issues/3053"&gt;This GitHub issue on the &lt;code&gt;apollo-client&lt;/code&gt; project&lt;/a&gt; suggests calling &lt;code&gt;stopPolling&lt;/code&gt; before changing the query’s variables, and &lt;code&gt;startPolling&lt;/code&gt; to re-enable polling with those new variables.&lt;/p&gt;

&lt;p&gt;In our case, that would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const Table = () =&amp;gt; {
- let { data, refetch } = useQuery(gql`
+ let { data, refetch, startPolling, stopPolling } = useQuery(gql`
         query items($page: Int!) {
             items(page: $page) {
                 pages
                 results {
                     _id
                     result
                 }
             }
         }
     `, {
         pollInterval: 5000
     });

     const onClick = page =&amp;gt; {
+ stopPolling();
         refetch({ variables: { page } });
+ startPolling(5000);
     };

     return (
         &amp;lt;&amp;gt;
             &amp;lt;table&amp;gt;
                 {data.items.results.map(({ _id, result }) =&amp;gt; (
                     &amp;lt;tr key={_id}&amp;gt;
                         &amp;lt;td&amp;gt;{result}&amp;lt;/td&amp;gt;
                     &amp;lt;/tr&amp;gt;
                 ))}
             &amp;lt;/table&amp;gt;
             {_.chain(data.items.pages)
                 .map(page =&amp;gt; (
                 &amp;lt;Button onClick={() =&amp;gt; onClick(page)}&amp;gt;
                     Page {page + 1}
                 &amp;lt;/Button&amp;gt;
                 ))
                 .value()}
         &amp;lt;/&amp;gt;
     );
 };

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And it works! Now our polling queries will fetch from the server with the correctly updated variables. When a user navigates to page two, they’ll stay on page two!&lt;/p&gt;

&lt;p&gt;My best guess for why this is happening, and why the &lt;code&gt;stopPolling&lt;/code&gt;/&lt;code&gt;startPolling&lt;/code&gt; solution works is that &lt;a href="https://github.com/apollographql/apollo-client/blob/2a4e4158e5d1b7249e3b6347d1038e00ec89c579/packages/apollo-client/src/core/QueryManager.ts#L1373-L1399"&gt;when polling is started&lt;/a&gt;, the value of &lt;code&gt;variables&lt;/code&gt; is &lt;a href="https://github.com/apollographql/apollo-client/blob/2a4e4158e5d1b7249e3b6347d1038e00ec89c579/packages/apollo-client/src/core/QueryManager.ts#L1349"&gt;trapped in a closure&lt;/a&gt;. When &lt;code&gt;refetch&lt;/code&gt; is called, &lt;a href="https://github.com/apollographql/apollo-client/blob/master/packages/apollo-client/src/core/ObservableQuery.ts#L323-L329"&gt;it changes the reference to the &lt;code&gt;options.variables&lt;/code&gt; object&lt;/a&gt;, &lt;em&gt;but not the referenced object&lt;/em&gt;. This means the value of &lt;code&gt;options.variables&lt;/code&gt; doesn’t change within polling interval.&lt;/p&gt;

&lt;p&gt;Calling &lt;code&gt;stopPolling&lt;/code&gt; and &lt;code&gt;startPolling&lt;/code&gt; forces our polling interval to restart under a new closure with our new &lt;code&gt;variables&lt;/code&gt; values.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>graphql</category>
      <category>apollo</category>
    </item>
    <item>
      <title>Elixir Style Conditions in Javascript</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Mon, 16 Sep 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/elixir-style-conditions-in-javascript-17o2</link>
      <guid>https://dev.to/petecorey/elixir-style-conditions-in-javascript-17o2</guid>
      <description>&lt;p&gt;Elixir has a useful control flow structure called &lt;a href="https://elixir-lang.org/getting-started/case-cond-and-if.html#cond"&gt;&lt;code&gt;cond&lt;/code&gt;&lt;/a&gt; that lets you branch on arbitrary conditions. Unlike the more common &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch"&gt;&lt;code&gt;switch&lt;/code&gt;&lt;/a&gt; control structure (&lt;a href="https://elixir-lang.org/getting-started/case-cond-and-if.html#case"&gt;&lt;code&gt;case&lt;/code&gt; in Elixir&lt;/a&gt;), &lt;code&gt;cond&lt;/code&gt; doesn’t match against a predetermined value. Instead, it evaluates each condition, in order, looking for the first one that evaluates to a truthy value (not &lt;code&gt;nil&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
numbers = [1, 2, 3]
result = cond do
  4 in numbers -&amp;gt;
    :four
  6 == Enum.sum(numbers) -&amp;gt;
    :sum
  true -&amp;gt;
    :default
end

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This is all probably old hat to you.&lt;/p&gt;

&lt;p&gt;As I mentioned, &lt;code&gt;cond&lt;/code&gt; can be an incredibly useful control structure, and there are times when I’ve missed it while working in languages like Javascript that only have &lt;code&gt;switch&lt;/code&gt; expressions.&lt;/p&gt;

&lt;p&gt;A traditional Javascript implementation of the above (with a little help from &lt;a href="https://lodash.com/"&gt;Lodash&lt;/a&gt;) would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let numbers = [1, 2, 3];
if (_.includes(numbers, 4)) {
  var result = "four";
} else if (6 === _.sum(numbers)) {
  var result = "sum";
} else {
  var result = "default";
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, &lt;a href="https://twitter.com/swyx/status/1163225169676132353"&gt;I recently stumbled upon a trick&lt;/a&gt; that lets you implement a &lt;code&gt;switch&lt;/code&gt; statement in Javascript that behaves very similarly to a &lt;code&gt;cond&lt;/code&gt; expression in Elixir. The key is to &lt;code&gt;switch&lt;/code&gt; on the value of &lt;code&gt;true&lt;/code&gt;. The case expressions that evaluate to &lt;code&gt;true&lt;/code&gt; will match, and their corresponding statements will be evaluated in order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let numbers = [1, 2, 3];
switch (true) {
  case _.includes(numbers, 4):
    var result = "four";
    break;
  case 6 === _.sum(numbers):
    var result = "sum";
    break;
  default:
    var result = "default";
    break;
}

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Whether or not this is any more useful or readable than a series of &lt;code&gt;if&lt;/code&gt;/&lt;code&gt;else&lt;/code&gt; blocks is debatable. That said, this is definitely an interesting example of perspective shifting and seeing old code in a new light. Hopefully you find it as interesting as I do.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Obverse and Under</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Fri, 13 Sep 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/obverse-and-under-3mp</link>
      <guid>https://dev.to/petecorey/obverse-and-under-3mp</guid>
      <description>&lt;p&gt;I previously wrote about &lt;a href="https://dev.to/blog/2019/08/26/prime-parallelograms/"&gt;plotting an “amazing graph”&lt;/a&gt; using &lt;a href="https://www.jsoftware.com/"&gt;the J programming language&lt;/a&gt;. The solution I landed on looked something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require 'plot'
f =: ] - [: #. [: |. #:
'type dot' plot f"0 p: i. 10000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our verb, &lt;code&gt;f&lt;/code&gt;, is taking a very explicit approach by making judicious use of “capped” ([&lt;code&gt;[:&lt;/code&gt;](&lt;a href="https://www.jsoftware.com/help/dictionary/d502.htm)"&gt;https://www.jsoftware.com/help/dictionary/d502.htm)&lt;/a&gt;) verb trains. We’re essentially saying that &lt;code&gt;f&lt;/code&gt; is (&lt;a href="https://www.jsoftware.com/help/dictionary/d001.htm"&gt;&lt;code&gt;=:&lt;/code&gt;&lt;/a&gt;) the given number ([&lt;code&gt;]&lt;/code&gt;](&lt;a href="https://www.jsoftware.com/help/dictionary/d500.htm)"&gt;https://www.jsoftware.com/help/dictionary/d500.htm)&lt;/a&gt;) minus (&lt;a href="https://www.jsoftware.com/help/dictionary/d120.htm"&gt;&lt;code&gt;-&lt;/code&gt;&lt;/a&gt;) the base two (&lt;a href="https://www.jsoftware.com/help/dictionary/d401.htm"&gt;&lt;code&gt;#.&lt;/code&gt;&lt;/a&gt;) of the reverse (&lt;a href="https://www.jsoftware.com/help/dictionary/d231.htm"&gt;&lt;code&gt;|.&lt;/code&gt;&lt;/a&gt;) of the antibase two (&lt;a href="https://www.jsoftware.com/help/dictionary/d402.htm"&gt;&lt;code&gt;#:&lt;/code&gt;&lt;/a&gt;) of the given number.&lt;/p&gt;

&lt;p&gt;Several members of the J community pointed out to me that this verb could be simplified with the help of the “under” (&lt;a href="https://www.jsoftware.com/help/dictionary/d631.htm"&gt;&lt;code&gt;&amp;amp;.&lt;/code&gt;&lt;/a&gt;) conjunction. Let’s dig into what “under” is, and how we can use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Under What?
&lt;/h2&gt;

&lt;p&gt;The best way to think about “under” (&lt;a href="https://www.jsoftware.com/help/dictionary/d631.htm"&gt;&lt;code&gt;&amp;amp;.&lt;/code&gt;&lt;/a&gt;), &lt;a href="https://code.jsoftware.com/wiki/Vocabulary/ampdot"&gt;as explained by the NuVoc page on “under”&lt;/a&gt;, is to think in terms of &lt;a href="https://en.wikipedia.org/wiki/Domain_of_a_function"&gt;domains&lt;/a&gt; and transformations in and out of those domains.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Verb v defines a transformation of the argument(s) (x and) y into the v-domain. Next, verb u operates on the transformed argument(s). Lastly the result is transformed back from the v-domain to the original domain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In our example, the domain of our input is base ten, but the transformation we want to apply (reversal) needs to happen in the base two domain. “Under” (&lt;a href="https://www.jsoftware.com/help/dictionary/d631.htm"&gt;&lt;code&gt;&amp;amp;.&lt;/code&gt;&lt;/a&gt;) can be used to transform our input into base two (&lt;a href="https://www.jsoftware.com/help/dictionary/d402.htm"&gt;&lt;code&gt;#:&lt;/code&gt;&lt;/a&gt;), apply our reversal (&lt;a href="https://www.jsoftware.com/help/dictionary/d231.htm"&gt;&lt;code&gt;|.&lt;/code&gt;&lt;/a&gt;), and transform the result of that reversal back to our original base ten domain with the obverse, or opposite, of our base two verb, anti base (&lt;a href="https://www.jsoftware.com/help/dictionary/d401.htm"&gt;&lt;code&gt;#.&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f =: ] - |. &amp;amp;. #:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that we’re not explicitly stating how to transform the result of our reversal back into our original domain. J knows that the obverse of &lt;a href="https://www.jsoftware.com/help/dictionary/d402.htm"&gt;&lt;code&gt;#:&lt;/code&gt;&lt;/a&gt; is &lt;a href="https://www.jsoftware.com/help/dictionary/d401.htm"&gt;&lt;code&gt;#.&lt;/code&gt;&lt;/a&gt;, and automatically applies it for us.&lt;/p&gt;

&lt;p&gt;Out of the box, J comes with many obverse pairings. “Open” (&lt;a href="https://www.jsoftware.com/help/dictionary/d020.htm"&gt;&lt;code&gt;&amp;gt;&lt;/code&gt;&lt;/a&gt;), for example, is the obverse of “box” (&lt;a href="https://www.jsoftware.com/help/dictionary/d010.htm"&gt;&lt;code&gt;&amp;lt;&lt;/code&gt;&lt;/a&gt;), and visa versa. This pairing is especially useful when applying transformations to boxed values:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1+&amp;amp;.&amp;gt;1;2;3
┌─┬─┬─┐
│2│3│4│
└─┴─┴─┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Check out a full listing of obverse pairs &lt;a href="https://code.jsoftware.com/wiki/Fifty_Shades_of_J/Chapter_12#Obverse_to_Adverse"&gt;at the end of this Shades of J article&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inferred Obverses
&lt;/h2&gt;

&lt;p&gt;Even compound verbs built up of verbs with well-defined obverse pairings can be used with “under” (&lt;a href="https://www.jsoftware.com/help/dictionary/d631.htm"&gt;&lt;code&gt;&amp;amp;.&lt;/code&gt;&lt;/a&gt;). J will correctly infer and apply the compound obverse without any intervention or instruction.&lt;/p&gt;

&lt;p&gt;For example, if we wanted to unbox a list of values and then work with them in the “square root domain” (whatever that means), we could do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1+&amp;amp;.([:%:&amp;gt;)1;2;3
┌────────────────┐
│4 5.82843 7.4641│
└────────────────┘
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;J takes each value, opens it and finds its square root (&lt;code&gt;[:%:&amp;gt;&lt;/code&gt;), adds one to the result, and then squares and boxes up (&lt;code&gt;[:*:&amp;lt;&lt;/code&gt;) the incremented value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explicit Obverses
&lt;/h2&gt;

&lt;p&gt;Even more interestingly, if an obverse pairing isn’t defined or inferable for a given verb, J lets us define our own pairing using the “obverse” (&lt;a href="https://www.jsoftware.com/help/dictionary/d311.htm"&gt;&lt;code&gt;:.&lt;/code&gt;&lt;/a&gt;) verb.&lt;/p&gt;

&lt;p&gt;As an example, imagine that we have a JSON string holding an array of values. We want to parse our string, perform some operation on those values, and then serialize the resulting list back into JSON.&lt;/p&gt;

&lt;p&gt;We can use the &lt;code&gt;dec_json&lt;/code&gt; and &lt;code&gt;enc_json&lt;/code&gt; verbs provided by &lt;a href="https://github.com/jsoftware/convert_json"&gt;the &lt;code&gt;convert/json&lt;/code&gt; package&lt;/a&gt;, and tell J that the obverse of &lt;code&gt;dec_json&lt;/code&gt; is &lt;code&gt;enc_json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;json =: dec_json :. enc_json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;dec_json&lt;/code&gt; on a JSON array like &lt;code&gt;'[1, 2, 3]'&lt;/code&gt; will return a list of boxed numbers, so we’ll want to open each of these boxes, perform our operation, and box the results back up. This sounds like another job for “under” (&lt;a href="https://www.jsoftware.com/help/dictionary/d631.htm"&gt;&lt;code&gt;&amp;amp;.&lt;/code&gt;&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transform =: 1&amp;amp;+&amp;amp;.&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All together, we can perform our &lt;code&gt;transform&lt;/code&gt; “under” (&lt;a href="https://www.jsoftware.com/help/dictionary/d631.htm"&gt;&lt;code&gt;&amp;amp;.&lt;/code&gt;&lt;/a&gt;) the &lt;code&gt;json&lt;/code&gt; domain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;transform &amp;amp;. json '[1, 2, 3]'
[2,3,4]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And our result is the JSON string &lt;code&gt;'[2,3,4]'&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;“Under” is definitely a very powerful conjunction, and I can see myself using it extensively in the future. Thanks to everyone in the J community who was kind enough to point it out and teach me something new!&lt;/p&gt;

</description>
      <category>j</category>
    </item>
    <item>
      <title>Animating a Canvas with Phoenix LiveView</title>
      <dc:creator>Pete Corey</dc:creator>
      <pubDate>Mon, 02 Sep 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/petecorey/animating-a-canvas-with-phoenix-liveview-1m8i</link>
      <guid>https://dev.to/petecorey/animating-a-canvas-with-phoenix-liveview-1m8i</guid>
      <description>&lt;p&gt;&lt;a href="https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html"&gt;Phoenix LiveView&lt;/a&gt; recently released a new feature called “hooks” that introduces &lt;a href="https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#module-js-interop-and-client-controlled-dom"&gt;Javascript interoperability into the LiveView lifecycle&lt;/a&gt;. Put simply, we can now run arbitrary Javascript every time a DOM node is changed by LiveView! &lt;strong&gt;LiveView hooks are a complete game changer&lt;/strong&gt; , and open the doors to a whole new world of applications that can be built with this amazing technology.&lt;/p&gt;

&lt;p&gt;As a proof of concept, let’s use LiveView hooks to animate an HTML5 canvas in real time using data provided by the server!&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Set Up
&lt;/h2&gt;

&lt;p&gt;To keep this article short(er), we’ll skip the rigmarole of configuring your application to use LiveView. If you need help with this step, I highly recommend you &lt;a href="https://elixirschool.com/blog/phoenix-live-view/"&gt;check out Sophie DeBenedetto’s thorough walkthrough&lt;/a&gt;. Be sure to cross reference with &lt;a href="https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html"&gt;the official documentation&lt;/a&gt;, as things are moving quickly in the LiveView world.&lt;/p&gt;

&lt;p&gt;Moving forward, let’s assume that you have a bare-bones LiveView component attached to a route that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;defmodule LiveCanvasWeb.PageLive do
  use Phoenix.LiveView

  def render(assigns) do
    ~L"""
    &amp;lt;canvas&amp;gt;
      Canvas is not supported!
    &amp;lt;/canvas&amp;gt;
    """
  end

  def mount(_session, socket) do
    {:ok, socket}
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We’ll also assume that your &lt;code&gt;assets/js/app.js&lt;/code&gt; file is creating a LiveView connection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import LiveSocket from "phoenix_live_view";

let liveSocket = new LiveSocket("/live");

liveSocket.connect();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we’re on the same page, let’s get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating Data to Animate
&lt;/h2&gt;

&lt;p&gt;Before we start animating on the client, we should have some data to animate. We’ll start by storing a numeric value called &lt;code&gt;i&lt;/code&gt; in our LiveView process’ &lt;code&gt;assigns&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def mount(_session, socket) do
  {:ok, assign(socket, :i, 0)}
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we’ll increase &lt;code&gt;i&lt;/code&gt; by instructing our LiveView process to send an &lt;code&gt;:update&lt;/code&gt; message to itself after a delay of &lt;code&gt;16&lt;/code&gt; milliseconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def mount(_session, socket) do
  Process.send_after(self(), :update, 16)
  {:ok, assign(socket, :i, 0)}
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When we handle the &lt;code&gt;:udpate&lt;/code&gt; message in our process, we’ll schedule another recursive call to &lt;code&gt;:update&lt;/code&gt; and increment the value of &lt;code&gt;i&lt;/code&gt; in our socket’s &lt;code&gt;assigns&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def handle_info(:update, %{assigns: %{i: i}} = socket) do
  Process.send_after(self(), :update, 16)
  {:noreply, assign(socket, :i, i + 0.05)}
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our LiveView process now has an &lt;code&gt;i&lt;/code&gt; value that’s slowly increasing by &lt;code&gt;0.05&lt;/code&gt; approximately sixty times per second.&lt;/p&gt;

&lt;p&gt;Now that we have some data to animate, let’s add a &lt;code&gt;canvas&lt;/code&gt; to our LiveView’s template to hold our animation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def render(assigns) do
  ~L"""
  &amp;lt;canvas data-i="&amp;lt;%= @i %&amp;gt;"&amp;gt;
    Canvas is not supported!
  &amp;lt;/canvas&amp;gt;
  """
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that we’re associating the value of &lt;code&gt;i&lt;/code&gt; with our canvas by assigning it to a data attribute on the DOM element. Every time &lt;code&gt;i&lt;/code&gt; changes in our process’ state, LiveView will update our &lt;code&gt;canvas&lt;/code&gt; and set the value of &lt;code&gt;data-i&lt;/code&gt; to the new value of &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is great, but to render an animation in our &lt;code&gt;canvas&lt;/code&gt;, we need some way of executing client-side Javascript every time our &lt;code&gt;canvas&lt;/code&gt; updates. Thankfully, LiveView’s new hook functionality lets us do exactly that!&lt;/p&gt;

&lt;h2&gt;
  
  
  Hooking Into LiveView
&lt;/h2&gt;

&lt;p&gt;LiveView hooks lets us execute Javascript &lt;a href="https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#module-js-interop-and-client-controlled-dom"&gt;at various points in a DOM node’s lifecycle&lt;/a&gt;, such as when the node is first &lt;code&gt;mounted&lt;/code&gt;, when it’s &lt;code&gt;updated&lt;/code&gt; by LiveView, when it’s &lt;code&gt;destroyed&lt;/code&gt; and removed from the DOM, and when it becomes &lt;code&gt;disconnected&lt;/code&gt; or &lt;code&gt;reconnected&lt;/code&gt; to our Phoenix server.&lt;/p&gt;

&lt;p&gt;To hook into LiveView’s client-side lifecycle, we need to create a set of &lt;code&gt;hooks&lt;/code&gt; and pass them into our &lt;code&gt;LiveSocket&lt;/code&gt; constructor. Let’s create a hook that initializes our &lt;code&gt;canvas&lt;/code&gt;’ rendering context when the element mounts, and renders a static circle every time the element updates:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let hooks = {
  canvas: {
    mounted() {
      let canvas = this.el;
      let context = canvas.getContext("2d");

      Object.assign(this, { canvas, context });
    },
    updated() {
      let { canvas, context } = this;

      let halfHeight = canvas.height / 2;
      let halfWidth = canvas.width / 2;
      let smallerHalf = Math.min(halfHeight, halfWidth);

      context.clearRect(0, 0, canvas.width, canvas.height);
      context.fillStyle = "rgba(128, 0, 255, 1)";
      context.beginPath();
      context.arc(
        halfWidth,
        halfHeight,
        smallerHalf / 16,
        0,
        2 * Math.PI
      );
      context.fill();
    }
  }
};

let liveSocket = new LiveSocket("/live", { hooks });

liveSocket.connect();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that we’re storing a reference to our &lt;code&gt;canvas&lt;/code&gt; and our newly created rendering &lt;code&gt;context&lt;/code&gt; on &lt;code&gt;this&lt;/code&gt;. When LiveView calls our lifecycle callbacks, &lt;a href="https://github.com/phoenixframework/phoenix_live_view/blob/140cfb3f2924cb6595b9bb2979a1963ac0de6eb8/assets/js/phoenix_live_view.js#L904-L906"&gt;&lt;code&gt;this&lt;/code&gt; points to an instance of a &lt;code&gt;ViewHook&lt;/code&gt; class&lt;/a&gt;. A &lt;code&gt;ViewHook&lt;/code&gt; instance holds references to our provided lifecycle methods, a reference to the current DOM node in &lt;code&gt;el&lt;/code&gt;, &lt;a href="https://github.com/phoenixframework/phoenix_live_view/blob/140cfb3f2924cb6595b9bb2979a1963ac0de6eb8/assets/js/phoenix_live_view.js#L1100-L1104"&gt;and various other pieces of data related to the current set of hooks&lt;/a&gt;. As long as we’re careful and we don’t overwrite these fields, we’re safe to store our own data in &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we need to instruct LiveView to attach this new set of &lt;code&gt;canvas&lt;/code&gt; hooks to our &lt;code&gt;canvas&lt;/code&gt; DOM element. We can do that with the &lt;code&gt;phx-hook&lt;/code&gt; attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;canvas
  data-i="&amp;lt;%= @i %&amp;gt;"
  phx-hook="canvas"
&amp;gt;
  Canvas is not supported!
&amp;lt;/canvas&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;When our page reloads, we should see our circle rendered gloriously in the center of our canvas.&lt;/p&gt;

&lt;h2&gt;
  
  
  Resizing the Canvas
&lt;/h2&gt;

&lt;p&gt;On some displays, our glorious circle may appear to be fuzzy or distorted. This can be fixed by scaling our canvas to match the pixel density of our display. While we’re at it, we might want to resize our canvas to fill the entire available window space.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zAnh2OwT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/blurry.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zAnh2OwT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/blurry.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can accomplish both of these in our &lt;code&gt;mounted&lt;/code&gt; callback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mounted() {
  let canvas = this.el;
  let context = canvas.getContext("2d");
  let ratio = getPixelRatio(context);

  resize(canvas, ratio);

  Object.assign(this, { canvas, context });
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;getPixelRatio&lt;/code&gt; is a helper function that determines the ratio of physical pixels in the current device’s screen to “CSS pixels” which are used within the rendering context of our &lt;code&gt;canvas&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const getPixelRatio = context =&amp;gt; {
  var backingStore =
    context.backingStorePixelRatio ||
    context.webkitBackingStorePixelRatio ||
    context.mozBackingStorePixelRatio ||
    context.msBackingStorePixelRatio ||
    context.oBackingStorePixelRatio ||
    context.backingStorePixelRatio ||
    1;

  return (window.devicePixelRatio || 1) / backingStore;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And &lt;code&gt;resize&lt;/code&gt; is a helper function that modifies the &lt;code&gt;canvas&lt;/code&gt;’ width and height attributes in order to resize our canvas to fit the current window, while fixing any pixel density issues we may be experiencing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const resize = (canvas, ratio) =&amp;gt; {
  canvas.width = window.innerWidth * ratio;
  canvas.height = window.innerHeight * ratio;
  canvas.style.width = `${window.innerWidth}px`;
  canvas.style.height = `${window.innerHeight}px`;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Unfortunately, our &lt;code&gt;canvas&lt;/code&gt; doesn’t seem to be able to hold onto these changes. Subsequent calls to our &lt;code&gt;updated&lt;/code&gt; callback seem to lose our &lt;code&gt;resize&lt;/code&gt; changes, and the canvas reverts back to its original, blurry self. This is because when LiveView updates our &lt;code&gt;canvas&lt;/code&gt; DOM node, it resets our &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; attributes. Not only does this revert our pixel density fix, it also forcefully clears the canvas’ rendering context.&lt;/p&gt;

&lt;p&gt;LiveView has a quick fix for getting around this problem. By setting &lt;code&gt;phx-update&lt;/code&gt; to &lt;code&gt;"ignore"&lt;/code&gt; on our &lt;code&gt;canvas&lt;/code&gt; element, we can instruct LiveView to leave our &lt;code&gt;canvas&lt;/code&gt; element alone after its initial mount.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;canvas
  data-i="&amp;lt;%= @i %&amp;gt;"
  phx-hook="canvas" 
  phx-update="ignore"
&amp;gt;
  Canvas is not supported!
&amp;lt;/canvas&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now our circle should be rendered crisply in the center of our screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iHiJOFEo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/crisp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iHiJOFEo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/crisp.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Animating Our Circle
&lt;/h2&gt;

&lt;p&gt;We didn’t go all this way to render a static circle in our &lt;code&gt;canvas&lt;/code&gt;. Let’s tie everything together and animate our circle based on the ever-changing values of &lt;code&gt;i&lt;/code&gt; provided by the server!&lt;/p&gt;

&lt;p&gt;The first thing we’ll need to do is update our &lt;code&gt;updated&lt;/code&gt; callback to grab the current value of the &lt;code&gt;data-i&lt;/code&gt; attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let i = JSON.parse(canvas.dataset.i);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The value of &lt;code&gt;canvas.dataset.i&lt;/code&gt; will reflect the contents of our &lt;code&gt;data-i&lt;/code&gt; attribute. All data attributes are stored as strings, so a call to &lt;code&gt;JSON.parse&lt;/code&gt; will convert a value of &lt;code&gt;"0.05"&lt;/code&gt; to its numeric counterpart.&lt;/p&gt;

&lt;p&gt;Next, we can update our rendering code to move our circle based on the value of &lt;code&gt;i&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;context.arc(
  halfWidth + (Math.cos(i) * smallerHalf) / 2,
  halfHeight + (Math.sin(i) * smallerHalf) / 2,
  smallerHalf / 16,
  0,
  2 * Math.PI
);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That’s it! With those two changes, our circle will rotate around the center of our canvas based entirely on real-time data provided by our server!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QaRMi2Pr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/spinning.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QaRMi2Pr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/spinning.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Requesting Animation Frames
&lt;/h2&gt;

&lt;p&gt;Our solution works, but by forcing re-renders on the browser, we’re being bad net citizens. Our client may be forcing re-renders when its tab is out of focus, or it may be re-rendering more than sixty times per second, wasting CPU cycles.&lt;/p&gt;

&lt;p&gt;Instead of telling the browser to re-render our &lt;code&gt;canvas&lt;/code&gt; on every LiveView update, we should invert our control over rendering and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame"&gt;request an animation frame from the browser&lt;/a&gt; on every update.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        &amp;lt;p&amp;gt;I find myself producing lots of ranging content from books and articles, like the one you're reading now, to open source software projects. If you like what I'm doing, &amp;lt;strong&amp;gt;nothing shows your support more than signing up for &amp;lt;a href="#newsletter"&amp;gt;my mailing list&amp;lt;/a&amp;gt;&amp;lt;/strong&amp;gt;.&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The process for this is straight forward. In our &lt;code&gt;updated&lt;/code&gt; callback, we’ll wrap our rendering code in a lambda passed into &lt;code&gt;requestAnimationFrame&lt;/code&gt;. We’ll save the resulting request reference to &lt;code&gt;this.animationFrameRequest&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.animationFrameRequest = requestAnimationFrame(() =&amp;gt; {
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.beginPath();
  context.arc(
    halfWidth + (Math.cos(i) * smallerHalf) / 2,
    halfHeight + (Math.sin(i) * smallerHalf) / 2,
    smallerHalf / 16,
    0,
    2 * Math.PI
  );
  context.fill();
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It’s conceivable that our LiveView component may update multiple times before our browser is ready to re-render our &lt;code&gt;canvas&lt;/code&gt;. In those situations, we’ll need to cancel any previously requested animation frames, and re-request a new frame. We can do this by placing a guard just above our call to &lt;code&gt;requestAnimationFrame&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (this.animationFrameRequest) {
  cancelAnimationFrame(this.animationFrameRequest);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With those two changes, our LiveView hooks will now politely request animation frames from the browser, resulting in a smoother experience for everyone involved.&lt;/p&gt;

&lt;h2&gt;
  
  
  Taking it Further
&lt;/h2&gt;

&lt;p&gt;Using a canvas to animate a numeric value updated in real-time by a LiveView process running on the server demonstrates the huge potential power of LiveView hooks, but it’s not much to look at.&lt;/p&gt;

&lt;p&gt;We can take things further by generating and animating a much larger set of data on the server. &lt;a href="https://frozen-coast-73544.herokuapp.com/"&gt;Check out this example project&lt;/a&gt; that simulates over two hundred simple particles, and renders them on the client at approximately sixty frames per second:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DRVmN8tM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/particles.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DRVmN8tM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.petecorey.com/img/2019-09-02-animating-a-canvas-with-phoenix-liveview/particles.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Is it a good idea to take this approach if your goal is to animate a bunch of particles on the client? &lt;strong&gt;Probably not.&lt;/strong&gt; Is it amazing that LiveView gives us the tools to do this? &lt;strong&gt;Absolutely, yes!&lt;/strong&gt; Be sure to &lt;a href="https://github.com/pcorey/live_canvas"&gt;check out the entire source for this example on Github&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Hooks have opened the doors to a world of new possibilities for LiveView-based applications. I hope this demonstration has given you a taste of those possibilities, and I hope you’re as eager as I am to explore what we can do with LiveView moving forward.&lt;/p&gt;

</description>
      <category>elixir</category>
      <category>phoenix</category>
      <category>liveview</category>
    </item>
  </channel>
</rss>
