if (foo) {
// comment
}
const _ = Symbol('private'); | |
class ReadOnlyMap { | |
constructor(map) { | |
this[_] = map; | |
} | |
static from(entries) { | |
return new ReadOnlyMap(new Map(entries)); | |
} |
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; |
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 = '-'; |
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*/ |
export class Coroutine { | |
constructor(generator) { | |
this.generator = generator[Symbol.iterator](); | |
this.current = this.generator?.next(); | |
} | |
get value() { | |
return this.current.value; | |
} |
/* | |
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 = []; |
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); |