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
// 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; |
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
// 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 = []) { |
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
// 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)); | |
} | |
}; |
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
// 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 = []; |
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 balanced (string) { | |
const iterator = string[Symbol.iterator](); | |
return balancedIterator(iterator) === true; | |
} | |
const CLOSE = { | |
'(': ')', | |
'[': ']', | |
'{': '}' |
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
const just = | |
target => | |
input => | |
input.startsWith(target) && | |
target; | |
const cases = | |
(...patterns) => | |
input => { | |
const matches = patterns.map(p => p(input)).filter(m => m !== false); |
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
// | |
// 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; | |
} |
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
const ComposableCarCdr = { | |
has (target, name) { | |
if (name in target) { | |
return true; | |
} | |
if (name === Symbol.isConcatSpreadable) { | |
return true; | |
} | |
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 scoop(before, fromPit) { | |
const after = before.slice(0); | |
const seedsInHand = after[fromPit]; | |
after[fromPit] = 0; | |
return [after, seedsInHand]; | |
} | |
function nextPit(pit) { |
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 isBalancedSimple (before) { | |
if (before === '') return true; | |
const after = before.replace('()',''); | |
return (before !== after) && isBalancedSimple(after); | |
} | |
function isBalancedMultiple (before) { | |
if (before === '') return true; |