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]:= | ![ResourceFunction["SpanRange"][3 ;; 7, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/0941e6c9eb5fc740.png) | 
| Out[1]= |  | 
| In[2]:= |  | 
| Out[2]= |  | 
Get another range from span specification:
| In[3]:= | ![ResourceFunction["SpanRange"][2 ;; 9 ;; 2, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/33f31492e4e1a3a4.png) | 
| Out[3]= |  | 
| In[4]:= |  | 
| Out[4]= |  | 
Negative integers encode position from the right side of the sequence:
| In[5]:= | ![ResourceFunction["SpanRange"][3 ;; -3, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/72b27edc9d9269fe.png) | 
| Out[5]= |  | 
| In[6]:= | ![ResourceFunction["SpanRange"][-7 ;; 7, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/3e0891952c434e44.png) | 
| Out[6]= |  | 
| In[7]:= | ![ResourceFunction["SpanRange"][-6 ;; -2, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/468f3d8738c70d2b.png) | 
| Out[7]= |  | 
Negative step:
| In[8]:= | ![ResourceFunction["SpanRange"][7 ;; 3 ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/297c4a6e87e7c77c.png) | 
| Out[8]= |  | 
| In[9]:= | ![ResourceFunction["SpanRange"][9 ;; 2 ;; -2, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/60d6de32295e78af.png) | 
| Out[9]= |  | 
All in the left or right span position maximizes number of elements spanned by choosing one of two boundary indices:
| In[10]:= | ![ResourceFunction["SpanRange"][All ;; 3, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/3170460276f46ed9.png) | 
| Out[10]= |  | 
| In[11]:= | ![ResourceFunction["SpanRange"][All ;; 3 ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/065d7d1ebde188c2.png) | 
| Out[11]= |  | 
| In[12]:= | ![ResourceFunction["SpanRange"][3 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/11d7cf30a52d9bf2.png) | 
| Out[12]= |  | 
| In[13]:= | ![ResourceFunction["SpanRange"][3 ;; All ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/042f4d7d3543be83.png) | 
| Out[13]= |  | 
All in the step position of a span chooses either to increment or decrement by 1:
| In[14]:= | ![ResourceFunction["SpanRange"][3 ;; 7 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/103324b3ab38f703.png) | 
| Out[14]= |  | 
| In[15]:= | ![ResourceFunction["SpanRange"][7 ;; 3 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/55f58342a29c6aaf.png) | 
| Out[15]= |  | 
| In[16]:= | ![ResourceFunction["SpanRange"][All ;; 7 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/64ed7582743f660e.png) | 
| Out[16]= |  | 
| In[17]:= | ![ResourceFunction["SpanRange"][All ;; 3 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/321f4f6e5e996858.png) | 
| Out[17]= |  | 
| In[18]:= | ![ResourceFunction["SpanRange"][10 ;; All ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/6d27e0c9af40baec.png) | 
| Out[18]= |  | 
UpTo is interpreted as an upper bound for the output range size:
| In[19]:= | ![ResourceFunction["SpanRange"][4 ;; UpTo[3], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/58d88102ca545dda.png) | 
| Out[19]= |  | 
When necessary, a negative step is used to satisfy the input:
| In[20]:= | ![ResourceFunction["SpanRange"][UpTo[3] ;; 2 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/4eb51f60602ffc78.png) | 
| Out[20]= |  | 
A positive step is used when possible:
| In[21]:= | ![ResourceFunction["SpanRange"][UpTo[3] ;; 5 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/1b241f6afc36bc42.png) | 
| Out[21]= |  | 
| In[22]:= | ![ResourceFunction["SpanRange"][All ;; UpTo[3], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/104f9d1829daa5b4.png) | 
| Out[22]= |  | 
| In[23]:= | ![ResourceFunction["SpanRange"][UpTo[3] ;; All ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/7f99bb84bb61ad75.png) | 
| Out[23]= |  | 
| In[24]:= | ![ResourceFunction["SpanRange"][4 ;; UpTo[10] ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/521bbdd468b022b1.png) | 
| Out[24]= |  | 
| In[25]:= | ![ResourceFunction["SpanRange"][6 ;; UpTo[10] ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/565c05986d54b107.png) | 
| Out[25]= |  | 
| In[26]:= | ![ResourceFunction["SpanRange"][UpTo[4] ;; 6 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/268fe8c3962eb5e4.png) | 
| Out[26]= |  | 
| In[27]:= | ![ResourceFunction["SpanRange"][UpTo[10] ;; 4 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/122b59b2182fbfd3.png) | 
| Out[27]= |  | 
UpTo with negative size. Use Quiet to avoid messages from UpTo:
| In[28]:= | ![ResourceFunction["SpanRange"][UpTo[-2] ;;, 10] // Quiet](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/745d19ef6757cee5.png) | 
| Out[28]= |  | 
| In[29]:= | ![ResourceFunction["SpanRange"][;; UpTo[-2], 10] // Quiet](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/605cb95930cc40e5.png) | 
| Out[29]= |  | 
UpTo[0] generates different empty ranges:
| In[30]:= | ![ResourceFunction["SpanRange"][UpTo[0] ;;, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/229cbac739b4da15.png) | 
| Out[30]= |  | 
| In[31]:= | ![ResourceFunction["SpanRange"][UpTo[0] ;; ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/65b6995a00a5db6b.png) | 
| Out[31]= |  | 
| In[32]:= | ![ResourceFunction["SpanRange"][;; UpTo[0], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/38526cd1df84f92f.png) | 
| Out[32]= |  | 
| In[33]:= | ![ResourceFunction["SpanRange"][;; UpTo[0] ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/2208974c7bfa2b19.png) | 
| Out[33]= |  | 
| In[34]:= | ![ResourceFunction["SpanRange"][All ;; UpTo[0] ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/1dffff60736518c3.png) | 
| Out[34]= |  | 
0 is allowed as left or right span position in order to index a Head of an expression:
| In[35]:= | ![ResourceFunction["SpanRange"][0 ;;, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/3ecb872ffa442f50.png) | 
| Out[35]= |  | 
| In[36]:= | ![ResourceFunction["SpanRange"][All ;; 0 ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/761c9890bf4e3658.png) | 
| Out[36]= |  | 
| In[37]:= | ![ResourceFunction["SpanRange"][0 ;; UpTo[3], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/710dbb9a11ddee0c.png) | 
| Out[37]= |  | 
| In[38]:= | ![ResourceFunction["SpanRange"][0 ;;, 0]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/54ce1d1682c29ed1.png) | 
| Out[38]= |  | 
0 length expressions generate empty ranges:
| In[39]:= | ![ResourceFunction["SpanRange"][UpTo[10] ;;, 0]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/68f77e8bcf473be6.png) | 
| Out[39]= |  | 
| In[40]:= | ![ResourceFunction["SpanRange"][All ;; UpTo[10], 0]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/5492648ffb5f3fe2.png) | 
| Out[40]= |  | 
| In[41]:= | ![ResourceFunction["SpanRange"][All ;; UpTo[10] ;; -1, 0]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/26963c5437f989b5.png) | 
| Out[41]= |  | 
| In[42]:= | ![ResourceFunction["SpanRange"][UpTo[10] ;; ;; -1, 0]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/431f6fe7ac9ba119.png) | 
| Out[42]= |  | 
Infinity length may generate infinite ranges:
| In[43]:= | ![ResourceFunction["SpanRange"][;; , Infinity]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/3ccf28854f6bcd69.png) | 
| Out[43]= |  | 
| In[44]:= | ![ResourceFunction["SpanRange"][10 ;; ;; 2, Infinity]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/2b861d71b0bd2448.png) | 
| Out[44]= |  | 
UpTo[Infinity] is the same as All:
| In[45]:= | ![ResourceFunction["SpanRange"][UpTo[Infinity] ;; 4 ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/2750de50a4f81320.png) | 
| Out[45]= |  | 
| In[46]:= | ![ResourceFunction["SpanRange"][4 ;; UpTo[Infinity] ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/7eace9a963bde974.png) | 
| Out[46]= |  | 
UpTo at both ends of the span chooses the minimum constraint:
| In[47]:= | ![ResourceFunction["SpanRange"][UpTo[7] ;; UpTo[3], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/0cce9a77b00e8a36.png) | 
| Out[47]= |  | 
| In[48]:= | ![ResourceFunction["SpanRange"][UpTo[3] ;; UpTo[7] ;; All, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/74d18cffdb405df8.png) | 
| Out[48]= |  | 
| In[49]:= | ![ResourceFunction["SpanRange"][UpTo[3] ;; UpTo[7] ;; 1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/43f9c72eced6d105.png) | 
| Out[49]= |  | 
| In[50]:= | ![ResourceFunction["SpanRange"][UpTo[3] ;; UpTo[7] ;; -1, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/1f4f454d6c6f37df.png) | 
| Out[50]= |  | 
Inputs which are not an explicit Span:
| In[51]:= | ![ResourceFunction["SpanRange"][UpTo[7], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/58b147f165f0e31b.png) | 
| Out[51]= |  | 
| In[52]:= | ![ResourceFunction["SpanRange"][-UpTo[7], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/190689616adfed11.png) | 
| Out[52]= |  | 
| In[53]:= | ![ResourceFunction["SpanRange"][7, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/6c8d94f21821e0e1.png) | 
| Out[53]= |  | 
| In[54]:= | ![ResourceFunction["SpanRange"][{7}, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/227d9176adee8794.png) | 
| Out[54]= |  | 
| In[55]:= | ![ResourceFunction["SpanRange"][-7, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/492dff1931186e35.png) | 
| Out[55]= |  | 
| In[56]:= | ![ResourceFunction["SpanRange"][{-7}, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/18f6387042efce49.png) | 
| Out[56]= |  | 
| In[57]:= | ![ResourceFunction["SpanRange"][{-7, 9}, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/12f06255c29aa13b.png) | 
| Out[57]= |  | 
| In[58]:= | ![ResourceFunction["SpanRange"][{-7, 9, 2}, 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/369e7e0f611cdf47.png) | 
| Out[58]= |  | 
Compose multiple spans together:
| In[59]:= | ![ResourceFunction["SpanRange"][(2 ;;)/*(-2 ;; ;; -1), 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/577a18bff3672af1.png) | 
| Out[59]= |  | 
| In[60]:= | ![ResourceFunction["SpanRange"][(2 ;; ;; 3)/*UpTo[3], 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/28baaec09d755d53.png) | 
| Out[60]= |  | 
| In[61]:= | ![ResourceFunction["SpanRange"][(2 ;; ;; 3)/*(UpTo[3] ;; ;; -2), 10]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/206d820106f35cfb.png) | 
| Out[61]= |  | 
| In[62]:= | ![ResourceFunction[
 "SpanRange"][(2 ;; ;; 2)/*(;; UpTo[3] ;; 3), Infinity]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/0085d08e2aa96073.png) | 
| Out[62]= |  | 
Use SpanRange with Range to generate explicit part numbers for various computations:
| In[63]:= | ![extract[expr_, span_] := Enclose@Extract[expr, List /@ Range @@ Confirm@ResourceFunction["SpanRange"][span, expr]]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/78917e4c3d98db78.png) | 
| In[64]:= | ![mapAt[f_, expr_, span_] := Enclose@MapAt[f, expr, List /@ Range @@ Confirm@ResourceFunction["SpanRange"][span, expr]]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/4729e49898433584.png) | 
| In[65]:= | ![replacePart[expr_, span_ -> new_] := Enclose@ReplacePart[expr, new, List /@ Range @@ Confirm@ResourceFunction["SpanRange"][span, expr]]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/009bb40301b09643.png) | 
| In[66]:= | ![extract[{1, 2, 3, 4}, -2 ;;]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/4440b5d71b48cb5d.png) | 
| Out[66]= |  | 
| In[67]:= | ![extract[{1, 2, 3, 4}, -2 ;; 0 ;; -1]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/12f50cdf05b6831b.png) | 
| Out[67]= |  | 
| In[68]:= | ![extract[{1, 2, 3, 4}, -UpTo[3]]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/401c726d8e20f7aa.png) | 
| Out[68]= |  | 
| In[69]:= | ![extract[{1, 2}, -UpTo[3]]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/7c78a8d065ae2c31.png) | 
| Out[69]= |  | 
| In[70]:= | ![mapAt[f, {1, 2, 3, 4}, 2 ;; UpTo[2]]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/0419d476d2993f26.png) | 
| Out[70]= |  | 
| In[71]:= | ![mapAt[f, {1, 2, 3, 4}, -2 ;; UpTo[2] ;; -2]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/5f0c731678411621.png) | 
| Out[71]= |  | 
| In[72]:= | ![mapAt[f, {1, 2, 3, 4}, 2 ;; 0 ;; All]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/0d56dbbcda7e3a86.png) | 
| Out[72]= |  | 
| In[73]:= | ![replacePart[{1, 2, 3, 4}, 0 ;; UpTo[2] -> x]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/36d456c7140c8b9e.png) | 
| Out[73]= |  | 
| In[74]:= | ![replacePart[{1, 2, 3, 4}, ;; ;; 2 -> x]](https://www.wolframcloud.com/obj/resourcesystem/images/cc5/cc55d33f-8425-4015-a831-95e5eaced642/2492d4a606e6f46e.png) | 
| Out[74]= |  | 
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License