14
$\begingroup$

We got this nice Iconize function to shrink code in notebook from version 11.3. But, I noticed a caveat in reverting an Iconize-d object back: if the wrapped stuff is comment, then the reversion (by using Convert to Standard Form) will give a Null rather than the comment before.

Say for the comment below:

(*some nice comment;
Plot[Sin[x],{x,-10,10}]
*)

If you Iconize it and then Convert to Standard Form, you'll get

Null

I think it can hardly be considered as bug as to some sense the commented code should give Null; but it definitely doesn't give the comment back.

So, is there any workaround to avoid Null if we de-Iconize a comment?

$\endgroup$
17
$\begingroup$

It is not a bug. Iconize works with expressions. Comments / line breaks / indentation and other box/input features are stripped by the FrontEnd before sending it to the Kernel.

Iconize[
 Hold@Range[1,
   10
   ]
 ]
(*evaluate and convert to StandardForm*)   
Hold[Range[1, 10]] (*no line breaks, indentation or @ *) 
$\endgroup$
  • 1
    $\begingroup$ Thanks for the explanation! I'll use the feature with your caution/suggestion in mind. $\endgroup$ – sunt05 Mar 15 '18 at 14:20
  • $\begingroup$ @sunt05 how would you like to be able to use a feature like Iconize which would preserve comments etc? $\endgroup$ – Kuba Mar 15 '18 at 19:59
  • 1
    $\begingroup$ the question was raised when I was planning to switch between several functions with the same name: I wrapped the alternative functions in comment so I can easily switch between them by un/commenting. As some of the alternative functions take large space, I was trying this new feature out and then fell into the Null scenario. Now I know I should just Iconize the content in the comment rather than the whole. $\endgroup$ – sunt05 Mar 15 '18 at 20:15
  • 1
    $\begingroup$ @sunt05 To put it more explicitly, Iconize preserves expressions, not code. f@x, f[x] and (* look: *) x // f are different code, but represent the same expression, and therefore iconize to the same. $\endgroup$ – Szabolcs Mar 15 '18 at 23:08
  • $\begingroup$ @Szabolcs partially better, but saying that it preserves expressions suggests it could preserve code. It is not up to Iconize. $\endgroup$ – Kuba Mar 16 '18 at 7:26
10
$\begingroup$

Here's a follow up from Kuba's point. If you wanted to pass a few Cells around (say storing a function definition) you can cache them in an Unevaluated manner so that they auto-write when evaluated. For instance:

iconizeCell[c_CellObject] :=
 With[{cont = NotebookRead[c]},
  Iconize[Unevaluated[NotebookWrite[EvaluationNotebook[], cont]]]
  ]
(* insert a cell break here *)
iconizeCell@PreviousCell[]

enter image description here

and evaluating that gets me a Cell with:

iconizeCell[c_CellObject] :=
 With[{cont = NotebookRead[c]},
  Iconize[Unevaluated[NotebookWrite[EvaluationNotebook[], cont]]]
  ]

as its content

Note that this strategy could work for function definitions too. e.g.:

iconizeDefinition[fn_] :=

 With[{defs = Association@Language`ExtendedFullDefinition[fn][[1, 2]]},
  Iconize[
   Unevaluated[
    KeyValueMap[
      Function[#[fn] = #2],
      defs
      ];
    ]
   ]
  ]

iconizeDefinition[iconizeDefinition]

enter image description here

Then running ClearAll[iconizeDefinition] and evaluating that blob gets me my definitions back.

$\endgroup$

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.