Skip to content

Instantly share code, notes, and snippets.

View rpivo's full-sized avatar

Ryan Pivovar rpivo

  • New York City, NY
View GitHub Profile
@rpivo
rpivo / index.md
Last active December 10, 2020 05:46
Sync Methods in Node

Sync Methods in Node

It's very easy to get into callback hell in Node if the programmer needs to block the process and chooses not to use Node's sync methods (or its promise methods).

Since Node is made to deal with lots of streamed data, and since these streams have an unknown size and can take an unknown amount of time to complete, much of Node's functionality is built with callbacks to block parts of the process.

Even just simulating an arbitrary and minimal example of this, it's clear how obnoxiously complicated this can get.

function callFirst(cb) {
@rpivo
rpivo / index.md
Last active December 10, 2020 15:38
Public & Private Access Modifiers in C++

Inside C++ classes, access modifiers like public and private are written similarly to Python branches like if, else, and for statements.

Public & Private Access Modifiers in C++

class Thing {
  public:
    int x;
  private:
 int y;
@rpivo
rpivo / index.md
Last active December 11, 2020 14:24
const in C++

const in C++

In C++, we can declare a value as immutable by using the const keyword, which is similar to const in JavaScript.

bool isVowel(const char p_char) { ... }

Unlike JavaScript, we can label incoming arguments with const, indicating that they won't undergo any mutation throughout the function.

@rpivo
rpivo / index.md
Last active May 1, 2021 02:55
Binary Numbers & Two's Complement

Binary Numbers & Two's Complement

Ben Eater's video on two's complement helped me come up with some ideas for this gist.

Interpreting binary numbers is not terribly difficult, but it's also easy to forget how they work. I decided to jot down a few quick things for reference.

Given that each bit can represent either 0 or 1, there are a total of two possibilities per bit. The number of possibilities doubles for each new bit added.

In an 8-bit sequence, each digit, from right to left, is double the previous digit, with the first bit representing 1.

@rpivo
rpivo / index.md
Last active December 13, 2020 03:16
Why It's Called "Floating-Point"

Why It's Called "Floating-Point"

Each 64-bit floating-point number is broken up into three parts:

  • 1 bit is used for the sign (-/+)
  • 11 bits are used for the exponent
  • 52 bits are used for the fraction

In researching floating-point arithmetic, some key terminology comes up frequently. Understanding what a significand is and how a radix point relates to it can help explain where the term "floating-point" comes from.

@rpivo
rpivo / index.md
Last active December 12, 2020 23:32
How Biased Exponents Work in Floating-Point Numbers

How Biased Exponents Work in Floating-Point Numbers

Each 64-bit floating-point number is broken up into three parts:

  • 1 bit is used for the sign (-/+)
  • 11 bits are used for the exponent
  • 52 bits are used for the fraction

Looking at just the exponent portion:

@rpivo
rpivo / index.md
Last active December 14, 2020 00:35
Gerard Meszaros' Test Double Definitions

Gerard Meszaros' Test Double Definitions

Martin Fowler wrote a great blog article called Mocks Aren't Stubs, and in it he describes some of Gerard Meszaros' test double definitions. Here's a quote:

  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.
  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).
  • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed [...] for the test.
  • Spies are stubs that also record some information based on how they were called. One form of this might be an email service that records how many messages it was sent.
  • Mocks are [...] objects pre-programmed with expectations which form a specification of the calls they are expected to receive.
@rpivo
rpivo / index.md
Last active December 14, 2020 21:11
Using NPM's Pre & Post Scripts

Using NPM's Pre & Post Scripts

NPM has some powerful built-in scripts that can be used inside a package.json file. Scripts have built-in pre and post lifecycle hooks that help you control these processes to some degree. If the process(es) are more complicated, this may be too simplistic for your needs, but it's nice to know that they're available.

You can do something like this:

  "scripts": {
    "prestart": "echo 'preparing build...'",
    "poststart": "echo 'process finished.'",
@rpivo
rpivo / index.md
Last active December 15, 2020 13:58
Common Data Types in C++

Common Data Types in C++

Some of the more popular primitive types in C++ are:

  • bool: 1 byte (usually)
  • int: 4 bytes
  • char: 1 byte
  • float: 4 bytes
  • double: 8 bytes
  • void: no size
  • wchar_t: 4 bytes
@rpivo
rpivo / index.md
Last active December 16, 2020 14:45
Strings in C++

Strings in C++

In JavaScript, strings are a primitive type. In C++, they're a class that can be constructed with different char types depending on the kind of string you want.

#include <string>

string foo = "bar";