Function Repository Resource:

# SpanRange

Construct an explicit position range from a span specification

Contributed by: Nikolay Murzin
 ResourceFunction["SpanRange"][span,len] returns arguments for a Range representing span over an expression of length len. ResourceFunction["SpanRange"][span, expr] derives the length from a given expression expr.

## Details

span is a Span taking a general form of min;;max;;step.
Any argument of span (min, max or step) can be All, in which case it is interpreted as returning the largest possible number of elements consistent with any possible value of that argument.
The first two integers of a span are considered ordinals indexing parts of an expression with a given length, with 0 pointing to the Head of an expression and negative integers indexing from the end.
Span specifications of the form min;;UpTo[n] and UpTo[n];;max will result in a range with a maximum of n elements (n is treated as a cardinal).
UpTo[n];;UpTo[m] will result in a range with a maximum of Min[n,m] elements.
UpTo[Infinity] is the same as All.
Some spans including 0 are invalid when used as a Part specification but are still supported in ResourceFunction["SpanRange"].

## Examples

### Basic Examples (2)

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]=

### Scope (9)

Negative integers:

 In[5]:=
 Out[5]=
 In[6]:=
 Out[6]=
 In[7]:=
 Out[7]=

Negative step:

 In[8]:=
 Out[8]=
 In[9]:=
 Out[9]=

Use of All for the min and max:

 In[10]:=
 Out[10]=
 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=
 In[13]:=
 Out[13]=

Use of All for the step:

 In[14]:=
 Out[14]=
 In[15]:=
 Out[15]=
 In[16]:=
 Out[16]=
 In[17]:=
 Out[17]=
 In[18]:=
 Out[18]=

Use of single UpTo:

 In[19]:=
 Out[19]=
 In[20]:=
 Out[20]=
 In[21]:=
 Out[21]=
 In[22]:=
 Out[22]=
 In[23]:=
 Out[23]=
 In[24]:=
 Out[24]=
 In[25]:=
 Out[25]=
 In[26]:=
 Out[26]=

UpTo[0] generates different empty ranges:

 In[27]:=
 Out[27]=
 In[28]:=
 Out[28]=
 In[29]:=
 Out[29]=
 In[30]:=
 Out[30]=

0 is allowed as the min or the max (which is indexing the Head of an expression):

 In[31]:=
 Out[31]=
 In[32]:=
 Out[32]=
 In[33]:=
 Out[33]=

UpTo[Infinity] is the same as All:

 In[34]:=
 Out[34]=
 In[35]:=
 Out[35]=

Use UpTo at both ends of the span:

 In[36]:=
 Out[36]=
 In[37]:=
 Out[37]=
 In[38]:=
 Out[38]=
 In[39]:=
 Out[39]=

### Applications (1)

Use SpanRange with Range to generate explicit part numbers for various computations:

 In[40]:=
 In[41]:=
 In[42]:=
 In[43]:=
 Out[43]=
 In[44]:=
 Out[44]=
 In[45]:=
 Out[45]=
 In[46]:=
 Out[46]=
 In[47]:=
 Out[47]=
 In[48]:=
 Out[48]=
 In[49]:=
 Out[49]=

### Possible Issues (1)

Invalid spans indexing beyond a given length result in Failure:

 In[50]:=
 Out[50]=
 In[51]:=
 Out[51]=
 In[52]:=
 Out[52]=
 In[53]:=
 Out[53]=

## Requirements

Wolfram Language 13.0 (December 2021) or above

## Version History

• 1.3.0 – 08 March 2024
• 1.2.0 – 27 September 2023
• 1.1.0 – 10 May 2022
• 1.0.0 – 18 April 2022