Skip to content

Instantly share code, notes, and snippets.

View raganwald's full-sized avatar

Reg Braithwaite raganwald

View GitHub Profile
@raganwald
raganwald / confoosed.es6
Created June 26, 2017 21:00
I'm very confoosed
// How does this iterable ever become "done?"
function * Numbers () {
let n = 0;
while (true) yield n++;
}
const numbers = Numbers();
// 1. I expect 'true', I don't expect it to iterate to infinity
@raganwald
raganwald / sequence.es6
Last active June 18, 2017 09:31
Generating sequences using iterators and without integers or arrays
/////////////////////////////////////////////////////////////////////////////////
// Generating "A Sequence Problem" using iterators, without integers or arrays //
// See http://raganwald.com/2017/06/04/sequences.html //
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// Generic things useful for working with interables and iterators //
/////////////////////////////////////////////////////////////////////
// 1. These operations produce repeatable iterables.
@raganwald
raganwald / life.es6
Last active April 3, 2017 02:24
My first working implementation of Life on a Turing Machine
// A prelude of handy items
const I = _ => _;
const aKindOf = clazz => x => x instanceof clazz;
// preallocation and copy would be way faster
const flatMap = (arr, lambda) => {
const inLen = arr.length;
const mapped = new Array(inLen);
@raganwald
raganwald / binary-encoded.js
Last active April 1, 2017 13:35
Compiler that translates a program for a Turing Machine with arbitrary marks, into a program for a Turing Machine that only uses ones and zeroes
// binary encode a possibly unflattened instruction set with
// marks beyond 0 and 1
function binaryEncoded ({ description: descriptionWithMarks, tape: tapeWithMarks = [], LEFT, RIGHT, HALT, Write, Move }) {
const recognizeStates = new Set();
const marks = new PseudoSet();
for (const [currentState, currentMark, nextState, ...instructions] of descriptionWithMarks) {
recognizeStates.add(currentState);
@raganwald
raganwald / genesis.es6
Created March 21, 2017 02:45
The simplest building block for Life
// This two-dimensional Turing Machine starts in the upper-left-hand corner of the
// tableau and counts the number of 1s surrounding a center cell, and
// then determines whether the centre cell should be born, survive, or die.
const life = [
['start', 0, 'ul-zero', Move(RIGHT)],
['start', 1, 'ul-one', Move(RIGHT)],
['ul-zero', 1, 'uc-one', Move(RIGHT)],
['ul-zero', 0, 'uc-zero', Move(RIGHT)],
@raganwald
raganwald / two-dimensional-turing-machines.js
Last active March 21, 2017 15:43
Compiler that compiles a Turing Machine for a two-dimensional ’tableau’ into a Turing Machine that operates on a standard one-dimensional ‘tape'
// inherently FIXED size, not infinite
// works by compiling both the description (a/k/a program)
// and the tape (a/k/a data), then decompiling the tape when done.
// takes a tape of square length (e.g. 32x32)
// description instructions look like this:
//
// [currentState, match, nextState, ...instructions].
//
// Instructions can be Write(mark), Move(UP), Move(DOWN), Move(LEFT), and Move(RIGHT)
// The instructions are compiled for a Turning Machine that supports annotations
@raganwald
raganwald / multiply.es6
Last active June 26, 2023 19:54
Turing Machine program that multiplies two numbers
// Multiplies two numbers
//
// The numbers are encoded as strings of `1`s separated by a single `0`
//
// e.g. 2x3 => 6
// [1, 1, 0, 1, 1, 1] => [1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1]
//
// Dependencies:
// - The TM must understand aribtrary tape marks (numbers *and* strings)
// - The TM must allow sequences of instructions e.g. Write(1), Move(LEFT)
@raganwald
raganwald / doubly-infinite-tape.es6
Last active March 4, 2017 20:10
Decorator that turns a Turing Machine with a tape that stretches rightward to infinity, into a Turing Machine with a tape that stretches to infinity in both directions
// A decorator that compiles a program written for a bi-directionally
// infinite tape, into a program for a unidirectionally
// infinite tape.
//
// Requires a machine that accepts a description written in quintupes
// [ [currentState, currentMark, nextState, markToWrite, direction], ... ]
//
// The machine it decorates must have properties for LEFT and RIGHT,
// representing the possible moves,and HELT, a conventional special state for
// (what else) halting.
@raganwald
raganwald / trampolines.es6
Last active February 26, 2017 13:32
Trampolines in ES6
// See http://raganwald.com/2013/03/28/trampolines-in-javascript.html
function trampoline (fn) {
return function trampolined (...args) {
let result = fn.apply(this, args);
while (result instanceof Function) {
result = result();
}
@raganwald
raganwald / curated-links-to-javascript-allonge.md
Last active October 11, 2016 18:11
A curated list of curated lists of JS links that link to JavaScript Allongé