Skip to content

Instantly share code, notes, and snippets.

@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):
@gigamonkey
gigamonkey / entitle_google_docs.js
Last active September 9, 2018 02:15
App Script script to change the link text of the link at the current cursor position to the name of the linked Google doc.
function onInstall() {
onOpen();
}
function onOpen() {
DocumentApp.getUi()
.createMenu('Fix links')
.addItem('Entitle link at cursor', 'entitleAtCursor')
.addItem('Entitle all links', 'entitleAllLinks')
.addToUi();
@gigamonkey
gigamonkey / tidy-branches
Last active February 1, 2018 01:22
Bash script for cleaning up local branches that have been removed from remotes
#!/bin/bash
# Prune remote branches that no longer exist on remotes
for remote in $(git remote); do
git remote prune "$remote"
done
# Delete branches whose tracking branch no longer exists
for b in $(git show-ref --heads | cut -c 53-); do
remote=$(git config "branch.$b.remote")
#!/usr/bin/env python3
#
# Dump all the messages from a given Slack channel since a certain
# timestamp, one line per message with timestamp and user name.
#
from datetime import datetime
from pytz import timezone, utc
#!/usr/bin/env python3
#
# Find the best matches between a set of dirty names and a canonical set.
#
# Is O(N^2) in the number of names.
#
import json
from lcs import lcs
@gigamonkey
gigamonkey / logging.py
Last active October 16, 2017 08:30
Proof of concept of a decorator for logged database objects.
#!/usr/bin/env python3
from functools import wraps
import json
# Pretend this was logging to disk
log = []
def mutator(fn):
@gigamonkey
gigamonkey / ids.py
Last active September 5, 2017 23:09
#!/usr/bin/env python3
from time import sleep
from datetime import datetime, timezone
# Snowflake like ID generator that generates a 53-bit id suitable for sending as a number in JSON.
TIME_BITS = 40
COUNTER_BITS = 10
MACHINE_BITS = 3
@gigamonkey
gigamonkey / allen.md
Last active August 7, 2017 20:56
Turing Award Winner Fran Allen on changing role of women in computing. (From Coders at Work)

Turing Award Winner Fran Allen on changing role of women in computing. (From Coders at Work)

Seibel: You also mentioned before that when you started, people thought women would be good programmers because women were thought to be detail-oriented. These days the conventional wisdom is that it’s men who have a bizarre ability to focus on things, usually to the detriment of everything else, and that’s why most programmers are men.

Allen: Right.