Re: Part // interesting undocumented feature
- To: mathgroup at smc.vnet.net
- Subject: [mg122699] Re: Part // interesting undocumented feature
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sun, 6 Nov 2011 05:57:11 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <j930uu$a9g$1@smc.vnet.net>
On 05/11/2011 09:51, Scot T. Martin wrote:
> A colleague new to Mathematica showed me something that I thought should cause an error:
>
> Part[data[[All,1]]]
>
> Of course, I thought the form should just be "data[[All,1]".
>
> It turns out that there is form of Part that works like an identity function:
>
> In[1]:= Part[5]
> Out[1]= 5
>
> Just out of a Mathematica trivia challenge question, can anyone think of an example when this undocumented syntax of Part[ ] could possibly be of use or why the programmers have it as an acceptable syntax?
>
>
>
I was not aware of this, but it makes sense.
It is the limiting case of the construction Part[expr,i,j,k....] - the
case where no part specifiers are supplied.
Usually this sort of feature is included to avoid having to perform
special tests for end conditions. For example:
In[673]:= pp={1,2};
expr={{a,b},{c,d}};
Part[expr,Sequence@@pp]
Out[675]= b
In[677]:= pp={1};
In[678]:= Part[expr,Sequence@@pp]
Out[678]= {a, b}
In[680]:= pp={};
In[681]:= Part[expr,Sequence@@pp]
Out[681]= {{a, b}, {c, d}}
Thus the case pp={} does not need to be handled as a special case.
David Bailey
http://www.dbaileyconsultancy.co.uk