- Essential JavaScript Links
- ES-6 Learning
- [JavaScript Path]
- [Awesome FP in JS]
This file contains 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 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 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 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 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)], |
This file contains 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
// 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 |
This file contains 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
// 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) |
This file contains 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 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. |
This file contains 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
// 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(); | |
} |