1
$\begingroup$

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.

$\endgroup$
  • $\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
1
$\begingroup$
$r = Association["x" -> ConstantArray[0, {1000, 1000}]];
    Shallow@($r /. Association -> Normal@*Association)

performs well.

$\endgroup$
  • $\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
1
$\begingroup$

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]
$\endgroup$
  • $\begingroup$ though MakeBoxes might be a bottleneck here with more complicated expressions $\endgroup$ – masterxilo Sep 2 '16 at 12:30

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.