10

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?

Daniel W
  • 3,416
  • 18
  • 31
  • @AlbertRetey There it is. I don't recall using the developer package. Thanks for finding it. – Daniel W Apr 05 '18 at 21:37
  • fwiw in 10.1 the symbol exists per ?Developer`ToList but remains unevaluated for any argument. – george2079 Apr 05 '18 at 21:52
  • 2
    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. – WReach Apr 05 '18 at 22:18
  • Fun fact that doesn't really add anything to WReach's comment in practice: We can do GeneralUtilities\PrintDefinitions[Developer`ToList](see [(1742)](https://mathematica.stackexchange.com/a/78898/11035)) to see that the current version ofToList` has only one definition yet accepts arbitrarily many arguments (including zero). – jjc385 Apr 06 '18 at 09:42

1 Answers1

11

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}

Albert Retey
  • 23,585
  • 60
  • 104
  • I'll let this stew for a few days and accept your answer. – Daniel W Apr 05 '18 at 21:39
  • 2
    great, waiting for more answers is always a good idea... – Albert Retey Apr 05 '18 at 21:41
  • 2
    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) – ktm Apr 05 '18 at 23:03
  • 3
    @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. – David Zhang Apr 06 '18 at 04:45