Skip to content

Instantly share code, notes, and snippets.


John Earnest JohnEarnest

View GitHub Profile
View k means clustering demo.k
/ the infamous iris dataset
iris:((5.1; 3.5; 1.4; 0.2; `setosa)
(4.9; 3.0; 1.4; 0.2; `setosa)
(4.7; 3.2; 1.3; 0.2; `setosa)
(4.6; 3.1; 1.5; 0.2; `setosa)
(5.0; 3.6; 1.4; 0.2; `setosa)
(5.4; 3.9; 1.7; 0.4; `setosa)
(4.6; 3.4; 1.4; 0.3; `setosa)
JohnEarnest / prog.k
Last active Mar 24, 2020
Sandpile Cellular Automaton
View prog.k
/ Sandpiles
/ see:
r: {y(#y)!x+!#y} / rotate y by x places
n: {+/(-1 1r\:x),(-1 1r/:\:x)} / orthogonal neighbor counts
t: {x+(-4*u)+1_+1_n@+0,+0,u:3<x} / toroidal sand collapsing
once: 100 100# 5 / initial board
tick: t / update: just collapse one iteration
draw: {,(;solarized;x)} / draw board as paletted pixels
JohnEarnest /
Created Feb 5, 2020
OLPC Setup Script
# OLPC setup script
# First, make sure your OS is updated to the latest version:
# Then ensure you have a WiFi connection.
# fix clock (modify time zone as desired)
sudo ntpdate
sudo hwclock --systohc
JohnEarnest / lisparse.js
Created Dec 13, 2019
A Lisp(ish) S-Expression parser in ES6, supporting symbols, signed ints, lists, and line comments.
View lisparse.js
// S-Expression Parser
const token = /^\s*(?:;[^\n]*\n)*\s*(?:(-?\d+)|([a-z\-!?]+|[+\-*\/<>=()]))/
function parse(text) {
const term = _ => {
const x = token.exec(text)
if (!x) throw 'Reached the end of input while parsing!'
text = text.slice(x[0].length)
const here = x[1] ? +x[1] : x[2]
JohnEarnest /
Created Oct 31, 2019
XO-Chip Event Queue Proposal

Input Events

The COSMAC VIP had a 16-key hexadecimal keypad, and while modern computers rarely have a keyboard which directly mimics this arrangement, there are many approaches for adapting the input devices which are available to render Chip8 games playable. Many emulators offer custom keyboard or gamepad mappings for each program, and recently Octo has begun to offer "Adaptive Input" options to allow touch-based devices to play games.

It is often desirable to more directly expose the input capabilities of modern machines. This proposal outlines a pair of new instructions for XO-Chip which would expand it with text and pointer input, with room for future expansion. The instructions are as follows:

8NN8 #  v0 := poll NN
8XY9 #  event vx - vy

As input events occur, the XO-Chip interpreter will store them in a FIFO queue. The poll instruction pops events from this queue, storing the type code of the event in the fixed register v0. A type code of zero indicates that no events are currently

JohnEarnest /
Created Jun 9, 2019
Minimal RSS podcast fetcher
# A script for fetching the latest episode of
# the podcast "My Brother, My Brother and Me" automagically
# every podcast hosts the XML RSS feed somewhere different,
# but feedburner appears to be a fairly popular aggregator.
# unfortunately, there's no way to just fetch the most N recent entries;
# for long-running podcasts the rss blob can get big.
print 'fetching rss feed...'
JohnEarnest / peg.js
Created Mar 31, 2017
A compact set of PEG parser combinators in ES6.
View peg.js
// PEG parser combinators
const lookup = x => typeof x == 'string' ? eval(x) : x
const match = (x, y, z) => x ? { c:y, v:z } : { e: true }
const eof = s => match(s.length, 0, '')
const char = s => match(s.length, 1, s[0])
const not = g => s => match(g(s).e, 0, '')
const has = g => s => match(!g(s).e, 0, '')
const oneof = t => s => match(t.indexOf(s[0]) >= 0, 1, s[0])
const lit = t => s => match(!s.indexOf(t), t.length, t)
const option = g => choose(g, lit(''))
JohnEarnest / CorsServer.js
Created Dec 27, 2016
A CORS-aware read/write HTTP server in Node.js
View CorsServer.js
// Node.js HTTP file server example.
// Serves documents via GET requests with CORS headers and (if enabled)
// additionally permits writes via POST requests.
// Before using, PLEASE be aware of the security risks associated with
// running a server like this on publicly accessible machines!
JohnEarnest /
Created Jan 6, 2016
Quake WAD3 texture unpacker
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
// a very basic texture unpacker for the Quake WAD3 file format
// see
public class WAD3 {
static int index = 0;
JohnEarnest /
Created Feb 22, 2014
Playing with hexagons.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.util.List;
import static java.lang.Math.*;
public class HexScape extends JPanel {
public static void main(String[] args) {