Skip to content

Instantly share code, notes, and snippets.

View conartist6's full-sized avatar

Conrad Buck conartist6

View GitHub Profile
@conartist6
conartist6 / data-structure
Last active March 22, 2022 13:04
@iter-tools/regex data structure
┌───────────────┐
│ Expression │
│ root: true │
│ globalIdx: 1 │
└───────┬───────┘
parentSeq │ ▲
│ │ expr
┌──────────────┐ ┌───────┴─┴─────┐
│ State │ │ State │
│ type: cont │ │ type: success │
@conartist6
conartist6 / diagrams.md
Last active March 28, 2022 18:15
Regex engine refactor diagrams

Before

                                                            ┌───────────────┐
                                                            │  Expression   │
                                                            │  root: true   │
                                                            │ globalIdx: 1  │
                                                            └───────┬───────┘
                                                          parentSeq │ ▲
                                                                    │ │ expr
                                  ┌──────────────┐          ┌───────┴─┴─────┐
@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);
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 / index.md
Last active May 25, 2022 17:26
Node package scripts

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": {
@conartist6
conartist6 / index.js
Last active June 12, 2022 11:32
sync vs async for loops
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 / output.md
Last active September 15, 2022 22:21
cst-tokens alpha output

Input:

if (foo) {
  // comment
}

Traversal log

@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 / 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();
}
@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 = '-';