Skip to content

Instantly share code, notes, and snippets.

View raganwald's full-sized avatar

Reg Braithwaite raganwald

View GitHub Profile
@raganwald
raganwald / why-indeed.js
Last active February 12, 2019 13:01
Recursive Pattern Matching using the Why Combinator
const just =
target =>
input =>
input.startsWith(target) &&
target;
const cases =
(...patterns) =>
input => {
const matches = patterns.map(p => p(input)).filter(m => m !== false);
//
// http://raganwald.com/2019/01/14/structural-sharing-and-copy-on-write.html
// http://raganwald.com/2019/01/26/reduce-reuse-recycle.html
//
const SliceHandler = {
has (slice, property) {
if (property in slice) {
return true;
}
@raganwald
raganwald / lisp.js
Last active January 11, 2019 20:02
A very leaky abstraction that Greenspuns Lisp's CAR/CDR, plus support for [first, ...rest]
const ComposableCarCdr = {
has (target, name) {
if (name in target) {
return true;
}
if (name === Symbol.isConcatSpreadable) {
return true;
}
function scoop(before, fromPit) {
const after = before.slice(0);
const seedsInHand = after[fromPit];
after[fromPit] = 0;
return [after, seedsInHand];
}
function nextPit(pit) {
@raganwald
raganwald / balanced.js
Last active November 12, 2018 19:55
Ridiculously Simple Balanced Parentheses Algorithm
function isBalancedSimple (before) {
if (before === '') return true;
const after = before.replace('()','');
return (before !== after) && isBalancedSimple(after);
}
function isBalancedMultiple (before) {
if (before === '') return true;
@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) {