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


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

A sequence of queries is indented like this:

    {"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.

  • 1
    $\begingroup$ You can use Code cell style. $\endgroup$ – Kuba Nov 23 '16 at 18:49
  • $\begingroup$ @Kuba, great, can you give an example? Any downstream effects? $\endgroup$ – alancalvitti Nov 23 '16 at 18:50
  • $\begingroup$ They are gray :) and also have InitializationCell->True option but this can be switched on notebook or stylesheet level if you need it. As to examples, select bracket and Alt+8 $\endgroup$ – Kuba Nov 23 '16 at 18:53
  • $\begingroup$ @Kuba, converting to Code style seems to work for issue 1 but not for 2, where it still throws a syntax error. $\endgroup$ – alancalvitti Nov 23 '16 at 19:16
  • $\begingroup$ Right, the same as with /., don't have a quick solution for you, sorry. $\endgroup$ – Kuba Nov 23 '16 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.