Technical blog exploring TypeScript, React, JavaScript, and computer science fundamentals. In-depth articles on software development, testing, and mathematical concepts.
# What is a barcode encoding? The black and white stripes are an optical encoding of a decimal number. A scanner reads the pattern of bars and recovers the digits. For retail products, the dominant formats are UPC-A (12 digits, common in the US) and EAN-13 (13 digits, common everywhere else). Every product you buy in a supermarket has one of these. # A brief history A corner shop with a few…
My mum was born 22nd November 1946. In 2005 she died of cancer. As of January 2023, she died more than half my life ago. We were in New Zealand in 2003. One day she began to bleed from the vagina. I remember her on the toilet with the door open, crying: "I just want to stop bleeding". The doctors diagnosed her with uterine cancer. She had a hysterectomy not much later. But later still when she had…
There's a skull on my shelf. I'm not kidding. It's a real human skull. I used to spend every summer with a family in Holland. The grandfather of the family was called Henk Pelser. He was a medical student in Amsterdam when World War II broke out. To the Nazis he was an Aryan, and he could simply have kept out of trouble in occupied Netherlands. But he saw Nazism for the evil it was and spent all…
# Definition: Fibonacci Sequence The Fibonacci Sequence is a sequence starting, 0, 1, 1, 2, 3, 5, 8, 13... , and each subsequent number is the sum of the previous two. We can define the Fibonacci numbers recursively as: F 0 = 0 F 1 = 1 F n = F n - 1 + F n - 2 # Definition: Phi Phi is the "Golden Ratio". Two quantities a and b are in the golden ratio φ if: a + b a = a b = φ Given this definition,…
In this post we'll explore the weird world of alternative base number systems. Let's start by counting to ten: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... Congratulations. But let's look at that sequence of symbols above. Isn't it weird that for most of the numbers I used just a single symbol, but for the last one I used two symbols ( 1 and 0 )? In base 10 we have 10 distinct symbols from 0-9, but we…
In JavaScript: "0" == 0 ; // true 0 == [ ] ; // true "0" == [ ] ; // false These three statements demonstrate that loose equality in JavaScript is not transitive and therefore is not a valid equivalence relation . Despite this, the above is not a bug. It conforms exactly to ECMAScript: IsLooselyEqual . # Equivalence Relations An Equivalence Relation is the mathematically formal term for what we…
Langton's Ant is a cellular automaton first described by Christopher Gale Langton in 1986. It looks like this: The "Ant" exists on an infinite grid. Each cell of the grid can be either white or black, but initially all are white. Each "generation" the Ant moves according to the following rules: Turn left if the current cell is white, else turn right Change the colour of the current cell Step…
Here's a pattern I find myself reusing in a lot of projects: type FormatOptions = { color : string ; fontWeight : string ; } ; const format = ( message : string , { color = "black" , fontWeight = "medium" } : Partial < FormatOptions > = { } , ) => { // Implementation not important // for this blog post } ; # Explanation Imagine you have a function which can display some text formatted in a…
There's a utility function I find myself adding to almost every TypeScript repo I work on. It goes like this: const isObject = ( unknown : unknown ) : unknown is Record < PropertyKey , unknown > => typeof unknown === 'object' && unknown !== null # Usecase in Error Handling Occasionally we have to deal with unknown values. The most common example of this is in catch blocks. Often we want to assume…
In this post I will demonstrate two approaches for simulating rolls of a die of a given size, using only rolls of a die of a different size. Throughout this post I will use dn terminology to refer to a die of size n . For example a d6 refers to the classic six-sided die. # Using base number conversion Given an input die of size n and a target die of size m , this approach works by expanding the…