Skip to content

Instantly share code, notes, and snippets.

View conartist6's full-sized avatar

Conrad Buck conartist6

  • Boulder, CO
View GitHub Profile
@conartist6
conartist6 / index.js
Last active January 28, 2023 00:00
ReadOnlyMap
const _ = Symbol('private');
class ReadOnlyMap {
constructor(map) {
this[_] = map;
}
static from(entries) {
return new ReadOnlyMap(new Map(entries));
}
@conartist6
conartist6 / ranges.js
Last active January 22, 2023 18:10
Tokenizer ranges
class Tokenizer {
constructor(source) {
this.source = source;
this.result = null; // result is a token, i.e. a stack of tokens
this.pathRangesByToken = new WeakMap();
this.prevTokensByToken = new WeakMap();
}
ownTokensFor(path) {
const { prevTokensByToken, pathRangesByToken } = this;
@conartist6
conartist6 / parse-grammar.js
Last active December 4, 2022 21:35
Parser spitballing
import { Grammar } from '@cst-tokens/grammar';
import { objectEntries } from '@cst-tokens/helpers/iterable';
import { eat, match } from '@cst-tokens/helpers/commands';
import { capture, ref, PN } from '@cst-tokens/helpers/shorthand';
// 2-+2+-2
/*
$capture = '2';
$node = { type: 'Digit', value: $caputure };
$capture = '-';
@conartist6
conartist6 / bench.js
Created November 26, 2022 03:37
Common empties microbenchmark
console.time('new Set');
for (let i = 0; i < 1_000_000; i++) {
new Set();
}
console.timeEnd('new Set');
console.time('new Map');
for (let i = 0; i < 1_000_000; i++) {
new Map();
}
function /*1*/ foo /*2*/ (/*3*/) /*4*/ {/*5*/} /*6*/
@conartist6
conartist6 / index.js
Created November 7, 2022 22:33
splitWhen for trusted destructuring
export class Coroutine {
constructor(generator) {
this.generator = generator[Symbol.iterator]();
this.current = this.generator?.next();
}
get value() {
return this.current.value;
}
@conartist6
conartist6 / output.md
Last active September 15, 2022 22:21
cst-tokens alpha output

Input:

if (foo) {
  // comment
}

Traversal log

@conartist6
conartist6 / lamp.js
Last active June 1, 2023 15:53
State machine with generators
/*
Lamps often have on-off switches that twist clockwise, clicking as they do.
Some such switches have four states, even though the lamp has only two (on, and off).
On these lamps clicking the switch will only cause an on/off transition every other click.
Just looking at the lamp you'll know if it's on, but not if the next click will turn it off.
To reliably interact with such a lamp you will use an algorithm, even in real life.
I use the term strategy to refer to an algorithm expressed with generators like this --
the strategy pattern is useful to abstract an algorithm from the representation of the data.
For more about lamps: https://temperaturemaster.com/why-does-it-take-two-clicks-to-turn-on-a-lamp/
function sum(arr) {
let acc = 0;
for (let i = 0; i < arr.length; i++) {
acc += arr[i].val;
}
return acc;
}
const array1 = [];
const array2 = [];
@conartist6
conartist6 / builders.js
Last active May 6, 2022 00:12
CST traversal prototype
class Builder {
*ensure(...tokens) {
for (const token of tokens) {
yield* token.type === 'Thunk' ? token.ensure(this) : this.advance(token);
}
}
*allow(...tokens) {
for (const token of tokens) {
yield* token.type === 'Thunk' ? token.allow(this) : this.advance(token, true);