Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is DropWhile in Mathematica?

Mathematica 6 added TakeWhile, which has the syntax:

TakeWhile[list, crit] gives elements ei from the beginning of list, continuing so long as crit[ei] is True.

There is however no corresponding "DropWhile" function. One can construct DropWhile using LengthWhile and Drop, but it almost seems as though one is discouraged from using DropWhile. Why is this?

To clarify, I am not asking for a way to implement this function. Rather: why is it not already present? It seems to me that there must be a reason for its absence other than an oversight, or it would have been corrected by now. Is there something inefficient, undesirable, or superfluous about DropWhile?


There appears to be some ambiguity about the function of DropWhile, so here is an example:

DropWhile = Drop[#, LengthWhile[#, #2]] &;

DropWhile[{1,2,3,4,5}, # <= 3 &]
Out= {4, 5}
like image 608
Mr.Wizard Avatar asked Apr 07 '11 08:04

Mr.Wizard


2 Answers

Just a blind guess.

There are a lot list operations that could take a while criteria. For example:

Total..While  
Accumulate..While  
Mean..While  
Map..While  
Etc..While

They are not difficult to construct, anyway.

I think those are not included just because the number of "primitive" functions is already growing too long, and the criteria of "is it frequently needed and difficult to implement with good performance by the user?" is prevailing in those cases.

like image 140
Dr. belisarius Avatar answered Oct 17 '22 00:10

Dr. belisarius


The ubiquitous Lists in Mathematica are fixed length vectors, and when they are of a machine numbers it is a packed array.

Thus the natural functions for a recursively defined linked list (e.g. in Lisp or Haskell) are not the primary tools in Mathematica.

So I am inclined to think this explains why Wolfram did not fill out its repertoire of manipulation functions.

like image 3
Chris Kuklewicz Avatar answered Oct 17 '22 00:10

Chris Kuklewicz