View draw-diagrams.es6
// A state machine that works from a description including explicit transitions,
// and can generate a transition map and a digraph
const STATES = Symbol("states");
const STARTING_STATE = Symbol("starting-state");
const TRANSITIONS = Symbol("transitions");
const RESERVED = [STARTING_STATE, TRANSITIONS];
const MACHINES_TO_CURRENT_STATE_NAMES = new WeakMap();
const MACHINES_TO_STARTING_STATES = new WeakMap();
View right-truncatables.es6
function * multiplesOf (startingWith, n) {
let number = startingWith;
while (true) {
yield number;
number = number + n;
}
}
function destructure (iterable) {
View confoosed.es6
// 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
View sequence.es6
/////////////////////////////////////////////////////////////////////////////////
// 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.
View life.es6
// 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);
View binary-encoded.js
// 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);
View genesis.es6
// 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)],
View two-dimensional-turing-machines.js
// 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
View multiply.es6
// 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)
View doubly-infinite-tape.es6
// 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.