<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
 <title>Journal</title>
 <link>journal/diary.html</link>
 <description>Journal</description>
 <pubDate>Tue, 11 Nov 2025 02:56:59 +0000</pubDate>
 <atom:link href="rss.xml" rel="self" type="application/rss+xml" />
 <item>
  <title>2025-04-24</title>
  <link>journal/2025-04-24.html</link>
  <guid isPermaLink="false">2025-04-24</guid>
  <description><![CDATA[
<p>
A depth-first tree iterator
</p>
<p>

</p>
<p>
The iterator pattern is nice because it gives the caller a simple flat-looking looping mechanism to traverse data entries. Consider the following pseudocode for traversing a tree depth-first.
</p>
<p>

</p>
<pre>
const tree = {
  id: 1,
  left: {
    id: 2,
    left: { id: 3, left: null, right: null }
    right: null,
  },
  right: {
    id: 4,
    left: {
      id: 5,
      left: null,
      right: { id: 6, left: null, right: null },
    },
    right: {
      id: 7,
      left: null,
      right: { id: 8, left: null, right: null },
    }
  }
};

traverse(tree, onEmit);

function onEmit(node) {
  console.log(node.id);
}

function traverse(node, onEmit) {
  if (node == null) return;
  
  onEmit(node);
  
  if (node.left) traverse(node.left, onEmit);
  if (node.right) traverse(node.right, onEmit);
}
</pre>
<p>

</p>
<p>
The program above takes a recursive approach to traversing a tree structure. While this may not be ideal in situations with very large tree structures, the program would print the following output when run:
</p>
<p>

</p>
<pre>
1
2
3
4
5
6
7
8
</pre>
<p>

</p>
<p>
Consider instead a breadth-first approach to traversing a tree:
</p>
<p>

</p>
<pre>

function traverse(node, onEmit) {
  if (node == null) return;
  
  emit(node, onEmit);
  descend(node, onEmit);
}

function emit(node, onEmit) {
  if (node) onEmit(node);
  if (node.left) onEmit(node.left);
  if (node.right) onEmit(node.right);
}

function descend(node, onEmit) {
  traverse(node.left, onEmit);
  traverse(node.right, onEmit);
}
</pre>
<p>

</p>
<p>
Note that I actually think the logic above is wrong.
</p>
<p>

</p>
<p>
---
</p>
<p>

</p>
<p>
Let's take the depth-first example and turn it into an iterator.
</p>
<p>

</p>
<p>
An iterator is an object with a <code>next</code> method that will return the following node in the tree. Once there are no more nodes, the next method will return null.
</p>
<p>

</p>
<p>
Let's look at the depth-first example again.
</p>
<p>

</p>
<pre>
function onEmit(node) {
  console.log(node.id);
}

function traverse(node, onEmit) {
  if (node == null) return;
  
  onEmit(node);
  
  if (node.left) traverse(node.left, onEmit);
  if (node.right) traverse(node.right, onEmit);
}
</pre>
<p>

</p>
<p>
Let's try to first model the recursion in an iterative manner.
</p>
<p>

</p>
<pre>
function traverse(node, onEmit) {
  if (node == null) return;
  
  onEmit(node);
  
  // This should be represented by some data structure
  if (node.left) traverse(node.left, onEmit);
  // This should be represented by some data structure
  if (node.right) traverse(node.right, onEmit);
}
</pre>
<p>

</p>
<p>
Let's think of the steps that are executed and see how we can decompose this recursive function into an iterative one.
</p>
<p>

</p>
<p>
Here's our example tree again:
</p>
<p>

</p>
<p>

</p>
<pre>
const tree = {
  id: 1,
  left: {
    id: 2,
    left: { id: 3, left: null, right: null }
    right: null,
  },
  right: {
    id: 4,
    left: {
      id: 5,
      left: null,
      right: { id: 6, left: null, right: null },
    },
    right: {
      id: 7,
      left: null,
      right: { id: 8, left: null, right: null },
    }
  }
};
</pre>
<p>

</p>
<ul>
<li>
Call traverse(tree, onEmit)

<li>
node = tree

<li>
Check if node is null - it's not, so continue

<li>
emit node (1)

<li>
Check if node.left is null - it's not, so let's traverse

<li>
prev = node

<li>
node = node.left

<li>
check if node.left is null - it's not, so let's traverse **

</ul>
<p>
** At this point, we realize that we're checking if node.left and node.right are null before we traverse. Then, we check again at the start of traverse. Let's just do it at the start of traverse only.
</p>
<p>

</p>
<pre>
function traverse(node, onEmit) {
  if (node == null) return;
  
  onEmit(node);
  
  traverse(node.left, onEmit);
  traverse(node.right, onEmit);
}
</pre>
<p>

</p>
<p>
Let's start again.
</p>
<p>

</p>
<ul>
<li>
Call traverse(tree, onEmit)

<li>
node = tree(1)

<li>
Check if node is null - it's not, so continue

<li>
emit node (1)

<li>
Check if node(1).left is null - it's not, so let's traverse

<li>
prev = node(1)

<li>
node = node(1).left(2)

<li>
Check if node is null - it's not, so continue

<li>
emit node (2)

<li>
prev2 = node(2)

<li>
node = node(2).left(3)

<li>
Check if node is null - it's not, so continue

<li>
emit node (3)

<li>
prev3 = node(3)

<li>
node = node(3).left(null)

<li>
Check if node is null - It is, so skip this

<li>
node = prev3(3)

<li>
node = node(3).right(null)

<li>
Check if node is null - It is, so skip this

<li>
node = prev3(3)

<li>
We've checked both prev3 left and right, so go back higher

<li>
prev3 = empty

<li>
node = prev2(2)

<li>
node = node(2).right(null)

<li>
Check if node is null - it is, so skip this

<li>
node = prev2(2)

<li>
We've checked both prev2 left and right, so go back

<li>
prev2 = empty

<li>
node = prev(1)

<li>
node = node(1).right(4)

<li>


</ul>
<p>
---
</p>
<p>

</p>
<p>
I think we need to come up with a max 
</p>
]]></description>
  <pubDate>2025-11-11</pubDate>
 </item>
 <item>
  <title>Musings on a templating language</title>
  <link>journal/2025-04-22.html</link>
  <guid isPermaLink="false">2025-04-22</guid>
  <description><![CDATA[
<div id="Musings on a templating language"><h1 id="Musings on a templating language" class="header"><a href="#Musings on a templating language">Musings on a templating language</a></h1></div>
<p>

</p>
<p>
A templating language can be boiled down to glorified string replacement.
</p>
<p>

</p>
<p>
Consider a fictional company called Widget Corporation. They operate an online store and have an email system in place where they can contact their users to encourage purchases. They would like to remind customers who add items to their cart but don't complete the payment process to continue the flow and result in a purchase. They believe that a personalized email increases conversion rates. Here's an email that they send out to a fictional customer named Bob.
</p>
<p>

</p>
<blockquote>
<p>
 Hello, Bob!
</p>
<p>

</p>
<p>
 You added 2 items to your cart. Still need time to think?
</p>
<p>
 
</p>
<p>
 Let us help with that.
</p>
<p>
 
</p>
<p>
 For the next 24 hours only, use coupon code BOB&lt;3WIDGET10 to get 10% off your order (up to $50).
</p>
<p>
 
</p>
<p>
 Visit the web store and act now -&gt;
</p>
<p>
 
</p>
<p>
 Don't miss out on this exclusive deal!
</p>
<p>
 
</p>
<p>
 
</p>
<p>
 Widget Corporation Loyalty Team
</p>
</blockquote>
<p>

</p>
<p>
The company has an employee diligently inspecting abandoned carts in the admin dashboard. When they find one, they inspect available offers, draft an email including the details, and send it onto the customer. To save time, they have a document with all of the boilerplate so they just need to replace specific sections with the customer's name, their cart deals, and the coupon data. Here's what that looks like:
</p>
<p>

</p>
<blockquote>
<p>
 Hello, {customer_name}!
</p>
<p>
 
</p>
<p>
 You added {cart_num_items} items to your cart. Still need time to think?
</p>
<p>
 
</p>
<p>
 Let us help with that.
</p>
<p>
 
</p>
<p>
 {unique_offer}
</p>
<p>
 
</p>
<p>
 Visit the web store and act now -&gt;
</p>
<p>
 
</p>
<p>
 Don't miss out on this exclusive deal!
</p>
<p>
 
</p>
<p>
 
</p>
<p>
 Widget Corporation Loyalty Team
</p>
</blockquote>
<p>

</p>
<p>

</p>
]]></description>
  <pubDate>2025-11-11</pubDate>
 </item>
 <item>
  <title>Structure of a Zig program</title>
  <link>journal/2025-04-16.html</link>
  <guid isPermaLink="false">2025-04-16</guid>
  <description><![CDATA[
<p>

</p>
<div id="Structure of a Zig program"><h1 id="Structure of a Zig program" class="header"><a href="#Structure of a Zig program">Structure of a Zig program</a></h1></div>
<p>

</p>
<p>
Here's some beginner-friendly advice on how to structure your Zig program to keep it readable and maintainable.
</p>
<p>

</p>
<div id="Structure of a Zig program-Entrypoint"><h2 id="Entrypoint" class="header"><a href="#Structure of a Zig program-Entrypoint">Entrypoint</a></h2></div>
<p>

</p>
<pre>
pub fn main() !void {
  const stdout = std.io.getStdOut().writer();
  
  try stdout.print("Hello, world!\n", .{});
}

const std = @import("std");
</pre>
<p>

</p>
<p>
The main function of a program is the entrypoint. The code section above illustrates some minimal "Hello world" program, which prints the text "Hello, world!" to the console and then exits.
</p>
<p>

</p>
<p>
In Zig, your entrypoint will be responsible for bootstrapping your entire program. It should set up a program-wide allocator, parse command line arguments, and read environment configuration. Then, it should invoke your "real" program logic while providing it this information.
</p>
<p>

</p>
<p>
In pseudocode, your main function will look like the following.
</p>
<p>

</p>
<pre>
main() {
  const allocator = setupAllocator();
  const cli_args = parseCliArgs();
  const env_data = queryEnvData();
  
  const config = .{ cli_args, env_data };
  
  runProgram(allocator, config);
}
</pre>
<p>

</p>
<p>
To set up the allocator, use the standard library's <code>DebugAllocator</code>.
</p>
<p>

</p>
<pre>
var gpa: DebugAllocator(.{}) = .init;
defer _ = gpa.deinit();
const allocator = gpa.allocator();
</pre>
<p>

</p>
<p>
Then, to parse cli args, use the standard library's <code>ArgIterator</code>.
</p>
<p>

</p>
<pre>
var args = try std.process.argsWithAllocator(allocator);
defer args.deinit();

// skip exe name
_ = args.next();

// todo: actual arg parsing logic...
</pre>
<p>

</p>
<p>
Then, to query env data, use <code>getEnvVarOwned</code>.
</p>
<p>

</p>
<pre>
const host = try std.process.getEnvVarOwned(allocator, "APP_HOST");
// ...
</pre>
<p>

</p>
]]></description>
  <pubDate>2025-11-11</pubDate>
 </item>
 <item>
  <title>Zig target triples</title>
  <link>journal/2025-04-14.html</link>
  <guid isPermaLink="false">2025-04-14</guid>
  <description><![CDATA[
<p>
% Zig target triples
</p>
<p>

</p>
<div id="Zig target triples"><h1 id="Zig target triples" class="header"><a href="#Zig target triples">Zig target triples</a></h1></div>
<p>

</p>
<p>
Here's how you can find all the target triples that your version of Zig supports.
</p>
<p>

</p>
<blockquote>
<p>
 Note that target triples will become target quadruples in an upcoming enhancement to the compiler. Follow the GitHub issue, [RFC/Proposal: Turning Zig target triples into quadruples](<a href="https://github.com/ziglang/zig/issues/20690).">https://github.com/ziglang/zig/issues/20690).</a>
</p>
</p></blockquote>
]]></description>
  <pubDate>2025-11-11</pubDate>
 </item>
 <item>
  <title>To Help You Think</title>
  <link>journal/2025-04-13.html</link>
  <guid isPermaLink="false">2025-04-13</guid>
  <description><![CDATA[
<p>
I am relatively easy to predict given my habits, routines, and modes but I can't seem to overcome the compounding effects of my (in)decisions. I must examine a snapshot, a slice over the course of weeks, for clues on how I might be able to determine the trajectory of my life as-so-far as I can influence it.
</p>
<p>

</p>
<p>
Stories...
</p>
<p>

</p>
<div id="To Help You Think"><h1 id="To Help You Think" class="header"><a href="#To Help You Think">To Help You Think</a></h1></div>
<p>

</p>
<p>
Inside a building in the neighbourhood of Coti at sunrise, Cody opened their eyes to a splitting headache. They spent the morning together, pushing against each other between modes of action. Cody needed to get to work on time because they'd been late a fair number of times recently. No one's said anything, but they want to get ahead of the issue and leave it in the past as transient, silent observation. Before long it'd grow to rumour then threat then disciplinary action. Not that Cody needed the stick, it's just that the thought of the whole situation gave them anxiety. So they forgot about it over drinks with friends. Clearly it didn't want to be forgotten and manifested itself as this headache, growing from subtle nag to dull throb.
</p>
<p>

</p>
<p>
Inside a building in the neighbourhood of Coti at sunrise, I opened my eyes to a splitting headache. We spent the morning together, pushing against each other between modes of action. I needed to get to work on time because I'd been late a fair number of times recently. No one's said anything yet, but I want to get ahead of the issue and leave it in the past as transient, silent observation. Unchecked, it'd grow to rumour then threat then disciplinary action. Not that I need the stick, it's just the thought of the whole situation fills my mental pool with a haze. So I forgot about it over drinks with friends. Clearly it didn't want to be forgotten, however, and manifested itself as this headache, growing from subtle nag to dull throb.
</p>
<p>

</p>
<p>
Sunrise -&gt; Neighbourhood -&gt; Building
</p>
<p>
Sunrise -&gt; Cody
</p>
<p>
Headache -&gt; Cody
</p>
<p>
Cody -&gt; Headache
</p>
<p>
Headache -&gt; Inaction
</p>
<p>
Cody -&gt; Action
</p>
<p>
Cody -&gt; Late -&gt; Past
</p>
<p>
Cody -&gt; On time -&gt; Future
</p>
<p>
...
</p>
]]></description>
  <pubDate>2025-11-11</pubDate>
 </item>
 <item>
  <title>The limits of the tools we create</title>
  <link>journal/2025-04-08.html</link>
  <guid isPermaLink="false">2025-04-08</guid>
  <description><![CDATA[
<p>

</p>
<div id="The limits of the tools we create"><h1 id="The limits of the tools we create" class="header"><a href="#The limits of the tools we create">The limits of the tools we create</a></h1></div>
<p>

</p>
<p>
A software designer is burdened with the task of creating divisions in functionality between projects. If you were to make kitchen implements, you wouldn't make a bowl that is also a spoon. Each implement has its own function. However, if you were to attach a handle to the edge of a bowl, is it now a spoon? Then, if you were to scale up the spoon until the round end of the utensil is as big as a bowl, how different are the two? In fact, the round end of the spoon is called a bowl. It seems as if it's a matter of scale or perspective which informs us to make distinctions between the physical tools in our lives. How can we apply similar principles to software design to allow us to make informed divisions with respect to the software tools that we create?
</p>
]]></description>
  <pubDate>2025-04-08</pubDate>
 </item>
 <item>
  <title>2025-04-01</title>
  <link>journal/2025-04-01.html</link>
  <guid isPermaLink="false">2025-04-01</guid>
  <description><![CDATA[
<p>

</p>
<p>
I'm building a static site generator with JS component support, all in a single binary. It uses markdown for content, mustache for templating, and an embedded Javascript runtime to render components, which are essentially templating partials which can emit CSS and JS for dynamic content
</p>
]]></description>
  <pubDate>2025-04-01</pubDate>
 </item>
 <item>
  <title>Learn Golang</title>
  <link>journal/2025-03-25.html</link>
  <guid isPermaLink="false">2025-03-25</guid>
  <description><![CDATA[
<p>

</p>
<div id="Learn Golang"><h1 id="Learn Golang" class="header"><a href="#Learn Golang">Learn Golang</a></h1></div>
<p>

</p>
<p>
On my recent job search I've come across quite a few postings with Go as their primary programming language. I believe that I can learn it on the job but I would rather give potential employers more confidence when it comes to my knowledge of Go. Also, I've always been interested in Go but never required to write it.
</p>
<p>

</p>
<p>
These are some of my jots of note when it comes to learning Go. This list is only beginning. I'll add here over time as I go along.
</p>
<p>

</p>
<div id="Learn Golang-Notes"><h2 id="Notes" class="header"><a href="#Learn Golang-Notes">Notes</a></h2></div>
<p>

</p>
<ul>
<li>
Immediately useful commands of note are <code>go mod init</code>, <code>go run</code>, <code>go help</code>, and <code>go mod tidy</code>

<li>
Help documentation for any command is available with <code>go help</code>

<li>
You can search for Go modules at <a href="https://pkg.go.dev">https://pkg.go.dev</a>

<li>
Go discovers module requirements depending on usage in source code when you run <code>go mod tidy</code>

<li>
In addition to fetching required modules and updating go.mod, the <code>mod tidy</code> command will also remove any unused modules from go.mod

<li>
Alternatively, you can use <code>go get</code> to fetch a particular module, but this should be used primarily for upgrading, downgrading, or removing existing modules

<li>
If you do hand-modify your go.mod file, you can use <code>go mod edit -fmt</code> to ensure it has the proper formatting

<li>
Go is opinionated when it comes to formatting, so there are specific conventions to follow

<li>
By convention, "Exported names" are those beginning with a capital letter, so a function named <code>Hello</code> is exported (i.e. callable by external packages) while a function named <code>hello</code> is not

<li>
When the <code>:=</code> operator is used to declare and initialize a variable, its type is infered from the value on the right-hand side

<li>
Go distinguishes a package as a "main package" if it contains a Go source file with <code>package main</code> at the start of the file

<li>
You can use <code>go mod edit</code> to add a <code>replace</code> in your <code>go.mod</code> to point dependencies to packages on the local filesystem (this action should usually be followed by <code>go mod tidy</code>)

<li>
You can use Go's Multiple return values feature to implement error handling

<li>
The standard library's <code>log.Fatal</code> "is equivalent to Print followed by a call to <code>os.Exit(1)</code>" (see <a href="https://pkg.go.dev/log#Fatal">https://pkg.go.dev/log#Fatal</a>)

</ul>
<div id="Learn Golang-Bonus"><h2 id="Bonus" class="header"><a href="#Learn Golang-Bonus">Bonus</a></h2></div>
<p>

</p>
<p>
The site <a href="https://pkg.go.dev">https://pkg.go.dev</a> has no API to search packages, but you can still achieve command-line package search by scraping the results from the search page.
</p>
<p>

</p>
<p>
Along with <code>curl</code> and tools from <code>html-xml-utils</code>, you can look up packages on the Go packages website:
</p>
<p>

</p>
<pre>
read -p 'Search go packages: ' query &amp;&amp; curl --get --data-urlencode "q=$query" "https://pkg.go.dev/search" | hxnormalize -x | hxselect -c -s '\n' '.SearchSnippet-synopsis, .SearchSnippet-header-path'
</pre>
<p>

</p>
<p>
Alternatively, you can add this bash function to your profile and invoke it like <code>gopkg &lt;query&gt;</code>:
</p>
<p>

</p>
<pre>
function gopkg {
  if [[ $# -ne 1 ]]; then
    echo "Usage: gopkg &lt;query&gt;"
    return 1
  fi
  
  curl --get --data-urlencode "q=$1" "https://pkg.go.dev/search" | hxnormalize -x | hxselect -c -s '\n' '.SearchSnippet-synopsis, .SearchSnippet-header-path'
}
</pre>
]]></description>
  <pubDate>2025-03-25</pubDate>
 </item>
 <item>
  <title>What do you think is important to know?</title>
  <link>journal/2025-03-22.html</link>
  <guid isPermaLink="false">2025-03-22</guid>
  <description><![CDATA[
<p>

</p>
<div id="What do you think is important to know?"><h1 id="What do you think is important to know?" class="header"><a href="#What do you think is important to know?">What do you think is important to know?</a></h1></div>
<p>

</p>
<p>
While out suit shopping, a clerk helped us choose ties to match our suits. He provided excellent colour combination suggestions. I complimented him and asked him what his approach was. He told me he was simply choosing complementary colours, like on the colour wheel. It's important for his line of work (selling suits) to have expertise in at least basic colour theory in order to create objectively good colour combinations on the fly.
</p>
<p>

</p>
<p>
How about you? In your line of work, what do you think is important to know?
</p>
]]></description>
  <pubDate>2025-03-22</pubDate>
 </item>
 <item>
  <title>Graceful degradation in web development</title>
  <link>journal/2025-03-19.html</link>
  <guid isPermaLink="false">2025-03-19</guid>
  <description><![CDATA[
<p>

</p>
<div id="Graceful degradation in web development"><h1 id="Graceful degradation in web development" class="header"><a href="#Graceful degradation in web development">Graceful degradation in web development</a></h1></div>
<p>

</p>
<p>
In a blog post by Jim Nielson titled <a href="https://blog.jim-nielsen.com/2025/lots-of-little-html-pages/">Building Websites with Lots of Little HTML Pages</a>, the author lists a few techniques where a developer might be able to use CSS page transitions and HTML pages over JS client-side operations. This isn't 100% true - there's still some JavaScript, but there are still some key takeaways.
</p>
<p>

</p>
<p>
In the first example, of allowing the user to switch between Latest, Trending, and Hacker News Hits filters for his blog posts, he opts for generating static HTML pages for each of these. It's a fairly trivial approach, but he makes use of CSS page transitions to make it feel like client-side navigation (on supported browsers), which is pretty interesting.
</p>
<p>

</p>
<p>
The second example was about how to handle the opening and closing of the nav menu via a hamburger icon. Traditionally, there would be some chunk of HTML inside the document that's hidden on the page or synthesized via JS that would become visible when the user clicks on the open icon or invisible once the user clicks on the close icon. A JS strategy that's fairly common is to add/remove a class name for this hidden menu and let the CSS styles do the work. A no-JS strategy that I've also seen (and employed) is by employing the <a href="https://css-tricks.com/the-checkbox-hack/">Checkbox Hack</a>, where you use an html <code>label</code>, <code>input[type="checkbox"]</code>, and the CSS <code>:checked</code> property combined with the sibling selector to hide/show the mobile menu when the checkbox changes state. It's a neat trick and lets you achieve that client-side interaction without JS. The approach in this blog post is neither of those and is more similar to something like HTMX, where the user action would instead request an HTML blob that gets injected into the page. In this blog post, the user's click instead initiates a document request like normal and presents to the user a page which contains the mobile navigation. It's pretty simple in concept and is also easy to understand.
</p>
<p>

</p>
<p>
The neat trick about the HTML page nav menu is that the close button on this page uses a bit of JS to improve the user experience. This is the opening tag:
</p>
<p>

</p>
<pre>
&lt;a href="/" onclick="return document.referrer?history.back():window.location.href=&amp;quot;/&amp;quot;,!1" aria-label="Close menu (back)"&gt;...&lt;/a&gt;
</pre>
<p>

</p>
<p>
Notice the <code>onclick</code> attribute which makes this link act like the browser's back button if there's any history, but otherwise sends them back to the home page. If JS isn't enabled, it just acts like a normal link. The beauty of this is that a backwards page navigation doesn't perform another HTTP request, so although it still technically uses JS, it lets the browser take care of the job of presenting the previous page, rather than putting that task on whatever client-side solution is present (even though toggling a class on a hidden element is already pretty simple to maintain). To me, this is the perfect example of "graceful degradation" and it's the kind of web magic that makes you feel like elegant solutions are right there waiting to be discovered.
</p>
<p>

</p>
<p>

</p>
]]></description>
  <pubDate>2025-03-19</pubDate>
 </item>
</channel>
</rss>
