Skip to content

Traversing a nested record in deep #1763

Closed Answered by yannham
suimong asked this question in Q&A
Discussion options

You must be logged in to vote

Hi!

Here is a possibility: I just wrote a recursive function which traverses records and gather all leafs as an array:

let leafs
  | doc m%"
      Returns all the leafs of a record as an array of paths.

      Each element of the returned outermost array, a path, is composed of the
      consective fields that need to be traversed to reach the leaf concatened
      with the value of the leaf.

      # Examples

      ```nickel
      {
        foo = {
          bar = 1,
          baz = 2,
        },
        foobis.barbis.borg = ["hello"],
      }
      |> leafs
      ```

      gives

      ```nickel
      [
        [ "foo", "bar", 1 ],
        [ "foo", "baz", 2 ],
        [ "foobis", "bar…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@suimong
Comment options

@yannham
Comment options

Answer selected by suimong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants