Shallow does not seem to work with associations:

$r = ConstantArray[0, {1000, 1000}];

$r = Association["x" -> ConstantArray[0, {1000, 1000}]];

the second construction involves a large lag, probably by the kernel sending all that stuff to the FrontEnd which it will finally display in a "large output" box.

Would you consider this a bug?

In the meantime, what options do we have for getting an overview over large expressions including Associations without incurring all that overhead?


Thank you for taking the time to send us this suggestion to extend the usefulness of Shallow to Associations. I have forwarded your suggestion to the appropriate people in our development group. I have also included your contact information so that you can be notified if this gets implemented.

  • $\begingroup$ I would consider it something worth reporting. $\endgroup$ – Szabolcs Sep 2 '16 at 12:17
  • $\begingroup$ @Szabolcs done. $\endgroup$ – masterxilo Sep 2 '16 at 13:28

This formats Associations in the usual way:

ShallowAssociation[x_Association] := 
  RawBoxes@(({s = Shallow@Normal@x}~With~MakeBoxes@s)~
     Replace~(RowBox[{"{", val_, "}"}] :> 
       RowBox@{"\[LeftAssociation]", val, "\[RightAssociation]"})


$c = ConstantArray[0, {1000, 1000}];

$a = Association["x" -> ConstantArray[0, {1000, 1000}], "y" -> 0];

$ac = {$a, $c};
Shallow@($ac /. x_Association :> ShallowAssociation@x)(*fast*)

So this is a more general and faster Shallow:

FastShallow[y_] := Shallow[y /. x_Association :> ShallowAssociation@x]
  • $\begingroup$ though MakeBoxes might be a bottleneck here with more complicated expressions $\endgroup$ – masterxilo Sep 2 '16 at 12:30
$r = Association["x" -> ConstantArray[0, {1000, 1000}]];
    Shallow@($r /. Association -> Normal@*Association)

performs well.

  • $\begingroup$ Shallow[y /. x_Association :> {z = "Association" @@ Normal@Association@x}~ With~(z /; True)] preserves the information of which heads where Association $\endgroup$ – masterxilo Sep 2 '16 at 12:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.