Guest User

Untitled

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. asso = <|"x" -> {
  2. <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
  3. <|"a" -> 6, "b" -> "z", "c" -> {}|>}
  4. |>
  5.  
  6. asso // Query["x", Select[#c === {} &], Framed@*"b"]
  7.  
  8. {Framed["z"]}
  9.  
  10. asso // Query[
  11. {"x"},
  12. MapAt[
  13. MapAt[Framed, #, "b"] &,
  14. #,
  15. Position[#, _?(Function[asso, asso["c"] === {}])]] &
  16. ]
  17.  
  18. <|"x" -> {
  19. <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>,
  20. <|"a" -> 6, "b" -> Framed["z"], "c" -> {}|>}
  21. |>
  22.  
  23. bSel = MapAt[Framed, #, "b"] &;
  24. cPos = Position[#, _?(#c === {} &)] &
  25. cSel = MapAt[bSel, #, cPos[#]] &
  26. cSel /@ asso
  27.  
  28. asso // Query[{"x"}, All, Query[If[#c === {}, {"b" -> Framed}, All]][#]&]
  29.  
  30. asso2 =
  31. <| "x" ->
  32. { <| "a" -> 5
  33. , "b" -> "y"
  34. , "c" -> {5, 6, 7}
  35. |>
  36. , <| "a" -> 6
  37. , "b" -> <| "x" ->
  38. { <|"a" -> 5, "b" -> "y", "c" -> {5, 6, 7}|>
  39. , <|"a" -> 6, "b" -> "z", "c" -> {}|>
  40. }
  41. |>
  42. , "c" -> {}
  43. |>
  44. }
  45. |>
  46.  
  47. asso2 //
  48. Query[{"x"},All,Query[If[#c==={},Query[{"b"->Query[{"x"},All,Query[If[#c==={},{"b"->Framed},All]][#]&]}][#]&,All]][#]&]
  49.  
  50. ReplacePart[
  51. asso2
  52. , ii:{"x", i_, "b", "x", j_, "b"}
  53. /; asso2[["x", i, "c"]] === {}
  54. && asso2[["x", i, "b", "x", j, "c"]] === {}
  55. :> RuleCondition@Extract[asso2, ii, Framed]
  56. ]
  57.  
  58. drill[row_] :=
  59. Query[
  60. {"x"},
  61. All,
  62. Which[
  63. #["c"] != {}, #
  64. , Head@#["b"] === Association, Query[{"b" -> drill}]@#
  65. , True, Query[{"b" -> Framed}]@#
  66. ] &
  67. ]@row
  68.  
  69. drill@asso
  70. drill@asso2 (* from WReach post *)
  71.  
  72. ClearAll[seekReplace]
  73. seekReplace[row_, {}] := row
  74. seekReplace[row_, cmd_?(VectorQ[#, AssociationQ] &)] :=
  75. Query[
  76. {"x"},
  77. All,
  78. Which[
  79. First[cmd]["ExitSeek"]@#, #
  80. , First[cmd]["Seek"]@#,
  81. Query[{First[cmd]["SeekKey"] -> (seekReplace[#, Rest@cmd] &)}]@#
  82. , True,
  83. Query[{First[cmd]["ReplaceKey"] -> First[cmd]["Replace"]}]@#
  84. ] &
  85. ]@row
  86.  
  87. seekCommands = <|
  88. "ExitSeek" -> (#["c"] != {} &),
  89. "Seek" -> (Head@#["b"] === Association &), "SeekKey" -> "b",
  90. "Replace" -> Framed, "ReplaceKey" -> "b"|>;
  91. seekCommands = ConstantArray[seekCommands, 2]
  92.  
  93. seekReplace[asso, seekCommands]
  94. seekReplace[asso2, seekCommands]
Add Comment
Please, Sign In to add comment