Another getOrDefault
This commit is contained in:
parent
489d6b31d5
commit
4e795cb92a
|
@ -1189,6 +1189,17 @@ proc mapEmbeds*[A, B](pr: sink Preserve[A]; op: proc (v: A): B): Preserve[B] =
|
||||||
of pkEmbedded:
|
of pkEmbedded:
|
||||||
result = embed op(pr.embed)
|
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]) =
|
proc writeText*[E](stream: Stream; pr: Preserve[E]) =
|
||||||
## Encode Preserves to a `Stream` as text.
|
## Encode Preserves to a `Stream` as text.
|
||||||
if pr.embedded: write(stream, "#!")
|
if pr.embedded: write(stream, "#!")
|
||||||
|
|
Loading…
Reference in New Issue