# Fast Shallow for Associations?

Shallow does not seem to work with associations:

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

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


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?

WRI:

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.

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

$r = Association["x" -> ConstantArray[0, {1000, 1000}]]; Shallow@($r /. Association -> Normal@*Association)


performs well.

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

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]"})
);


Usage:

$c = ConstantArray[0, {1000, 1000}];$c(*slow*)
Shallow@$c(*fast*)$a = Association["x" -> ConstantArray[0, {1000, 1000}], "y" -> 0];
Shallow@$a(*slow*) ShallowAssociation@$a(*fast*)

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


So this is a more general and faster Shallow:

FastShallow[y_] := Shallow[y /. x_Association :> ShallowAssociation@x]

• though MakeBoxes might be a bottleneck here with more complicated expressions – masterxilo Sep 2 '16 at 12:30