Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Construct an explicit position range from a span specification
ResourceFunction["SpanRange"][span,length] returns arguments for Range representing span over an expression of a given non-negative integer or infinite length.  | |
ResourceFunction["SpanRange"][span, expr] derives the length from a given expression expr.  | |
ResourceFunction["SpanRange"][span] assume length is equal to Infinity.  | 
Get a range from a span specification:
| In[1]:= | 
| Out[1]= | 
| In[2]:= | 
| Out[2]= | 
Get another range from span specification:
| In[3]:= | 
| Out[3]= | 
| In[4]:= | 
| Out[4]= | 
Negative integers encode position from the right side of the sequence:
| In[5]:= | 
| Out[5]= | 
| In[6]:= | 
| Out[6]= | 
| In[7]:= | 
| Out[7]= | 
Negative step:
| In[8]:= | 
| Out[8]= | 
| In[9]:= | 
| Out[9]= | 
All in the left or right span position maximizes number of elements spanned by choosing one of two boundary indices:
| In[10]:= | 
| Out[10]= | 
| In[11]:= | 
| Out[11]= | 
| In[12]:= | 
| Out[12]= | 
| In[13]:= | 
| Out[13]= | 
All in the step position of a span chooses either to increment or decrement by 1:
| In[14]:= | 
| Out[14]= | 
| In[15]:= | 
| Out[15]= | 
| In[16]:= | 
| Out[16]= | 
| In[17]:= | 
| Out[17]= | 
| In[18]:= | 
| Out[18]= | 
UpTo is interpreted as an upper bound for the output range size:
| In[19]:= | 
| Out[19]= | 
When necessary, a negative step is used to satisfy the input:
| In[20]:= | 
| Out[20]= | 
A positive step is used when possible:
| In[21]:= | 
| Out[21]= | 
| In[22]:= | 
| Out[22]= | 
| In[23]:= | 
| Out[23]= | 
| In[24]:= | 
| Out[24]= | 
| In[25]:= | 
| Out[25]= | 
| In[26]:= | 
| Out[26]= | 
| In[27]:= | 
| Out[27]= | 
UpTo with negative size. Use Quiet to avoid messages from UpTo:
| In[28]:= | 
| Out[28]= | 
| In[29]:= | 
| Out[29]= | 
UpTo[0] generates different empty ranges:
| In[30]:= | 
| Out[30]= | 
| In[31]:= | 
| Out[31]= | 
| In[32]:= | 
| Out[32]= | 
| In[33]:= | 
| Out[33]= | 
| In[34]:= | 
| Out[34]= | 
0 is allowed as left or right span position in order to index a Head of an expression:
| In[35]:= | 
| Out[35]= | 
| In[36]:= | 
| Out[36]= | 
| In[37]:= | 
| Out[37]= | 
| In[38]:= | 
| Out[38]= | 
0 length expressions generate empty ranges:
| In[39]:= | 
| Out[39]= | 
| In[40]:= | 
| Out[40]= | 
| In[41]:= | 
| Out[41]= | 
| In[42]:= | 
| Out[42]= | 
Infinity length may generate infinite ranges:
| In[43]:= | 
| Out[43]= | 
| In[44]:= | 
| Out[44]= | 
UpTo[Infinity] is the same as All:
| In[45]:= | 
| Out[45]= | 
| In[46]:= | 
| Out[46]= | 
UpTo at both ends of the span chooses the minimum constraint:
| In[47]:= | 
| Out[47]= | 
| In[48]:= | 
| Out[48]= | 
| In[49]:= | 
| Out[49]= | 
| In[50]:= | 
| Out[50]= | 
Inputs which are not an explicit Span:
| In[51]:= | 
| Out[51]= | 
| In[52]:= | 
| Out[52]= | 
| In[53]:= | 
| Out[53]= | 
| In[54]:= | 
| Out[54]= | 
| In[55]:= | 
| Out[55]= | 
| In[56]:= | 
| Out[56]= | 
| In[57]:= | 
| Out[57]= | 
| In[58]:= | 
| Out[58]= | 
Compose multiple spans together:
| In[59]:= | 
| Out[59]= | 
| In[60]:= | 
| Out[60]= | 
| In[61]:= | 
| Out[61]= | 
| In[62]:= | 
| Out[62]= | 
Use SpanRange with Range to generate explicit part numbers for various computations:
| In[63]:= | 
| In[64]:= | 
| In[65]:= | 
| In[66]:= | 
| Out[66]= | 
| In[67]:= | 
| Out[67]= | 
| In[68]:= | 
| Out[68]= | 
| In[69]:= | 
| Out[69]= | 
| In[70]:= | 
| Out[70]= | 
| In[71]:= | 
| Out[71]= | 
| In[72]:= | 
| Out[72]= | 
| In[73]:= | 
| Out[73]= | 
| In[74]:= | 
| Out[74]= | 
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License