Skip to content

Instantly share code, notes, and snippets.

View christianscott's full-sized avatar

Christian Scott christianscott

View GitHub Profile
@christianscott
christianscott / array.c
Created April 12, 2020 14:14
c array helpers
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_CAPACITY_INDEX 0
#define ARRAY_LENGTH_INDEX 1
#define ARRAY_DATA_START_INDEX 2
#define capacity(array) array[ARRAY_CAPACITY_INDEX]
#define length(array) array[ARRAY_LENGTH_INDEX]
#define get(array, index) array[ARRAY_DATA_START_INDEX + index]
@christianscott
christianscott / distance.ts
Created January 8, 2020 06:01
N-dimensional Euclidean distance
/**
* Calculates the euclidean distance between two N-dimensional
* points, represented as arrays of length N.
*/
export function euclideanDistance<P extends [number, ...number[]]>(
p1: P,
p2: P,
): number {
if (p1.length === 1) {
return Math.abs(p1[0] - p2[0]);
@christianscott
christianscott / conways_game_of_life.js
Last active November 14, 2019 14:39
Conway's game of life
// @ts-check
const ALIVE = 1;
const DEAD = 0;
/**
* @param {0 | 1} cell
* @param {number} nNeighbours
*/
function getNextCell(cell, nNeighbours) {
if (cell === DEAD) {
@christianscott
christianscott / groups_at_a_party.ts
Last active November 6, 2019 11:35
Groups at a party
function estimateExpectedNumberOfGroups(n: number, iterations: number = 2000): number {
const groupCounts = range(iterations).map(() =>
countGroups(createPairsOfPeople(n))
);
return mean(groupCounts);
}
type Pair<T> = readonly [T, T];
function countGroups(pairsOfPeople: Iterable<Pair<number>>): number {
export module Maybe {
enum MaybeKind {
SOME = 1,
NOTHING,
}
export type Nothing = { kind: MaybeKind.NOTHING };
export type Some<T> = { kind: MaybeKind.SOME; value: T };
export type t<T> = Some<T> | Nothing;
export const nothing = (): Nothing => ({ kind: MaybeKind.NOTHING });
@christianscott
christianscott / bundle.js
Created September 2, 2019 05:34
shitty module bundler
const renderBootstrap = body => `const modules = {};
function require(name) {
return modules[name] && modules[name].exports;
}
function define(name, definition) {
const module = {};
definition(require, module);
modules[name] = module;
}
${body}
@christianscott
christianscott / .vimrc
Created July 9, 2019 15:57
Minimal .vimrc
" plugins
call plug#begin('~/.vim/plugged')
Plug 'pangloss/vim-javascript'
Plug 'mxw/vim-jsx'
Plug 'leafgarland/typescript-vim'
Plug '/usr/local/opt/fzf'
Plug 'junegunn/fzf.vim'
@christianscott
christianscott / stacksize.js
Created July 9, 2019 15:29
Measure the maximum stack size for functions with different numbers of arguments
let range = length => Array.from({ length }, (_, i) => i)
let template = (params, args) => `let calls = 0
let r = (${params}) => {
calls++
r(${args})
}
try {
r(${args})
} catch (_) {
@christianscott
christianscott / route_with_query_params.tsx
Last active July 9, 2019 00:39
Route with query params (react router v4)
import * as React from 'react';
import { Redirect, Route, RouteComponentProps } from 'react-router';
type RouteWithQueryParamsProps<R extends string, O extends string> = {
path: string;
params: readonly ({
name: R;
required: true;
} | {
name: O;
@christianscott
christianscott / grep.rs
Created July 8, 2019 11:00
Shitty grep implementation
use std::env;
use std::fs;
use std::io;
use std::path::Path;
use std::str;
fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
let pattern = &args[1];
let path = &args[2];