8
$\begingroup$

I am writing my code documentation and need to list all the Mathematica built in functions I have used - explicitly written functions only. Is there a way to extract a list of all built in functions that I have used in my code?

For example

Insert[{a, b, c}, x, 3]
a=1
Style["this text",Bold]
Range[1,20]
b=3
a*b

I would require a list containing Insert, Style and Range.

$\endgroup$
3
  • $\begingroup$ You might also anticipate getting List, Set, and Times, since a=1 for example is equivalent to Set[a,1]. $\endgroup$ May 21, 2014 at 10:15
  • $\begingroup$ As noticed in comments you should probably explain what do you mean. 1) only explicitely written functions? 2) what is function $\endgroup$
    – Kuba
    May 21, 2014 at 10:49
  • $\begingroup$ @Öskå must be v9, Kuba's answer worked for the OP. $\endgroup$ May 21, 2014 at 11:05

3 Answers 3

6
$\begingroup$

Perhaps something like this:

systemSymbolsInNotebook[file_String] := Module[{n, cells, exprs, syms},
  n = NotebookOpen[file];
  SelectionMove[n, All, Notebook];
  cells = NotebookRead[n];
  NotebookClose[n];
  exprs = MakeExpression[First@#] & @@@ cells;
  syms = Cases[exprs, s_Symbol :> SymbolName@Unevaluated@s, {3, -1}, Heads -> True];
  Intersection[syms, Names["System`*"]]]

systemSymbolsInNotebook["test.nb"]
(* {"Bold", "Insert", "List", "Range", "Set", "Style", "Times"} *)
$\endgroup$
4
  • 1
    $\begingroup$ Even if OP agrees with non explicitly used functions as Times or Set I don't think Bold should appear there. $\endgroup$
    – Kuba
    May 21, 2014 at 10:22
  • $\begingroup$ That's nice - +1 $\endgroup$
    – ciao
    May 21, 2014 at 10:25
  • $\begingroup$ This doesn't seem to work. I had a notebook with NotebookPut, Get and some other things and the results I got from using this code were {"CellChangeTimes", "List", "Rule", "TextCell"} $\endgroup$
    – lara
    May 21, 2014 at 10:37
  • $\begingroup$ @LaraJordan, ah okay. I only tested it with the simple example in the question. $\endgroup$ May 21, 2014 at 10:59
7
$\begingroup$
functions[nb_] :=
 DeleteDuplicates[
  Join @@
     Extract[
      #
      ,
      Position[
       #
       ,
       x_Symbol /; Context[x] == "System`"
       ]
      ,
      HoldComplete
      ] &[
   ToExpression[Unevaluated[#], StandardForm, 
      HoldComplete] & /@ (NotebookRead@Cells[nb, GeneratedCell-> False])[[All, 1]]]
  ]

For your specific case, where you don't want symbols that never occur as the head of an expression, you could use the following.

functionsSpecific[nb_] :=
 DeleteDuplicates[
  Join @@
     Extract[
      #
      ,
      Position[
       #
       ,
       x_Symbol /; 
        Context[x] == "System`" &&
         MatchQ[SyntaxInformation[x], _[___, 
           "ArgumentsPattern" ->  _, ___]]
       ]
      ,
      HoldComplete
      ] &[
   ToExpression[Unevaluated[#], StandardForm, 
      HoldComplete] & /@ (NotebookRead@
       Cells[nb, GeneratedCell -> False])[[All, 1]]
   ]
  ]

For the notebook in which I am working, which contains a lot of functions, we then have

Complement[functions[nb], functionsSpecific[nb]]
HoldComplete[All,Bold,False,GeneratedCell,Integer,Italic,Null]

So it seems the functions that are excluded by functionsSpecific are reasonable.

$\endgroup$
4
  • $\begingroup$ Nice. I forgot about Cells... $\endgroup$ May 21, 2014 at 10:26
  • $\begingroup$ So this is a custom function. Do I put the filepath in place of nb_ ? When I do that I get an error message ToExpression::sntx: Invalid syntax in or before " \!(StandardForm`\\ecsdbn-srv-02\lara_jordan$\Cleaner Kumba\Cleaner Kumba Tool Non-Encoded Notebooks\Initialization.nb) " and some other error messages. $\endgroup$
    – lara
    May 21, 2014 at 10:39
  • 1
    $\begingroup$ @LaraJordan use EvaluationNotebook or other NotebookObject. You can set nb = EvaluationNotebook[] and use it on nb somewhere else so it will not interfere. $\endgroup$
    – Kuba
    May 21, 2014 at 10:42
  • $\begingroup$ Great :) can't +1 again :/ $\endgroup$
    – Kuba
    May 21, 2014 at 12:06
6
$\begingroup$

Evaluate this at the bottom of the notebook of interest:

Composition[
  Select[#, StringMatchQ[Evaluate[Symbol[#]]::usage, 
                         "\!\(\*RowBox[{\"" <> # <> "\", \"[\"" ~~ __] &] &,
  Intersection[#, Names["System`*"]] &,
  Union@Cases[#, _String, ∞, Heads -> True] &,
  First /@ # &,
  NotebookRead /@ # &,
  Most
  ]@Cells[EvaluationNotebook[], GeneratedCell -> False]
{"Insert", "Range", "Style"}

To find what I've assumed is a function, I've used Evaluate[Symbol[#]]::usage which should show something like

Plot::usage
Plot[f,{x,...

so I'm looking for name ~~ [ pattern. Unfortunately, it is not bulletproof, there are symbols with different usage structure:

Quiet@Select[Names["System`*"], StringMatchQ[Evaluate[Symbol[#]]::usage, 
"\!\(\*RowBox[{StyleBox[" ~~ __] &]
{AddTo,Condition,Decrement,Derivative,Divide,DivideBy,Dot,Equal,Factorial,Factorial2,Greater,GreaterEqual,Increment,Less,LessEqual,MessageName,NonCommutativeMultiply,Optional,Part,Pattern,PatternTest,Plus,Power,Put,PutAppend,Repeated,RepeatedNull,ReplaceAll,ReplaceRepeated,Rule,RuleDelayed,SameQ,Set,SetDelayed,Span,StringJoin,Subtract,SubtractFrom,TagSet,TagSetDelayed,TagUnset,Times,TimesBy,Unequal,UnsameQ,Unset,UpSet,UpSetDelayed}

some of them may be considered functions, some not. So it's really hard to make a proper answer without very precise definition of function.

$\endgroup$
14
  • $\begingroup$ Cells is new in v9. Same goes for Jacob. $\endgroup$
    – Öskå
    May 21, 2014 at 10:31
  • $\begingroup$ @Kuba it may be nice to adapt the second piece of code in my question list of dangerous functions then. My answer is basically an adaptation of the first code block. $\endgroup$ May 21, 2014 at 10:33
  • $\begingroup$ @JacobAkkerboom The dangerous functions list does not care if there is Bold or not, it will be dropped later anyway, that's why it's a little different problem. $\endgroup$
    – Kuba
    May 21, 2014 at 10:36
  • $\begingroup$ @Kuba yeah, thank you for reminding me that I don't really answer the question here :P. But I don't really see what symbols we should return then, short of making an explicit list of functions we don't want, like Bold. By the way how do you think symbols like $MachineEpsilon should be handled? That one does have a usage message, but your code does not find it. $\endgroup$ May 21, 2014 at 10:41
  • $\begingroup$ @JacobAkkerboom I've assumed that we need only explicitely stated functions and function is a symbol with DownValue which is a tricky part since those are hidden. $\endgroup$
    – Kuba
    May 21, 2014 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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