This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Prelude: Composeable Functional Pattern Matchers | |
| const just = | |
| target => | |
| input => | |
| input.startsWith(target) && | |
| target; | |
| const cases = | |
| (...patterns) => |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 = [".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", "."]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function * multiplesOf (startingWith, n) { | |
| let number = startingWith; | |
| while (true) { | |
| yield number; | |
| number = number + n; | |
| } | |
| } | |
| function destructure (iterable) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ///////////////////////////////////////////////////////////////////////////////// | |
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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)], |