Skip to content

Instantly share code, notes, and snippets.

View raganwald's full-sized avatar

Reg Braithwaite raganwald

View GitHub Profile
@raganwald
raganwald / balanced.js
Last active November 10, 2018 19:23
Balanced parentheses via pattern matching
// Prelude: Composeable Functional Pattern Matchers
const just =
target =>
input =>
input.startsWith(target) &&
target;
const cases =
(...patterns) =>
@raganwald
raganwald / eight-queens-0-fundamentals.js
Last active August 7, 2018 23:22
The Eight Queens Problem
// Computes the twelve "fundamental" solutions to the eight queens problem
// by filtering the results of the "half-inductive" algorithm.
//
// see http://raganwald.com/2018/08/03/eight-queens.html
//
// search space: 2,750 candidate positions
function testDiagonals (queens) {
const nesw = [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."];
const nwse = [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."];
@raganwald
raganwald / denumerable.rb
Created July 8, 2018 18:00
Denumerables in Ruby
#Denumerable
# Copyright 2002 Reginald Braithwaite-Lee. All Rights Reserved.
# http://www.braithwaite-lee.com/tips/denumerables.html
module Denumerable
#must define :denumerableIteratorFactory, :denumerableIteratorFactory=
#initialize must support NO ARGUMENTS
@raganwald
raganwald / draw-diagrams.es6
Last active January 3, 2022 20:38
State Machines
// 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();
@raganwald
raganwald / right-truncatables.es6
Created December 15, 2017 20:00
Computing right-truncatable primes by brute force
function * multiplesOf (startingWith, n) {
let number = startingWith;
while (true) {
yield number;
number = number + n;
}
}
function destructure (iterable) {
@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)],