How can symbols from assignments, ie left hand side in expressions Set
and SetDelayed
be matched after importing using NotebookImport
?
Consider a minimal notebook with these mixed definitions:
selPos = Select[#>0&];
selNeg[x_]:= Select[x<0];
selNegPair[x_, y_] := Select[x < 0 && y < 0];
There may be more involved definitions with SubValues
, UpValues
etc but these are the main cases to solve for. Note the typical use of CompoundExpression
.
After importing via NotebookImport
using the "Input" option:
exampleInput = NotebookImport["Example.nb","Input"]
{HoldComplete[selPos=Select[#1>0&];],HoldComplete[selNeg[x_]:=Select[x<0];],HoldComplete[selNegPair[x_,y_]:=Select[x<0&&y<0];]}
Note the assignments are wrapped in HoldComplete
(rather than HoldPattern
output by OwnValues
- is there an option for NotebookImport
that will preserve the OwnValues
?)
Is there an easier way to extract these symbols than to use 2 replaces and release hold? first to match the left hand of Set
or SetDelayed
, and second to extract the symbol from the HoldComplete
, it seems to work only if HoldForm
is used in the replacement rule, and finally hold is released:
exampleInput /. {HoldPattern[Set[f_,_]]:> f,HoldPattern[SetDelayed[f_,_]]:> f}/. {HoldComplete[g_[___];]:> HoldForm[g],HoldComplete[g_;]:> HoldForm[g]} // Map[ReleaseHold]
Gives the desired symbols - but only when evaluated in a separate kernel, otherwise releasing the hold will replace the symbols by their values:
{selPos,selNeg,selNegPair}
Is there an easier way to accomplish the above?
ToString
and importing the target notebooks usingNotebookImport[...,"Input"->"InputText"]
$\endgroup$ – alancalvitti May 20 '20 at 17:44