Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- asso = <|"x" -> {
- <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
- <|"a" -> 6, "b" -> "z", "c" -> {}|>}
- |>
- asso // Query["x", Select[#c === {} &], Framed@*"b"]
- {Framed["z"]}
- asso // Query[
- {"x"},
- MapAt[
- MapAt[Framed, #, "b"] &,
- #,
- Position[#, _?(Function[asso, asso["c"] === {}])]] &
- ]
- <|"x" -> {
- <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
- <|"a" -> 6, "b" -> Framed["z"], "c" -> {}|>}
- |>
- bSel = MapAt[Framed, #, "b"] &;
- cPos = Position[#, _?(#c === {} &)] &
- cSel = MapAt[bSel, #, cPos[#]] &
- cSel /@ asso
- asso // Query[{"x"}, All, Query[If[#c === {}, {"b" -> Framed}, All]][#]&]
- asso2 =
- <| "x" ->
- { <| "a" -> 5
- , "b" -> "y"
- , "c" -> {5, 6, 7}
- |>
- , <| "a" -> 6
- , "b" -> <| "x" ->
- { <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>
- , <|"a" -> 6, "b" -> "z", "c" -> {}|>
- }
- |>
- , "c" -> {}
- |>
- }
- |>
- asso2 //
- Query[{"x"},All,Query[If[#c==={},Query[{"b"->Query[{"x"},All,Query[If[#c==={},{"b"->Framed},All]][#]&]}][#]&,All]][#]&]
- ReplacePart[
- asso2
- , ii:{"x", i_, "b", "x", j_, "b"}
- /; asso2[["x", i, "c"]] === {}
- && asso2[["x", i, "b", "x", j, "c"]] === {}
- :> RuleCondition@Extract[asso2, ii, Framed]
- ]
- drill[row_] :=
- Query[
- {"x"},
- All,
- Which[
- #["c"] != {}, #
- , Head@#["b"] === Association, Query[{"b" -> drill}]@#
- , True, Query[{"b" -> Framed}]@#
- ] &
- ]@row
- drill@asso
- drill@asso2 (* from WReach post *)
- ClearAll[seekReplace]
- seekReplace[row_, {}] := row
- seekReplace[row_, cmd_?(VectorQ[#, AssociationQ] &)] :=
- Query[
- {"x"},
- All,
- Which[
- First[cmd]["ExitSeek"]@#, #
- , First[cmd]["Seek"]@#,
- Query[{First[cmd]["SeekKey"] -> (seekReplace[#, Rest@cmd] &)}]@#
- , True,
- Query[{First[cmd]["ReplaceKey"] -> First[cmd]["Replace"]}]@#
- ] &
- ]@row
- seekCommands = <|
- "ExitSeek" -> (#["c"] != {} &),
- "Seek" -> (Head@#["b"] === Association &), "SeekKey" -> "b",
- "Replace" -> Framed, "ReplaceKey" -> "b"|>;
- seekCommands = ConstantArray[seekCommands, 2]
- seekReplace[asso, seekCommands]
- seekReplace[asso2, seekCommands]
Add Comment
Please, Sign In to add comment