Step with mulitple indexes

This commit is contained in:
Emery Hemingway 2023-10-28 00:24:11 +01:00
parent 6e0834316f
commit ca5d44ed32
2 changed files with 9 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# Package
version = "20231026"
version = "20231028"
author = "Emery Hemingway"
description = "data model and serialization format"
license = "Unlicense"

View File

@ -94,7 +94,7 @@ type
DictEntry[E] = tuple[key: Preserve[E], val: Preserve[E]]
proc `==`*[A, B](x: Preserve[A]; y: Preserve[B]): bool =
func `==`*[A, B](x: Preserve[A]; y: Preserve[B]): bool =
## Check `x` and `y` for equivalence.
if x.kind == y.kind and x.embedded == y.embedded:
case x.kind
@ -329,6 +329,13 @@ func step*(pr, idx: Preserve): Option[Preserve] =
if i < pr.len:
result = some(pr[i])
func step*(pr: Preserve; path: varargs[Preserve]): Option[Preserve] =
## Step into `pr` by indexes at `path`.
result = some(pr)
for index in path:
if result.isSome:
result = step(result.get, index)
func step*[E](pr: Preserve[E]; key: Symbol): Option[Preserve[E]] =
## Step into dictionary by a `Symbol` key.
if pr.isDictionary: