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 / markit.hy
Created December 12, 2017 22:10
Given a URL and an optional list of tags, write an Emacs ORG-mode bookmark and associated content to a bookmark repository.
#!/usr/bin/env hy
; This script requires `pip install slugify newspaper`. It runs on Hy 0.12 and Python 3.5.
; This is part of an (incomplete) xinetd-driven toolchain for managing local bookmarks.
(import sys os os.path [slugify [slugify]] [newspaper [Article]])
(def bookmark-index
(os.path.join (get os.environ "HOME") "Wiki" "Bookmarks" "Bookmarks.org"))
@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 / 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 / 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 / fizzbuzz.rs
Last active February 8, 2021 08:11
Pedantic FizzBuzz implementation in Rust
use std::fmt;
enum FizzBuzzOr {
Fizzbuzz(&'static str),
Or(i32)
}
impl fmt::Display for FizzBuzzOr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use FizzBuzzOr::*;
@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