anonymous patterns + muting with _

This commit is contained in:
Felix Roos 2024-04-17 12:37:19 +02:00
parent e0c4997f1e
commit 93d151b943

View File

@ -54,10 +54,12 @@ export function repl({
const scheduler = const scheduler =
sync && typeof SharedWorker != 'undefined' ? new NeoCyclist(schedulerOptions) : new Cyclist(schedulerOptions); sync && typeof SharedWorker != 'undefined' ? new NeoCyclist(schedulerOptions) : new Cyclist(schedulerOptions);
let pPatterns = {}; let pPatterns = {};
let anonymousIndex = 0;
let allTransform; let allTransform;
const hush = function () { const hush = function () {
pPatterns = {}; pPatterns = {};
anonymousIndex = 0;
allTransform = undefined; allTransform = undefined;
return silence; return silence;
}; };
@ -82,6 +84,15 @@ export function repl({
// set pattern methods that use this repl via closure // set pattern methods that use this repl via closure
const injectPatternMethods = () => { const injectPatternMethods = () => {
Pattern.prototype.p = function (id) { Pattern.prototype.p = function (id) {
if (id.startsWith('_') || id.endsWith('_')) {
// allows muting a pattern x with x_ or _x
return silence;
}
if (id === '$') {
// allows adding anonymous patterns with $:
id = `$${anonymousIndex}`;
anonymousIndex++;
}
pPatterns[id] = this; pPatterns[id] = this;
return this; return this;
}; };