Skip to content

ER: simpler, faster walk/1 #2611

@pkoppstein

Description

@pkoppstein

A well-known efficiency enhancement for walk/1 is to use an inner function along the lines of:

def walk(f):
  def w: ...;
  w;

In addition to this efficiency enhancement, I would like to propose a very small change
that both greatly simplifies the definition and resolves some other issues, notably a discrepancy with the semantics of
assignments of the form .b = empty.

For example, the expression:

[ { a: {b: 2, c: 3} } ] | map(.b = empty)

yields []

so one would expect (and perhaps require) that:

walk(if type == "object" then .b = empty]

should also yield []. Using the current def of walk/1, however, it yields [null] instead.

Here then is the proposed new version of walk/1, which is
faster, simpler and more conformant as described above:

# Apply f to composite entities recursively, and to atoms
def walk(f):
  def w:
    if type == "object"
    then map_values(w)
    elif type == "array" then map(w)
    else .
    end
    | f;
  w;

Please also note that the above proposed definition is sufficient to resolve the issue raised in #2584.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions