Apportioning refunds and discounts

This is a recounting of one of the more diabolical problems I’ve worked on over the years - apportioning discounts and refunds against orders. Let’s assume for the sake of this discussion we have two tables in a relational database, orders and order_products. To understand unit economics, every business I can imagine would want to view net revenue, which is to say price - discounts, at a line level. Line-level financials allow analyses like “how do discounts compare across different cohorts of customers?

Issues you might encounter setting up a Hugo Blog on Gitlab

I recently made two changes with this blog: Switched hosts from Github to Gitlab Replaced Jekyll with Hugo as my static site generator Hugo is a well documented project and Gitlab’s documentation is quite thorough, but I ran into a few issues of note while relaunching this blog and thought it worth documenting them here to hopefully clear the way for you to more easily set up your own blog.

Bringing sanity to ActiveRecord validations

ActiveRecord uniqueness validations cannot be used on their own - we must use database level constraints to guard against race conditions. Given that we are guarding against duplication at the database level, should we also introduce uniqueness constraints in ActiveRecord models, and if so, under what circumstances? All of the examples use the locations table. The code column in this table has a unique index. Let’s start off by adding the following ActiveRecord validation to our model:

Fun with Ruby hash defaults

I recently reviewed a pull request wherein a coworker initialized a Hash to return an array as a default value and appended items to the arrays returned by hash keys as the program ran. When appending items to a Ruby array, using the shovel operator is usually preferred over plus-equals. The shovel operator is orders of magnitude faster because it appends a value to an existing array whereas plus-equals creates a new array every time (explored in more detail in this post by Alec Jacobson).

The Baconator: Finding Erdős Numbers on Wikipedia

The Erdős number describes the collaborative distance between an author of mathematical papers and Paul Erdős. The Bacon number, which measures an actor’s degress of separation from Kevin Bacon in cinema, is a well known varition on this theme. I recently came across another interesting variation - what is the shortest possible path between any two articles on Wikipedia? Finding the shortest path between two nodes suggests a breadth-first search. Given any starting page and a target page, our search should look something like this:

Building a Work Queue With Redis and Lua

I recently encountered a situation that called for a work queue. The system seemed simple enough at the outset - only producers and consumers, with jobs prioritized by when they are enqueued. There is, however, a curious twist - the consumers are people doing work in a single page app. There are enough concurrent users to warrant a datastore with atomic operations. Jobs cannot simply be dequeued when people start them because many jobs go unfinished for one reason or another.

Building Knod Part 2: Workflow and Testing

This is part 2 in a series about building Knod, a tiny HTTP server designed to help with front-end prototyping. One of the first challenges I faced in building Knod was establishing a good workflow. Using the REPL as a development tool is one of the great joys of using Ruby and languages in the LISP family. I tend to experiment with different things on the REPL when the domain isn’t well known and I want to spike on a solution - the exact circumstance faced at the outset of this project.

Walking The Path: Notes on a Ten Year Journey From Stuttering to Fluency

While the lighting in the photo could use some work, you can still observe that I am: In front of a room giving a presentation Enjoying myself I gave this presentation as part of an ongoing lunch and learn series at thredUP and it went better than I could have hoped. The crowd was very much engaged and I was still fielding follow up questions the next day. A 15 minute presentation in front of ~40 people is barely a blip on the radar for some of my peers, but for me, it is a milestone at least a decade in the making.

Building Knod, a tiny HTTP server. Part 1: Introduction

While working through Facebook’s excellent React tutorial, I came across a common problem when developing and testing AJAX applications: Having some sort of rudimentary backend is necessary, or at the very least makes things easier. While options for http static servers are many, these only respond to GET requests and hence only get you part of the way there. The ideal solution would also respond to PUT, POST, and DELETE and would work with almost zero configuration.

Juxtaposition in Ruby

4Clojure recently introduced me to Clojure’s curious juxt function. From the official docs: Takes a set of functions and returns a function that is the juxtaposition of those functions. Makes a bit more sense once you see it in practice: ((juxt + max min) 2 3 5 1 6 4) ; => [21 6 1] This alone is pretty neat, but it gets better. Jay Fields observed that juxt has other interesting applications: