Skip to content

Instantly share code, notes, and snippets.

@gigamonkey
gigamonkey / church.js
Last active November 4, 2022 15:35
Demonstration of Church numerals and booleans in Javascript
// Church numerals -- functions of two arguments, a function and a zero value.
// The nth Church numeral applies an n-fold compostion f to the zero value.
const Zero = (f, z) => z;
const One = (f, z) => f(z);
const Two = (f, z) => f(f(z));
const Three = (f, z) => f(f(f(z)));
// etc.
/*
@gigamonkey
gigamonkey / Main.java
Created October 19, 2022 15:37
Ms. O'Keefe's loop challenge
/*
Write a program to generate this output:
#
$#
*$#
$*$#
*$*$#
$*$*$#
@gigamonkey
gigamonkey / clean-branches.sh
Last active October 16, 2022 18:44
Script to clean up local branches that have been merged to the current branch.
#!/bin/bash
# Script to clean up local branches that have been merged to the current branch.
# Normally you'd run this while on main.
set -euo pipefail
current=$(git branch --show-current)
git branch --merged | cut -c 3- | while read -r branch; do
@gigamonkey
gigamonkey / fn-to-svg.py
Created October 8, 2021 23:31
This is how I roll ...
#!/usr/bin/env python
from math import cos, pi
def svg(width, height, s, fn):
zero = s(0)
points = " ".join(f"{x},{s(fn(x, width))}" for x in range(width))
#!/usr/bin/env python
"Group people so everyone meets everyone."
import sys
from argparse import ArgumentParser, FileType
from collections import defaultdict
from itertools import combinations
from random import choice
@gigamonkey
gigamonkey / mult.py
Created May 15, 2020 00:00
Got nerd sniped by a terrible interview question
#!/usr/bin/env python3
# Let's pretend we have to implement 32-bit multiplication with just
# addition, subtraction, equality/inequality tests, and bit twiddling
# (shifts and bitwise logical ops). For bonus points detect overflow
# and signal via an exception.
# A friend got asked this on an interview. It is a terrible interview
# question unless possibly you're interviewing someone to be a junior
# chip designer or something. But it nerd sniped me anyway as an
#!/bin/bash
set -x
set -e
here=$(dirname $(realpath "$0"))
dir="$1"
mirrors="$2"
@gigamonkey
gigamonkey / steele-on-proofs.md
Last active March 26, 2019 16:56
Guy Steele on formal proofs from Coders at Work

Guy Steele on formal proofs from Coders at Work

Seibel: Do you ever try to formally prove your code correct?

Steele: Well, it depends on the code. If I’m writing code with some kind of tricky mathematical invariant I will go for a proof. I wouldn’t dream of writing a sorting routine without constructing some kind of invariant and proving it.

Seibel: Peter van der Linden in his book Expert C Programming has a sort of dismissive chapter about proofs in which he shows a proof of something, but then, ha ha, this proof has a bug in it.

Steele: Yes, indeed. Proofs can also have bugs.

#!/usr/bin/env python3
"Generate all strings containing just ( and ) with parens balenced."
from itertools import count
def parens():
def nested(x):
return ("(" * x) + (")" * x) if x > 0 else ""
def levenshtein_distance(a, b):
"""
Compute the Levenshtein distance between a and b.
(https://en.wikipedia.org/wiki/Levenshtein_distance) Uses an
efficient dynamic programming implementation.
"""
m = [ [ 0 for _ in range(len(a) + 1) ] for _ in range(len(b) + 1) ]
for i in range(1, len(a) + 1):