Another getOrDefault

This commit is contained in:
Emery Hemingway 2022-10-30 21:09:17 -05:00
parent 489d6b31d5
commit 4e795cb92a
1 changed files with 11 additions and 0 deletions

View File

@ -1189,6 +1189,17 @@ proc mapEmbeds*[A, B](pr: sink Preserve[A]; op: proc (v: A): B): Preserve[B] =
of pkEmbedded:
result = embed op(pr.embed)
proc getOrDefault*[T, V](pr: Preserve[T]; key: string; default: V): V =
## Retrieves the value of `pr[key]` if `pr` is a dictionary containing `key`
## or returns the `default` value.
var sym = toSymbol(key, T)
if pr.kind == pkDictionary:
for (k, v) in pr.dict:
if sym == k:
if fromPreserve(result, v): return
else: break
default
proc writeText*[E](stream: Stream; pr: Preserve[E]) =
## Encode Preserves to a `Stream` as text.
if pr.embedded: write(stream, "#!")