Skip to content

Instantly share code, notes, and snippets.

View elfsternberg's full-sized avatar
💭
Breathing

Ken "Elf" Mathieu Sternberg elfsternberg

💭
Breathing
View GitHub Profile
@elfsternberg
elfsternberg / assert_is_enum.rs
Created February 6, 2018 22:27
A pair of Rust macros to assert if a value matches a simple enum (no ADT support).
// I was really annoyed that I couldn't easily assert that a
// straightforward enum was being matched. I'm sure there's a better
// way to do this but, hey, this is my first Rust macro.
macro_rules! assert_is_enum {
($left:expr, $right:path) => ({
match &$left {
left_val =>
if let $right = *left_val { }
else {
@elfsternberg
elfsternberg / markow-name-generator.py
Created March 15, 2018 02:07
A simple Python library using Markov chains to generate new names.
#!/usr/bin/env python3
"""
Takes a filename as its required argument. The file should contain
names from a sample list, one name per line. It creates a Markov
chain of one, two, and three-letter character sequences, including
octothorpe-anchored sequences for the starts of names. It then
generates 'n' random names, each using the probabistic distribution of
three-letter character sequences, to make names that match the
patterns fed in the original.
@elfsternberg
elfsternberg / derivatives.py
Last active March 19, 2018 03:02
Python parsing with derivatives.
#!/usr/bin/env python
# This file implements Brzozowski's parsing-with-derivatives (see:
# https://arxiv.org/abs/1604.04695) with fairly simple and
# straightforward Python code. This implementation is naive and
# subject to exponential memory growth in the worst case; in practice,
# average complexity is closer to linear. This version recalculates
# derivatives every time they are needed; memoization, the first and
# most obvious optimization, is not implemented.
#
@elfsternberg
elfsternberg / ffoxsession.js
Created May 14, 2018 22:20
A small utility, written in nodejs (with a little C), to unpack and display your last known Firefox tab list.
#!/usr/bin/env nodejs
// ffoxsession is a little utility to dump the contents of your last
// known firefox session. Works with 52 through 60 (so far). Requires
// the lz4json utility (https://github.com/andikleen/lz4json), as
// Firefox uses some weird version of lz4 optimized for JSON data.
var fs = require("fs");
var os = require("os");
@elfsternberg
elfsternberg / deque.scm
Created September 13, 2018 21:04
The Deque exercise from SICP, Chapter 3, section 3.3
; This is the "Write a Deque" exercise from SICP, section 3.3.
;
; It took me THREE DAYS to get this right, and in the end, you know what
; really made the difference? Doing it on paper. Once I'd written out
; all the actions and in text described what I intended to do, this took
; less than an hour to put together.
;
; This is my lesson: ALWAYS, ALWAYS, ALWAYS describe the algorithm, no
; matter how simple, on paper first. ALWAYS explain it to your rubber
; duck first: https://selftaughtcoders.com/rubber-duck-debugging/
@elfsternberg
elfsternberg / mdual.py
Created November 21, 2018 01:16
Dual-Monitor Setup For A Modern Laptop and An Old Desktop Monitor
#!/usr/bin/env python
# This is the current script I use to configure my laptop and my old
# desktop monitor. My old monitor is an Acer H243, released in 2009,
# maximum resolution 1920x1200. The laptop is crazy modern huge,
# 3840x2160. This script picks out the identities of the monitors in
# xrandr, and their maximal resolutions, and then rescales the content
# so that the two monitors are displaying their best output.
# This code makes a lot of assumptions. It assumes that your external
@elfsternberg
elfsternberg / git-rb.py
Created December 13, 2018 16:39
Git extension to list the most recent branches, in most-recent-commit order, with a human-friendly interval display.
#!/usr/bin/env python3
# GIT-RBAGO
#
# For any given git repository, this script will print out the
# (default) ten _most recently worked on_ branches, along with a
# fairly human-readable version of how long ago that was.
#
# Example:
#
@elfsternberg
elfsternberg / tumblon.py
Created December 13, 2018 16:58
Pick a few random images from Tumblr based on the day they were posted
#!/usr/bin/env python3
# TUMBLON
#
# With Tumblr shutting down, I wanted to at least keep some semblance
# of the experience of Tumblr around. Since I was able to download
# all of my tumblr blogs into separate local repos thanks to
# Beat Bolli's wonderful tumblr_backup tool
# (https://github.com/bbolli/tumblr-utils/blob/master/tumblr_backup.md),
# I just wanted it to spit out a few random images everyday I could
{-# START_FILE package.yaml #-}
name: {{name}}
version: 0.1.0.0
homepage: https://github.com/{{github-username}}{{^github-username}}githubuser{{/github-username}}/{{name}}#readme
license: MPL-2.0
license-file: LICENSE.md
author: {{author-name}}{{^author-name}}Author name here{{/author-name}}
maintainer: {{author-email}}{{^author-email}}example@example.com{{/author-email}}
copyright: {{copyright}}{{^copyright}}{{year}}{{^year}}2019{{/year}} {{author-name}}{{^author-name}}Author name here{{/author-name}}{{/copyright}}
@elfsternberg
elfsternberg / Makefile
Created October 3, 2019 20:22
A Makefile recipe for attaching short help messages to targets
.PHONY: default help
default: help
help: ## Print this help message
@M=$$(perl -ne 'm/^((\w|-)*):.*##/ && print length($$1)."\n"' Makefile | \
sort -nr | head -1) && \
perl -ne "m/^((\w|-)*):.*##\s*(.*)/ && print(sprintf(\"%s: %s\t%s\n\", \$$1, \" \"x($$M-length(\$$1)), \$$3))" Makefile