6

For this data:

dt = <|"a"-> <|"x"-> 1|>,"b"-> <|"y"-> 2|>,"c"-> <|"z"->3|>|>

It’s awkward when mapping the same function to multiple locations:

dt // Query[{"a" -> {"x" -> Framed}}]

When applying the same function at multiple sites, syntax gets long:

dt // Query[{"a" -> {"x" -> Framed}}] // Query[{"b" -> {"y" -> Framed}}]

In larger datasets the problem compounds. Is there a workaround for shortcuts like this:

dt // Query[{All -> {All -> Framed}}]

And also with Lists, and Span

dt // Query[{All -> {(1;;2) -> Framed}}]

as well as with hybrids of List and Associations.

PS - Even the nested parentheses are a pain for deeply nested data (we routinely deal with 6 levels or more), mitigated by this helper:

queryAt[seq__] := Query[Fold[Rule /* Reverse /* List, Reverse[{seq}]]];

eg

 queryAt["a", "x", Framed]
alancalvitti
  • 15,143
  • 3
  • 27
  • 92

1 Answers1

4

You can use Replace and Tuples to expand a Part specification in MapAt so it behaves as needed with Span.

expandedPart[rules__] :=
 Tuples[Replace[{Span[r__] :> Range[r], (t_ /; Head[t] =!= List) -> List[t]}] /@ {rules}]

This expands a Part specification.

expandedPart[1 ;; 3 ;; 2, All]
(* {{1, All}, {3, All}} *)

Now with MapAt

MapAt[Framed, expandedPart[1 ;; 3 ;; 2, All]]@dt

Mathematica graphics

or

MapAt[Framed, expandedPart[{"a", "c"}, All]]@dt

Mathematica graphics

Hope this helps.

Edmund
  • 42,267
  • 3
  • 51
  • 143
  • @alancalvitti Note that relative from end specifications like -4;;-2 will work with the above but not open mixed relative from start and end like ;;-2 or 1;;-2. – Edmund Jun 02 '16 at 00:51