3

Is there a way to modify the parser, or define options, to align the indentation of a sequence of Queries on a Dataset?

Given:

ds = <|"a" -> 1, "b" -> <|"c" -> 2|>, "d" -> <|"e" -> 3, "f" -> <|"g" -> 4|>|>|> // Dataset;

A sequence of queries is indented like this:

ds[
    {"b" -> {"c" -> foo}}][
   {"d" -> {"e" -> bar}}][
  {"d" -> {"f" -> {"g" -> baz}}}][
 {"d" -> {{"f" -> quz}}}]

There are 2 problems with this format in practice with realistic workflows where queries are often lengthy and need to be modified repeatedly:

  1. The lines are staggered - it would be desirable to have them left-aligned, as applications with many lines continues to accumulate that slope, making spot-checking of depth more difficult than it should be.

  2. The open bracket "[" of each query appears on the line above it, making it more difficult than necessary to highlight a query (eg, to delete it). It would be more modular to confine each on its own line, but doing so results in a syntax error:

 Syntax::sntxb: Expression cannot begin with "[{d->{e->bar}}][{d->{f->{g->baz}}}]".

Interestingly, writing out Query solves the 2nd problem but not the first:

ds // 
 Query[{"b" -> {"c" -> foo}}] /*
  Query[ {"d" -> {"e" -> bar}}] /* 
    Query[ {"d" -> {"f" -> {"g" -> baz}}}] /*
      Query[ {"d" -> {{"f" -> quz}}}]

Note, chaining queries using RightComposition reverses the indentation compared to \, which also works.

alancalvitti
  • 15,143
  • 3
  • 27
  • 92

0 Answers0