Drop discards in smart pattern constructors

This commit is contained in:
Tony Garnock-Jones 2021-12-09 18:52:42 +01:00
parent 20b83aa5a5
commit 58daa8c981
1 changed files with 16 additions and 7 deletions

View File

@ -2,7 +2,7 @@
/// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com> /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones <tonyg@leastfixedpoint.com>
import { canonicalString, is, KeyedDictionary, Record, RecordConstructorInfo, Value } from '@preserves/core'; import { canonicalString, is, KeyedDictionary, Record, RecordConstructorInfo, Value } from '@preserves/core';
import { AnyValue } from './actor.js'; import { AnyValue, Ref } from './actor.js';
import * as P from '../gen/dataspacePatterns.js'; import * as P from '../gen/dataspacePatterns.js';
export type Path = Array<AnyValue>; export type Path = Array<AnyValue>;
@ -164,26 +164,35 @@ export function lit(v: AnyValue): P.Pattern {
return P.Pattern.DLit(P.DLit(v)); return P.Pattern.DLit(P.DLit(v));
} }
function indexedMembers(items: P.Pattern[]): KeyedDictionary<number, P.Pattern, Ref> {
const members = new KeyedDictionary<number, P.Pattern, Ref>();
items.forEach((p, i) => {
if (!is(p, _)) members.set(i, p);
});
return members;
}
export function rec(label: AnyValue, ... fields: P.Pattern[]): P.Pattern { export function rec(label: AnyValue, ... fields: P.Pattern[]): P.Pattern {
return P.Pattern.DCompound(P.DCompound.rec({ return P.Pattern.DCompound(P.DCompound.rec({
ctor: P.CRec({ ctor: P.CRec({
label, label,
arity: fields.length, arity: fields.length,
}), }),
members: new KeyedDictionary(fields.map((p, i) => [i, p])), members: indexedMembers(fields),
})); }));
} }
export function arr(... patterns: P.Pattern[]): P.Pattern { export function arr(... patterns: P.Pattern[]): P.Pattern {
return P.Pattern.DCompound(P.DCompound.arr({ return P.Pattern.DCompound(P.DCompound.arr({
ctor: P.CArr(patterns.length), ctor: P.CArr(patterns.length),
members: new KeyedDictionary(patterns.map((p, i) => [i, p])), members: indexedMembers(patterns),
})); }));
} }
export function dict(... entries: [AnyValue, P.Pattern][]): P.Pattern { export function dict(... entries: [AnyValue, P.Pattern][]): P.Pattern {
return P.Pattern.DCompound(P.DCompound.dict({ const members = new KeyedDictionary<AnyValue, P.Pattern, Ref>();
ctor: P.CDict(), entries.forEach(([k, p]) => {
members: new KeyedDictionary(entries), if (!is(p, _)) members.set(k, p);
})); });
return P.Pattern.DCompound(P.DCompound.dict({ ctor: P.CDict(), members }));
} }