<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="4.3.4">Jekyll</generator><link href="https://paulfedory.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://paulfedory.com/" rel="alternate" type="text/html" /><updated>2025-01-08T23:24:45-05:00</updated><id>https://paulfedory.com/feed.xml</id><title type="html">Paul Fedory</title><subtitle>Product Engineer &amp; Software Developer</subtitle><author><name>Paul Fedory</name></author><entry><title type="html">Practicing Elixir in the age of AI coding assistants</title><link href="https://paulfedory.com/software/practicing-elixir-in-the-age-of-ai/" rel="alternate" type="text/html" title="Practicing Elixir in the age of AI coding assistants" /><published>2025-01-08T00:00:00-05:00</published><updated>2025-01-08T00:00:00-05:00</updated><id>https://paulfedory.com/software/practicing-elixir-in-the-age-of-ai</id><content type="html" xml:base="https://paulfedory.com/software/practicing-elixir-in-the-age-of-ai/"><![CDATA[<p>I’ve been playing around a lot recently with Elixir and its ecosystem. In particular, I’ve been doing exercises on <a href="https://exercism.org/">Exercism</a> to get better at solving common coding challenges in the <em>Elixir Way™</em>—that is, writing idiomatic Elixir.</p>

<p>For reference, I’m using GitHub’s <a href="https://github.com/features/copilot">Copilot</a> service set to use the Claude Sonnet 3.5 model. I’m leveraging both code autocompletion and a chat interface.</p>

<p>I have a few observations about how having an ever-present AI coding assistant is changing things.</p>

<hr />

<h3 id="hard-to-generate-idiomatic-elixir">Hard to Generate Idiomatic Elixir</h3>

<p>For the most part, the generated code works well. However, the model doesn’t often generate idiomatic Elixir unless explicitly prompted to rewrite the code in a specific way. For example, I’ve noticed it prefers standard control structures (e.g., <code class="language-plaintext highlighter-rouge">if</code>/<code class="language-plaintext highlighter-rouge">else</code>, <code class="language-plaintext highlighter-rouge">case</code>) over function clauses with guards. (one of the fun aspects of writing Elixir!)</p>

<hr />

<h3 id="solving-challenges-fun-or-frustrating">Solving Challenges: Fun or Frustrating?</h3>

<p>On one hand, solving simple challenges with the model was easy and effective. The autocomplete would pop up and suggest the full solution. Great! This was exactly what I wanted, as it removed the need to tediously consider edge cases for straightforward problems.</p>

<p>However, with slightly more challenging exercises, the model would often behave the same way—taking all the fun out of the process. This was, of course, expected. Boo.</p>

<hr />

<h3 id="still-need-to-learn-the-patterns">Still Need to Learn the Patterns</h3>

<p>I feel like the models aren’t quite there yet when it comes to understanding the broader Elixir ecosystem. Maybe I’m wrong, but it seems like they lack familiarity with some of the idiomatic patterns that make Elixir code elegant and concise.</p>

<hr />

<h3 id="reading-and-understanding-the-code-a-different-skillset">Reading and Understanding the Code: A Different Skillset</h3>

<p>Another thing I noticed with slightly more challenging problems: it wasn’t immediately obvious what the generated code was doing. I’d often need to spend time reading and analyzing it—just as I would if it had been written by another engineer, one I didn’t necessarily trust.</p>

<hr />

<h3 id="are-coding-exercises-still-useful">Are coding exercises still useful?</h3>

<p>One thing that strikes me: the coding challenges are solved pretty well with the model. That raises questions about the futility of doing them! Perhaps a new way to learn Elixir, or any other new language, is to watch the AI solve them and then read the code to understand. Like I pointed out, the generated solutions might not be idiomatic. I have no doubt with future models or with better prompts from the Copilot agent, normal Elixir code can be generated.</p>]]></content><author><name>Paul Fedory</name></author><category term="software" /><category term="ai" /><category term="coding" /><category term="elixir" /><category term="beginner" /><summary type="html"><![CDATA[Exploring how AI tools like GitHub Copilot shape coding in Elixir, from simplifying challenges to highlighting the need for understanding idiomatic patterns and generated code.]]></summary></entry><entry><title type="html">Tracking Real-Time Ship Data with Elixir and a WebSocket API</title><link href="https://paulfedory.com/software/tracking-real-time-ship-data-over-websockets/" rel="alternate" type="text/html" title="Tracking Real-Time Ship Data with Elixir and a WebSocket API" /><published>2024-12-08T00:00:00-05:00</published><updated>2024-12-08T00:00:00-05:00</updated><id>https://paulfedory.com/software/tracking-real-time-ship-data-over-websockets</id><content type="html" xml:base="https://paulfedory.com/software/tracking-real-time-ship-data-over-websockets/"><![CDATA[<p class="notice--info"><strong>Who This Blog Post Is For:</strong> This blog post is for Elixir developers, hobbyists interested in real-time APIs, or anyone, like myself, who has never seen a WebSocket API before.</p>

<p>Living on the bank of the Detroit River, I watch massive ships pass by every day. While <a href="https://marinetraffic.com">MarineTraffic</a> can help identify these vessels, I thought: why not build my own display to show real-time ship data? This project began with exploring the <a href="https://aisstream.io/">AIS Stream WebSocket API</a>, my first step in combining Elixir with maritime data.</p>

<figure>
  
<img src="/assets/images/detroit-river-ship.jpg" alt="A ship going down the Detroit river" />

  <figcaption>A freighter navigating the Detroit river. What's its name? Where is it headed? And what's its story?</figcaption>
</figure>

<h2 id="ais-automatic-identification-system">AIS (Automatic Identification System)</h2>

<p>Large ships send out their position information over radio using a system called <a href="https://en.wikipedia.org/wiki/Automatic_identification_system">AIS (Automatic idenfitication system)</a>. AIS radio receivers collect data from nearby ships and can share it online, creating a global network of maritime traffic data. By consuming this data via APIs, you can gain insights into ship movements worldwide—or in your backyard.</p>

<p>I chose to explore the <a href="https://aisstream.io/documentation">AIS Stream WebSocket API</a> to process live AIS messages in real-time. This API provides a WebSocket interface for receiving real-time ship data, which makes it an appropriate project to tackle using Elixir. Elixir is particularly well-suited for consuming real-time APIs over WebSockets, and its advantages stem from its robust concurrency model, lightweight processes, and the fault-tolerant runtime of the underlying Erlang VM (BEAM).</p>

<h2 id="setting-up-the-project">Setting Up the Project</h2>

<p>First, I created a new Elixir project:</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>mix new ais_stream_client
<span class="nb">cd </span>ais_stream_client
</code></pre></div></div>

<p>Then, I added the <code class="language-plaintext highlighter-rouge">websockex</code> library to my dependencies for WebSocket communication and poison for JSON encoding and decoding. Here’s how the <code class="language-plaintext highlighter-rouge">mix.exs</code> file looked:</p>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">defp</span> <span class="n">deps</span> <span class="k">do</span>
  <span class="p">[</span>
    <span class="p">{</span><span class="ss">:websockex</span><span class="p">,</span> <span class="s2">"~&gt; 0.4.3"</span><span class="p">},</span>
    <span class="p">{</span><span class="ss">:poison</span><span class="p">,</span> <span class="s2">"~&gt; 6.0"</span><span class="p">}</span>
  <span class="p">]</span>
<span class="k">end</span>
</code></pre></div></div>

<p>Run <code class="language-plaintext highlighter-rouge">mix deps.get</code> to fetch the dependencies.</p>

<h2 id="creating-the-websocket-client">Creating the WebSocket Client</h2>

<p>Using <code class="language-plaintext highlighter-rouge">websockex</code>, I created a simple WebSocket client module that connects to the AIS Stream WebSocket API and processes incoming messages.
WebSockex works like other Elixir behaviours (like <code class="language-plaintext highlighter-rouge">GenServer</code> or <code class="language-plaintext highlighter-rouge">Supervisor</code>)–you need to implement some callbacks to start using it.</p>

<p>There are only 3 things you need to do to get started with Websockex.</p>

<ol>
  <li>Call <code class="language-plaintext highlighter-rouge">WebSockex.start_link</code> with the URL of the API you’re calling.</li>
  <li>Authenticate yourself by sending a message (with your API key) over the websocket to the API.</li>
  <li>Implement the <code class="language-plaintext highlighter-rouge">WebSockex</code> behaviour and in particular, the <code class="language-plaintext highlighter-rouge">handle_frame</code> callback to handle any messages the API sends you.</li>
</ol>

<p><a href="https://hexdocs.pm/websockex/readme.html">Check out the docs for WebSockex, they’re useful.</a></p>

<p>Here’s the code to implement a WebSockex behaviour, the bare minimum to get started:</p>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">defmodule</span> <span class="no">AISStreamClient</span> <span class="k">do</span>
  <span class="kn">use</span> <span class="no">WebSockex</span>

  <span class="nv">@url</span> <span class="s2">"wss://stream.aisstream.io/v0/stream"</span> <span class="c1"># WebSocket URL</span>

  <span class="c1"># Start the WebSocket client</span>
  <span class="k">def</span> <span class="n">start_link</span><span class="p">(</span><span class="n">initial_state</span> <span class="p">\\</span> <span class="p">%{})</span> <span class="k">do</span>
    <span class="no">WebSockex</span><span class="o">.</span><span class="n">start_link</span><span class="p">(</span><span class="nv">@url</span><span class="p">,</span> <span class="bp">__MODULE__</span><span class="p">,</span> <span class="n">initial_state</span><span class="p">,</span> <span class="ss">name:</span> <span class="bp">__MODULE__</span><span class="p">)</span>
  <span class="k">end</span>

  <span class="c1"># Handle incoming messages</span>
  <span class="k">def</span> <span class="n">handle_frame</span><span class="p">({</span><span class="ss">:binary</span><span class="p">,</span> <span class="n">msg</span><span class="p">},</span> <span class="n">state</span><span class="p">)</span> <span class="k">do</span>
    <span class="k">case</span> <span class="no">Poison</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span> <span class="k">do</span>
      <span class="p">{</span><span class="ss">:ok</span><span class="p">,</span> <span class="n">decoded_msg</span><span class="p">}</span> <span class="o">-&gt;</span>
        <span class="no">IO</span><span class="o">.</span><span class="n">inspect</span><span class="p">(</span><span class="n">decoded_msg</span><span class="p">,</span> <span class="ss">label:</span> <span class="s2">"Decoded AIS Data"</span><span class="p">)</span>
        <span class="p">{</span><span class="ss">:ok</span><span class="p">,</span> <span class="n">state</span><span class="p">}</span>

      <span class="p">{</span><span class="ss">:error</span><span class="p">,</span> <span class="n">reason</span><span class="p">}</span> <span class="o">-&gt;</span>
        <span class="no">IO</span><span class="o">.</span><span class="n">puts</span><span class="p">(</span><span class="s2">"Failed to decode JSON: </span><span class="si">#{</span><span class="n">inspect</span><span class="p">(</span><span class="n">reason</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
        <span class="p">{</span><span class="ss">:ok</span><span class="p">,</span> <span class="n">state</span><span class="p">}</span>
    <span class="k">end</span>
  <span class="k">end</span>

  <span class="c1"># Handle other frames not covered by the above pattern matching</span>
  <span class="k">def</span> <span class="n">handle_frame</span><span class="p">(</span><span class="n">frame</span><span class="p">,</span> <span class="n">state</span><span class="p">)</span> <span class="k">do</span>
    <span class="no">IO</span><span class="o">.</span><span class="n">puts</span><span class="p">(</span><span class="s2">"Unhandled frame: </span><span class="si">#{</span><span class="n">inspect</span><span class="p">(</span><span class="n">frame</span><span class="p">)</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
    <span class="p">{</span><span class="ss">:ok</span><span class="p">,</span> <span class="n">state</span><span class="p">}</span>
  <span class="k">end</span>
<span class="k">end</span>
</code></pre></div></div>

<p><strong>A few notes:</strong></p>

<ol>
  <li><code class="language-plaintext highlighter-rouge">__MODULE__</code> is a compile-time macro that represents the name of the current module, in this case, <code class="language-plaintext highlighter-rouge">AISStreamClient</code>.</li>
  <li>I’m not using the <code class="language-plaintext highlighter-rouge">initial_state</code> argument in the call to <code class="language-plaintext highlighter-rouge">start_link</code>, or the changing the state before it’s returned from <code class="language-plaintext highlighter-rouge">handle_frame</code>, but this is the convention of managing state found in OTP behaviours. The state is a way to store information between messages—for example, connection settings or processed data. Even though I don’t update it in this example, following this pattern ensures your client can scale to handle more complex tasks later.</li>
</ol>

<h2 id="connecting-and-sending-the-subscription-key">Connecting and Sending the Subscription Key</h2>

<p>To authenticate with the AIS Stream WebSocket API, I needed a subscription key. To filter AIS data to a specific geographic area, the API requires a bounding box, defined by latitude and longitude coordinates. Here’s how I started the client and sent the key and bounding boxes:</p>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">defmodule</span> <span class="no">AISStreamApp</span> <span class="k">do</span>
  <span class="k">def</span> <span class="n">start</span> <span class="k">do</span>
    <span class="p">{</span><span class="ss">:ok</span><span class="p">,</span> <span class="n">pid</span><span class="p">}</span> <span class="o">=</span> <span class="no">AISStreamClient</span><span class="o">.</span><span class="n">start_link</span><span class="p">(%{})</span>

    <span class="n">data</span> <span class="o">=</span> <span class="p">%{</span>
      <span class="c1"># Replace with your actual API key from AISStream</span>
      <span class="ss">APIKey:</span> <span class="s2">"your_subscription_key"</span><span class="p">,</span>

      <span class="c1"># Bounding box covering the Detroit River</span>
      <span class="ss">BoundingBoxes:</span> <span class="p">[</span>
        <span class="p">[[</span><span class="mf">42.414173</span><span class="p">,</span> <span class="o">-</span><span class="mf">82.907625</span><span class="p">],</span> <span class="p">[</span><span class="mf">42.037176</span><span class="p">,</span> <span class="o">-</span><span class="mf">83.250223</span><span class="p">]]</span>
      <span class="p">]</span>
    <span class="p">}</span>

    <span class="c1"># Send the API key as JSON</span>
    <span class="no">WebSockex</span><span class="o">.</span><span class="n">send_frame</span><span class="p">(</span><span class="n">pid</span><span class="p">,</span> <span class="p">{</span><span class="ss">:text</span><span class="p">,</span> <span class="no">Poison</span><span class="o">.</span><span class="n">encode!</span><span class="p">(</span><span class="n">data</span><span class="p">)})</span>
  <span class="k">end</span>
<span class="k">end</span>
</code></pre></div></div>

<p>Run the application using:</p>

<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>iex <span class="nt">-S</span> mix
AISStreamApp.start<span class="o">()</span>
</code></pre></div></div>

<h2 id="processing-ais-data">Processing AIS Data</h2>

<p>I was able to successfully receive and process real-time AIS data. Here’s an example of the decoded JSON message:</p>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">Decoded</span> <span class="no">AIS</span> <span class="ss">Data:</span> <span class="p">%{</span>
  <span class="s2">"Message"</span> <span class="o">=&gt;</span> <span class="p">%{</span>
    <span class="s2">"PositionReport"</span> <span class="o">=&gt;</span> <span class="p">%{</span>
      <span class="s2">"Cog"</span> <span class="o">=&gt;</span> <span class="mi">91</span><span class="p">,</span>
      <span class="s2">"CommunicationState"</span> <span class="o">=&gt;</span> <span class="mi">59916</span><span class="p">,</span>
      <span class="s2">"Latitude"</span> <span class="o">=&gt;</span> <span class="mf">42.30840333333333</span><span class="p">,</span>
      <span class="s2">"Longitude"</span> <span class="o">=&gt;</span> <span class="o">-</span><span class="mf">83.083435</span><span class="p">,</span>
      <span class="s2">"MessageID"</span> <span class="o">=&gt;</span> <span class="mi">3</span><span class="p">,</span>
      <span class="s2">"NavigationalStatus"</span> <span class="o">=&gt;</span> <span class="mi">5</span><span class="p">,</span>
      <span class="s2">"PositionAccuracy"</span> <span class="o">=&gt;</span> <span class="no">true</span><span class="p">,</span>
      <span class="s2">"Raim"</span> <span class="o">=&gt;</span> <span class="no">false</span><span class="p">,</span>
      <span class="s2">"RateOfTurn"</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span>
      <span class="s2">"RepeatIndicator"</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span>
      <span class="s2">"Sog"</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span>
      <span class="s2">"Spare"</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span>
      <span class="s2">"SpecialManoeuvreIndicator"</span> <span class="o">=&gt;</span> <span class="mi">0</span><span class="p">,</span>
      <span class="s2">"Timestamp"</span> <span class="o">=&gt;</span> <span class="mi">42</span><span class="p">,</span>
      <span class="s2">"TrueHeading"</span> <span class="o">=&gt;</span> <span class="mi">43</span><span class="p">,</span>
      <span class="s2">"UserID"</span> <span class="o">=&gt;</span> <span class="mi">538006783</span><span class="p">,</span>
      <span class="s2">"Valid"</span> <span class="o">=&gt;</span> <span class="no">true</span>
    <span class="p">}</span>
  <span class="p">},</span>
  <span class="s2">"MessageType"</span> <span class="o">=&gt;</span> <span class="s2">"PositionReport"</span><span class="p">,</span>
  <span class="s2">"MetaData"</span> <span class="o">=&gt;</span> <span class="p">%{</span>
    <span class="s2">"MMSI"</span> <span class="o">=&gt;</span> <span class="mi">538006783</span><span class="p">,</span>
    <span class="s2">"MMSI_String"</span> <span class="o">=&gt;</span> <span class="mi">538006783</span><span class="p">,</span>
    <span class="s2">"ShipName"</span> <span class="o">=&gt;</span> <span class="s2">"FEDERAL OSHIMA      "</span><span class="p">,</span>
    <span class="s2">"latitude"</span> <span class="o">=&gt;</span> <span class="mf">42.30840333333333</span><span class="p">,</span>
    <span class="s2">"longitude"</span> <span class="o">=&gt;</span> <span class="o">-</span><span class="mf">83.083435</span><span class="p">,</span>
    <span class="s2">"time_utc"</span> <span class="o">=&gt;</span> <span class="s2">"2024-12-09 02:27:43.237370229 +0000 UTC"</span>
  <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>

<p>This is great, and exactly what I wanted. However, I was receiving data intermittently; on some days I would receive information and on others I would not. I wonder if this is due to the intermittent running of the AIS radio receivers. (e.g., Maybe it was turned off?)</p>

<p>Oh well! I’ll keep an eye on it, and hopefully it will work out for me. I’ll try another API next time!</p>

<h2 id="conclusion">Conclusion</h2>

<p>While I’m not certain that the API will work for my specific needs, building a WebSocket client in Elixir to consume real-time ship data was a great learning experience. If you’re working with WebSockets in Elixir, libraries like websockex and poison make the process straightforward.</p>

<p>You can check out <a href="https://aisstream.io/documentation">AIS Stream’s documentation</a> to explore their API further. Let me know if you have any questions or tips for improving WebSocket clients in Elixir!</p>

<p class="notice--info"><strong>Note:</strong> This is just the beginning! In future posts, I’ll explore other AIS APIs and ways to integrate this data into a custom physical display. Stay tuned.</p>]]></content><author><name>Paul Fedory</name></author><category term="software" /><category term="elixir" /><category term="websocket" /><category term="intermediate" /><category term="ais" /><summary type="html"><![CDATA[Learn how to build a real-time WebSocket client in Elixir to track live ship data using the AIS Stream API.]]></summary></entry><entry><title type="html">Generating a random colour in Elixir</title><link href="https://paulfedory.com/software/generate-random-colour/" rel="alternate" type="text/html" title="Generating a random colour in Elixir" /><published>2024-11-13T00:00:00-05:00</published><updated>2024-11-13T00:00:00-05:00</updated><id>https://paulfedory.com/software/generate-random-colour</id><content type="html" xml:base="https://paulfedory.com/software/generate-random-colour/"><![CDATA[<p>I’ve been working on a project using Phoenix LiveView, which serves up server-rendered HTML over websockets.
I needed a way to generate a random RGB colour on the backend, so I wrote this function that seems to do the job.</p>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">def</span> <span class="n">generate_random_colour</span> <span class="k">do</span>
  <span class="ss">:rand</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">16_777_216</span><span class="p">)</span>
  <span class="o">|&gt;</span> <span class="no">Kernel</span><span class="o">.-</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
  <span class="o">|&gt;</span> <span class="no">Integer</span><span class="o">.</span><span class="n">to_string</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span>
  <span class="o">|&gt;</span> <span class="no">String</span><span class="o">.</span><span class="n">pad_leading</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s2">"0"</span><span class="p">)</span>
  <span class="o">|&gt;</span> <span class="p">(</span><span class="k">fn</span> <span class="n">s</span> <span class="o">-&gt;</span> <span class="s2">"#</span><span class="si">#{</span><span class="n">s</span><span class="si">}</span><span class="s2">"</span> <span class="k">end</span><span class="p">)</span><span class="o">.</span><span class="p">()</span>
<span class="k">end</span>
</code></pre></div></div>

<h2 id="lets-break-it-down">Let’s Break it down!</h2>

<h4 id="1-generate-a-random-number">1. Generate a random number</h4>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="ss">:rand</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">16_777_216</span><span class="p">)</span>
  <span class="o">|&gt;</span> <span class="no">Kernel</span><span class="o">.-</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</code></pre></div></div>

<p><a href="https://www.erlang.org/doc/apps/stdlib/rand.html#uniform/1">This erlang function</a> generates a random number between 1 and 16,777,216, representing all possible colours in the standard RGB colour space. By subtracting 1, we include 0 in the range, which corresponds to the lowest CSS colour, <code class="language-plaintext highlighter-rouge">#000000</code>. The highest number, 16,777,215, gives us <code class="language-plaintext highlighter-rouge">#FFFFFF</code>.</p>

<h4 id="2-convert-to-hexadecimal">2. Convert to Hexadecimal</h4>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="o">|&gt;</span> <span class="no">Integer</span><span class="o">.</span><span class="n">to_string</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span>
</code></pre></div></div>

<p>It converts the random integer into a hexadecimal string, which is used by HTML and CSS for colours.</p>

<h4 id="3-pad-with-leading-zeros">3. Pad with leading zeros</h4>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="o">|&gt;</span> <span class="no">String</span><span class="o">.</span><span class="n">pad_leading</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="s2">"0"</span><span class="p">)</span>
</code></pre></div></div>

<p>This ensures the string is at least 6 characters and pads the string with leading zeros until it is. For example, suppose the random number generator produced the number 3. After subtracting one, and converting to hexadecimal - the string so far would be “2” and not “000002” - which is what we need for use with HTML and CSS.</p>

<h4 id="4-add-a-hash-">4. Add a hash (#)</h4>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code>  <span class="o">|&gt;</span> <span class="p">(</span><span class="k">fn</span> <span class="n">s</span> <span class="o">-&gt;</span> <span class="s2">"#</span><span class="si">#{</span><span class="n">s</span><span class="si">}</span><span class="s2">"</span> <span class="k">end</span><span class="p">)</span><span class="o">.</span><span class="p">()</span>
</code></pre></div></div>

<p>Finally, this anonymous function prepends the random colour with a “#” which makes it usable in HTML and CSS.</p>

<h4 id="example-output">Example output:</h4>

<div class="language-elixir highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">iex</span><span class="o">&gt;</span> <span class="no">MyModule</span><span class="o">.</span><span class="n">generate_random_colour</span><span class="p">()</span>
<span class="s2">"#A3F1C7"</span>
</code></pre></div></div>

<p>I hope this is useful! For more fun, experiment with these variations:</p>

<ul>
  <li>Return the RGB values as a tuple: {63, 162, 188}.</li>
  <li>Generate only light or dark colours.</li>
</ul>

<p>Happy coding!</p>]]></content><author><name>Paul Fedory</name></author><category term="software" /><category term="elixir" /><category term="phoenix" /><category term="liveview" /><category term="beginner" /><summary type="html"><![CDATA[I’ve been working on a project using Phoenix LiveView, which serves up server-rendered HTML over websockets. I needed a way to generate a random RGB colour on the backend, so I wrote this function that seems to do the job.]]></summary></entry><entry><title type="html">New blog, who dis?</title><link href="https://paulfedory.com/software/new-blog-who-dis/" rel="alternate" type="text/html" title="New blog, who dis?" /><published>2024-10-31T00:00:00-04:00</published><updated>2024-10-31T00:00:00-04:00</updated><id>https://paulfedory.com/software/new-blog-who-dis</id><content type="html" xml:base="https://paulfedory.com/software/new-blog-who-dis/"><![CDATA[<p>Hello, world.</p>

<p>It’s been a while since I’ve kept up a blog. In the past, I’ve hosted blogs on various platforms, kept separate from any personal website. It’s time to self-host and bring it all under one roof.</p>

<h4 id="reasons-i-havent-written-much">Reasons I haven’t written much</h4>

<p>I cringe when I look back on old blog posts, and that’s a reason why I haven’t been more profilic. I’m not even <em>that</em> active on social media anymore (even when it’s only close friends and family). Whenever an old post, or “memory” pops up in my feed, I become so embarrassed. <em>Why did I feel the need to share that?</em></p>

<p>Another reason: imposter syndrome. I can’t help but feel like any blog post I write won’t be interesting, useful or insightful to anyone else. My inner saboteur always has me thinking: <em>“This information is obvious to me, and so it must be to everyone else. Your thoughts on this are <strong>not</strong> worth sharing.”</em></p>

<p>But self-effacement isn’t useful to anyone, nor is it a good look.</p>

<h4 id="a-new-leaf">A new leaf</h4>

<p>Recently I’ve taken some time off to evaluate my career and how I can shake it up a little. I love my career, and I’m interested in exploring what new directions I can take it to. I’m hoping one of those directions is being more public with my career: more writing, more public speaking, more events. Building a public persona.</p>

<p>I want to overcome those reasons above and write more blog posts. So, here it goes!</p>]]></content><author><name>Paul Fedory</name></author><category term="software" /><category term="blogging" /><category term="writing" /><summary type="html"><![CDATA[Hello, world.]]></summary></entry></feed>