Function Repository Resource:

SpanRange (1.2.0) current version: 1.3.0 »

Source Notebook

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

Any argument of span 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.
Spans of the form from;;UpTo[n] and UpTo[n];;to 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.

Examples

Basic Examples (2) 

Get a range from span specification:

In[1]:=
ResourceFunction["SpanRange"][3 ;; 7, 10]
Out[1]=
In[2]:=
Range @@ %
Out[2]=

Get another range from span specification:

In[3]:=
ResourceFunction["SpanRange"][2 ;; 9 ;; 2, 10]
Out[3]=
In[4]:=
Range @@ %
Out[4]=

Scope (8) 

Negative integers:

In[5]:=
ResourceFunction["SpanRange"][3 ;; -3, 10]
Out[5]=
In[6]:=
ResourceFunction["SpanRange"][-7 ;; 7, 10]
Out[6]=
In[7]:=
ResourceFunction["SpanRange"][-6 ;; -2, 10]
Out[7]=

Negative step:

In[8]:=
ResourceFunction["SpanRange"][7 ;; 3 ;; -1, 10]
Out[8]=
In[9]:=
ResourceFunction["SpanRange"][9 ;; 2 ;; -2, 10]
Out[9]=

Use of All for the min and max:

In[10]:=
ResourceFunction["SpanRange"][All ;; 3, 10]
Out[10]=
In[11]:=
ResourceFunction["SpanRange"][All ;; 3 ;; -1, 10]
Out[11]=
In[12]:=
ResourceFunction["SpanRange"][3 ;; All, 10]
Out[12]=
In[13]:=
ResourceFunction["SpanRange"][3 ;; All ;; -1, 10]
Out[13]=

Use of All for the step:

In[14]:=
ResourceFunction["SpanRange"][3 ;; 7 ;; All, 10]
Out[14]=
In[15]:=
ResourceFunction["SpanRange"][7 ;; 3 ;; All, 10]
Out[15]=
In[16]:=
ResourceFunction["SpanRange"][All ;; 7 ;; All, 10]
Out[16]=
In[17]:=
ResourceFunction["SpanRange"][All ;; 3 ;; All, 10]
Out[17]=
In[18]:=
ResourceFunction["SpanRange"][10 ;; All ;; All, 10]
Out[18]=

Use of single UpTo:

In[19]:=
ResourceFunction["SpanRange"][4 ;; UpTo[3], 10]
Out[19]=
In[20]:=
ResourceFunction["SpanRange"][UpTo[3] ;; 2 ;; All, 10]
Out[20]=
In[21]:=
ResourceFunction["SpanRange"][All ;; UpTo[3], 10]
Out[21]=
In[22]:=
ResourceFunction["SpanRange"][UpTo[3] ;; All ;; All, 10]
Out[22]=
In[23]:=
ResourceFunction["SpanRange"][4 ;; UpTo[10] ;; All, 10]
Out[23]=
In[24]:=
ResourceFunction["SpanRange"][6 ;; UpTo[10] ;; All, 10]
Out[24]=
In[25]:=
ResourceFunction["SpanRange"][UpTo[4] ;; 6 ;; All, 10]
Out[25]=
In[26]:=
ResourceFunction["SpanRange"][UpTo[10] ;; 4 ;; All, 10]
Out[26]=

UpTo[0] generates different empty ranges:

In[27]:=
ResourceFunction["SpanRange"][UpTo[0] ;; ;; 1, 10]
Out[27]=
In[28]:=
ResourceFunction["SpanRange"][UpTo[0] ;; ;; -1, 10]
Out[28]=
In[29]:=
ResourceFunction["SpanRange"][;; UpTo[0] ;; 1, 10]
Out[29]=
In[30]:=
ResourceFunction["SpanRange"][;; UpTo[0] ;; -1, 10]
Out[30]=

UpTo[Infinity] is the same as All:

In[31]:=
ResourceFunction["SpanRange"][UpTo[Infinity] ;; 4 ;; All, 10]
Out[31]=
In[32]:=
ResourceFunction["SpanRange"][4 ;; UpTo[Infinity] ;; All, 10]
Out[32]=

Use UpTo at both ends of the span:

In[33]:=
ResourceFunction["SpanRange"][UpTo[7] ;; UpTo[3], 10]
Out[33]=
In[34]:=
ResourceFunction["SpanRange"][UpTo[3] ;; UpTo[7] ;; All, 10]
Out[34]=
In[35]:=
ResourceFunction["SpanRange"][UpTo[3] ;; UpTo[7] ;; 1, 10]
Out[35]=
In[36]:=
ResourceFunction["SpanRange"][UpTo[3] ;; UpTo[7] ;; -1, 10]
Out[36]=

Possible Issues (1) 

Invalid spans result in Failure:

In[37]:=
ResourceFunction["SpanRange"][3 ;; 15, 10]
Out[37]=
In[38]:=
ResourceFunction["SpanRange"][15 ;; 1 ;; All, 10]
Out[38]=

Requirements

Wolfram Language 12.3 (May 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

License Information