10
$\begingroup$

I am trying to revive some code from 2015 (version 10?) which uses the function ToList. I can not find it defined in any notebook on my system (in fact, it only appears in this one notebook). I had loaded the GeneralUtilities package in this notebook, so maybe ToList was defined in a previous version of that. From the context of its usage, it appears to force its arguments to be a list, but only if the argument is not already a list, like:

ToList[x_List] := x;
ToList[x__] := {x};

Does anybody remember this function?

$\endgroup$
  • $\begingroup$ @AlbertRetey There it is. I don't recall using the developer package. Thanks for finding it. $\endgroup$ – Daniel W Apr 5 '18 at 21:37
  • $\begingroup$ fwiw in 10.1 the symbol exists per ?Developer`ToList but remains unevaluated for any argument. $\endgroup$ – george2079 Apr 5 '18 at 21:52
  • 2
    $\begingroup$ GeneralUtilities`ToList existed in 10.0 and 10.1, but was moved into the Developer context in 10.2. The GeneralUtilities version only accepted exactly one argument, but the Developer version accepts any number of arguments (including zero) and concatenates any explicit list arguments. $\endgroup$ – WReach Apr 5 '18 at 22:18
  • $\begingroup$ Fun fact that doesn't really add anything to WReach's comment in practice: We can do GeneralUtilities`PrintDefinitions[Developer`ToList] (see (1742)) to see that the current version of ToList has only one definition yet accepts arbitrarily many arguments (including zero). $\endgroup$ – jjc385 Apr 6 '18 at 9:42
11
$\begingroup$

The function is still there (version 11.3). It lives in the Developer` context. If you don't know their context you can find functions with something like:

?*`ToList

or if they are in deeper nested contexts (e.g. private ones) with:

?*`*`To*

In that case the first one returns Developer`ToList. Of course that only works for those builtin symbols which are pre-loaded without an explict Get or Needs.

There is no documentation available, but some tests suggest that what you are guessing is correct:

Developer`ToList[x]

{x}

Developer`ToList[{x}]

{x}

Developer`ToList[{1,2,3}]

{1,2,3}

Developer`ToList[1,2,3]

{1,2,3}

| improve this answer | |
$\endgroup$
  • $\begingroup$ I'll let this stew for a few days and accept your answer. $\endgroup$ – Daniel W Apr 5 '18 at 21:39
  • 2
    $\begingroup$ great, waiting for more answers is always a good idea... $\endgroup$ – Albert Retey Apr 5 '18 at 21:41
  • 1
    $\begingroup$ Any idea what the purpose of this function is? In what case would it be used instead of, say, Apply[List,f[1,2,3]], or just List[1,2,3]? (the reason this is nonobvious is maybe why it's not a global symbol) $\endgroup$ – ktm Apr 5 '18 at 23:03
  • 2
    $\begingroup$ @user6014 It might be useful in the implementation of functions like Max that take either a variable number of arguments (i.e., Max[1, 2, 3]) or a list of arguments (i.e., Max[{1, 2, 3}]). Pass the sequence of input arguments to ToList, and then either way, you've got a list you can process. $\endgroup$ – David Zhang Apr 6 '18 at 4:45

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.