Skip to content

Instantly share code, notes, and snippets.

Reg Braithwaite raganwald

Block or report user

Report or block raganwald

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@raganwald
raganwald / enumerations.es6
Last active Mar 9, 2019
Code from "Enumerations, Denumerables, and Cardinals"
View enumerations.es6
// See: http://raganwald.com/2019/02/27/enumerability.html
// combinators
function slice (generator, n, limit = Infinity) {
return function * sliced () {
let i = 0;
for (const element of generator()) {
if (i++ < n) continue;
View parentheses-and-palindromes.es6
// Inspired by:
//
// "Eh, have another semi-periodic reminder that ()() is not a palindrome but ())( is"
//
// --https://twitter.com/_julesh_/status/1101262745092218882
const END = Symbol('end');
class PushdownAutomaton {
constructor(internal = 'start', external = []) {
@raganwald
raganwald / generate-balanced-parentheses.es6
Last active Mar 1, 2019
A generator function that enumerates all finite balanced parentheses strings
View generate-balanced-parentheses.es6
// memoizes ordinary functions
const memoized = (fn, keymaker = JSON.stringify) => {
const lookupTable = new Map();
return function (...args) {
const key = keymaker.call(this, args);
return lookupTable[key] || (lookupTable[key] = fn.apply(this, args));
}
};
@raganwald
raganwald / balanced-dpa.es6
Last active Feb 16, 2019
A Deterministic Pushdown Automaton ("DPA") that recognizes balanced parentheses
View balanced-dpa.es6
// The Deterministic Pushdown Automata Engine
const END = Symbol('end');
const RECOGNIZED = Symbol('recognized');
const UNRECOGNIZED = Symbol('unrecognized');
const POP = Symbol('pop');
function DPA (start) {
return string => {
const stack = [];
@raganwald
raganwald / implicit.js
Last active Feb 15, 2019
Balanced parentheses solution with implicit state
View implicit.js
function balanced (string) {
const iterator = string[Symbol.iterator]();
return balancedIterator(iterator) === true;
}
const CLOSE = {
'(': ')',
'[': ']',
'{': '}'
@raganwald
raganwald / why-indeed.js
Last active Feb 12, 2019
Recursive Pattern Matching using the Why Combinator
View why-indeed.js
const just =
target =>
input =>
input.startsWith(target) &&
target;
const cases =
(...patterns) =>
input => {
const matches = patterns.map(p => p(input)).filter(m => m !== false);
View slice.es6
//
// 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 Jan 11, 2019
A very leaky abstraction that Greenspuns Lisp's CAR/CDR, plus support for [first, ...rest]
View lisp.js
const ComposableCarCdr = {
has (target, name) {
if (name in target) {
return true;
}
if (name === Symbol.isConcatSpreadable) {
return true;
}
View ayoayo.js
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 Nov 12, 2018
Ridiculously Simple Balanced Parentheses Algorithm
View balanced.js
function isBalancedSimple (before) {
if (before === '') return true;
const after = before.replace('()','');
return (before !== after) && isBalancedSimple(after);
}
function isBalancedMultiple (before) {
if (before === '') return true;
You can’t perform that action at this time.