From 58daa8c981f28d6e44e9a894aa524db01214c6a1 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 9 Dec 2021 18:52:42 +0100 Subject: [PATCH] Drop discards in smart pattern constructors --- packages/core/src/runtime/pattern.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/core/src/runtime/pattern.ts b/packages/core/src/runtime/pattern.ts index 898d4df..8a1a431 100644 --- a/packages/core/src/runtime/pattern.ts +++ b/packages/core/src/runtime/pattern.ts @@ -2,7 +2,7 @@ /// SPDX-FileCopyrightText: Copyright © 2016-2021 Tony Garnock-Jones 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'; export type Path = Array; @@ -164,26 +164,35 @@ export function lit(v: AnyValue): P.Pattern { return P.Pattern.DLit(P.DLit(v)); } +function indexedMembers(items: P.Pattern[]): KeyedDictionary { + const members = new KeyedDictionary(); + items.forEach((p, i) => { + if (!is(p, _)) members.set(i, p); + }); + return members; +} + export function rec(label: AnyValue, ... fields: P.Pattern[]): P.Pattern { return P.Pattern.DCompound(P.DCompound.rec({ ctor: P.CRec({ label, arity: fields.length, }), - members: new KeyedDictionary(fields.map((p, i) => [i, p])), + members: indexedMembers(fields), })); } export function arr(... patterns: P.Pattern[]): P.Pattern { return P.Pattern.DCompound(P.DCompound.arr({ 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 { - return P.Pattern.DCompound(P.DCompound.dict({ - ctor: P.CDict(), - members: new KeyedDictionary(entries), - })); + const members = new KeyedDictionary(); + entries.forEach(([k, p]) => { + if (!is(p, _)) members.set(k, p); + }); + return P.Pattern.DCompound(P.DCompound.dict({ ctor: P.CDict(), members })); }