Skip to content

Instantly share code, notes, and snippets.

Avatar

John Earnest JohnEarnest

View GitHub Profile
View k means clustering demo.k
/ the infamous iris dataset
cols:`sepalLength`sepalWidth`petalLength`petalWidth`species
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
JohnEarnest / prog.k
Last active Mar 24, 2020
Sandpile Cellular Automaton
View prog.k
/ Sandpiles
/ see: https://www.youtube.com/watch?v=hBdJB-BzudU&t=0s
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
JohnEarnest / setup.sh
Created Feb 5, 2020
OLPC Setup Script
View setup.sh
#!/bin/bash
# OLPC setup script
# First, make sure your OS is updated to the latest version:
# http://wiki.laptop.org/go/Release_notes/13.2.11#Installation
# Then ensure you have a WiFi connection.
# fix clock (modify time zone as desired)
sudo ntpdate pool.ntp.org
sudo hwclock --systohc
@JohnEarnest
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
JohnEarnest / eventproposal.md
Created Oct 31, 2019
XO-Chip Event Queue Proposal
View eventproposal.md

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
JohnEarnest / bam.py
Created Jun 9, 2019
Minimal RSS podcast fetcher
View bam.py
#!/usr/bin/python
# 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
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
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
JohnEarnest / WAD3.java
Created Jan 6, 2016
Quake WAD3 texture unpacker
View WAD3.java
import java.io.*;
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
// a very basic texture unpacker for the Quake WAD3 file format
// see http://hlbsp.sourceforge.net/index.php?content=waddef
public class WAD3 {
static int index = 0;
@JohnEarnest
JohnEarnest / HexScape.java
Created Feb 22, 2014
Playing with hexagons.
View HexScape.java
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) {