patterns: grabRecord, grabDictionary

This commit is contained in:
Emery Hemingway 2023-07-25 18:58:55 +01:00
parent ce8e800187
commit 8fc9608199
1 changed files with 13 additions and 2 deletions

View File

@ -277,15 +277,26 @@ proc inject*(pat: Pattern; bindings: openArray[(Preserve[Cap], Pattern)]): Patte
for (key, val) in bindings:
result.dcompound.dict.entries[key] = val
proc recordPattern*(label: Preserve[Cap], fields: varargs[Pattern]): Pattern =
proc grabRecord*(label: Preserve[Cap], fields: varargs[Pattern]): Pattern =
runnableExamples:
from std/unittest import check
import syndicate/actors, preserves
check:
$recordPattern("Says".toSymbol(Cap), grab(), grab()) ==
$grabRecord("Says".toSymbol(Cap), grab(), grab()) ==
"""<rec Says [<bind <_>> <bind <_>>]>"""
DCompoundRec(label: label, fields: fields.toSeq).toPattern
proc grabDictionary*(bindings: sink openArray[(Value, Pattern)]): Pattern =
## Construct a pattern that grabs some dictionary pairs.
DCompoundDict(entries: bindings.toTable).toPattern
proc grabDictionary*(bindings: sink openArray[(string, Pattern)]): Pattern =
## Construct a pattern that grabs some dictionary pairs.
## Keys are converted from strings to symbols.
result = DCompoundDict().toPattern
for (key, val) in bindings.items:
result.dcompound.dict.entries[key.toSymbol(Cap)] = val
type
Path* = seq[Value]
Paths* = seq[Path]