Skip to content

Instantly share code, notes, and snippets.

@karlrwjohnson
karlrwjohnson / createReducer.ts
Created April 19, 2019 18:24
Type-safe reducer builder
/*
createReducer() builds a reducer function using an initial state and a map of reducer functions.
Any action whose type matches a key in the reducer map will cause createReducer()'s return value to invoke a method in the reducer map.
If you annotate your dispatch() function as DispatchFor<yourReducerHere>, it will prevent you from passing an event that it's incapable of handling.
*/
import { Action } from 'redux';
// Define an action whose metadata is stored in its "payload" field.
interface PayloadAction<K, P> extends Action<K> {
@karlrwjohnson
karlrwjohnson / httpProxy.js
Created February 19, 2019 22:44
Quickie proxy that logs requests while forwarding to another service
const http = require('http');
const destHost = 'localhost';
const destPort = 4002;
http.createServer((req, res) => {
console.log(`> ${req.method} ${req.url}`);
for (let k in req.headers) {
console.log(`> ${k} = ${req.headers[k]}`)
}
@karlrwjohnson
karlrwjohnson / Taylor Cardsmith.txt
Created January 14, 2019 18:54
Taylor Cartsmith
Beastsoul Monk 1 / Armored & Gifted Blade (Soulknife) 4
STR 10 = ( 0p) 10
DEX 14 = ( 5p) 14
CON 14 = ( 5p) 14
INT 8 = (-2p) 8
WIS 20 = (17p) 18 + (Hu) 2
CHA 10 = ( 0p) 10
Level 1: Beastsoul Monk 1
@karlrwjohnson
karlrwjohnson / Show_nav_labels_in_bitbucket.js
Created November 26, 2018 16:05
Show navbar links in BitBucket (Greasemonkey script)
// ==UserScript==
// @name Show nav labels in Bitbucket
// @version 0.1
// @description Add labels below icons on the left nav bar
// @match (insert URL of bitbucket server)
// @grant none
// ==/UserScript==
console.log('user script loaded');
(function() {
@karlrwjohnson
karlrwjohnson / pattern.java
Created October 19, 2018 14:44
Action pattern for turn-based games
// Let's define a couple of dependencies to get started.
interface GameContext {
// Methods which Actions can use to query the state of the game
}
interface ActionGameController {
// Methods which Actions can use to affect the game
// - In Redux, this is the dispatch() method and various actionCreators.
// - In Java, this is a controller.
@karlrwjohnson
karlrwjohnson / cycles.py
Last active May 1, 2018 03:27
Solution to Adam Babcock's brain-teaser
'''
Solution to Adam Babcock's brain-teaser
Here's the basic gist of the problem: A principal places one hundred students'
names into one hundred identical boxes, which are sitting in a row in the principal's
office. Each box contains exactly one name, but the names are not guaranteed to
be arranged in any particular order within those boxes.
Students are called in one at a time and are permitted to open up to fifty of
those boxes. If one of the boxes they open contains their name, they get a reward.
@karlrwjohnson
karlrwjohnson / lockfile_demo.sh
Last active April 3, 2024 06:24
Lockfile Demo - Demonstrates how to use Linux's "flock" command to create mutexes inside of a shell script
#!/bin/bash
# Lockfiles are a feature in Linux which allows multiple processes to use a file as a mutex.
# This script contains example code based on this blog post, but with a bugfix: https://dmorgan.info/posts/linux-lock-files/
# Wrapper function for easy reuse.
# I've heavily annotated it, but without the comments or arguments it's only 13 lines long.
# Parameters (you can hard-code them instead if you want)
# - LOCK_FILE: Which file to use for the lock.
@karlrwjohnson
karlrwjohnson / Lodash FP.md
Last active February 27, 2018 16:17
Lodash FP Proposal

Lodash FP Proposal

Premises

  • Immutable: All functions return a copy of the data instead of modifying it. Even functions which normally mutate data, like _set, are wrapped to make a copy first.
@karlrwjohnson
karlrwjohnson / minimal_pyqt_pyopengl.py
Last active December 21, 2019 09:45
Python + QT + OpenGL 3
"""
Minimal demo of OpenGL >= 3.0, Qt >= 5.0 in Python 3.6
Uses a programmable shader pipeline to draw a pair of triangles.
Eschews PyQt's wrapper functions in favor of PyOpenGL's because I couldn't get it to draw
anything other than a black screen (which means any one of a hundred details were incorrect).
Besides, you'd have to fall back on PyOpenGL to get glDrawArrays anyway, so we may as well
use it for everything.
@karlrwjohnson
karlrwjohnson / generate_pinch_pot.py
Created December 22, 2017 06:02
Procedural STL file generation in Python
import functools
import numpy
import struct
import svg.path # pip install svg.path==2.2
from typing import List, Dict, Tuple, Callable # kitchen sink?
array=numpy.array
# SVG <path> string describing the undistorted profile of the pot
PATH_DATA='''
m 0,50.916666 9.1801683,0 c 0.8686257,-1.2561 2.0631007,-1.983971 3.2272117,-2.612425 6.345522,-3.425674 6.113453,-5.441741 9.183746,-10.130426 3.920877,-5.987623 6.247456,-11.575049 11.615424,-15.913572 1.361948,-1.10076 2.477231,0.565914 1.236122,1.532279 -5.445873,4.240321 -7.508074,9.124891 -11.047477,15.345625 C 20.372917,44.45 19.05,46.566667 12.975328,49.970155 11.821211,50.616778 10.583333,51.59375 10.583333,52.916667 L 0,52.916667