Skip to content

Instantly share code, notes, and snippets.

Avatar

Conrad Buck conartist6

  • Boulder, CO
View GitHub Profile
@conartist6
conartist6 / bench.js
Created Nov 26, 2022
Common empties microbenchmark
View bench.js
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();
}
View 1.fixture.js
function /*1*/ foo /*2*/ (/*3*/) /*4*/ {/*5*/} /*6*/
@conartist6
conartist6 / index.js
Created Nov 7, 2022
splitWhen for trusted destructuring
View index.js
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 Sep 15, 2022
cst-tokens alpha output
View output.md

Input:

if (foo) {
  // comment
}

Traversal log

@conartist6
conartist6 / lamp.js
Last active Aug 4, 2022
State machine with generators
View lamp.js
/*
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.
*/
function* toggleStrategy(lamp) {
@conartist6
conartist6 / index.js
Last active Jun 12, 2022
sync vs async for loops
View index.js
function* range(end) {
for (let i = 0; i < end; i++) {
yield i;
}
}
const a = Array.from(range(65536));
module.exports['for await..of values from one chunk'] = {
fn: async function(deferred) {
@conartist6
conartist6 / index.md
Last active May 25, 2022
Node package scripts
View index.md

Node packages usually ship with scripts that are useful to developers. Common scripts might be build, test, or lint, but you will need to look inside the package.json file to see what scripts are actually available for a particular package. Here is what a common package with a build script might look like:

{
  "name": "my-package",
  "version": "0.1.0",
  "scripts": {
    "build": "transpile -f lib/index.ts -o /lib/index.js"
  },
 "devDependencies": {
View code.js
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
CST traversal prototype
View builders.js
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);
@conartist6
conartist6 / json-parser.js
Last active Apr 19, 2022
A human-friendly json parser with parserate
View json-parser.js
import parserate from '@iter-tools/parserate';
const t = {
token: (value) => ({ type: 'token', value }),
literal: (value) => ({ type: 'literal', value }),
};
const escapes = {
'"': '"',
'\\': '\\',