Skip to content

Span ​

A range of positions i;;j;;step used as a part specification.

✓ checked against that system · ! checked, with disagreements (hover a chip)Wolfram LanguageSpan✓
Span(i, j, step?)positions i,i+step,…i, i+step, \dots up to jj; a negative position counts from the end. Never evaluated on its own — read by At.
Typed signatures
TypePackageApplies
(integer, integer, integer?) -> unknowncombinatoricsalways

Domain: Collections

Details
  • Never reduces by itself: it is a part specification other heads read, the way RangeRange describes a sequence of numbers. At is the only head here that reads one.
  • stepstep defaults to 1; a negative stepstep walks from ii down to jj.
  • Negative ii/jj count from the end, exactly as a plain negative index does in At.

Examples

Basic #

Positions 2 through 4 (2;;42;;4 in Wolfram)

live ↗
In[a,b,c,d,f][Span(2,4)]\bigl\lbrack a, b, c, d, f\bigr\rbrack[\mathrm{Span}(2, 4)]Out[b,c,d]\bigl\lbrack b, c, d\bigr\rbrack

Every other element through the last (1;;−1;;21;;-1;;2)

live ↗
In[a,b,c,d,f][Span(1,−1,2)]\bigl\lbrack a, b, c, d, f\bigr\rbrack[\mathrm{Span}(1, -1, 2)]Out[a,c,f]\bigl\lbrack a, c, f\bigr\rbrack
live ↗
In[a,b,c,d,k,f,g,h][Span(2,5)]\bigl\lbrack a, b, c, d, k, f, g, h\bigr\rbrack[\mathrm{Span}(2, 5)]Out[b,c,d,k]\bigl\lbrack b, c, d, k\bigr\rbrack
live ↗
In[a,b,c,d,k,f,g,h][Span(1,−1,3)]\bigl\lbrack a, b, c, d, k, f, g, h\bigr\rbrack[\mathrm{Span}(1, -1, 3)]Out[a,d,g]\bigl\lbrack a, d, g\bigr\rbrack
Scope #

A negative step walks backwards (−1;;1;;−1-1;;1;;-1)

live ↗
In[a,b,c,d,f][Span(−1,1,−1)]\bigl\lbrack a, b, c, d, f\bigr\rbrack[\mathrm{Span}(-1, 1, -1)]Out[f,d,c,b,a]\bigl\lbrack f, d, c, b, a\bigr\rbrack

Negative bounds: the last two elements

live ↗
In[a,b,c,d,f][Span(−2,−1)]\bigl\lbrack a, b, c, d, f\bigr\rbrack[\mathrm{Span}(-2, -1)]Out[d,f]\bigl\lbrack d, f\bigr\rbrack

Spans at two levels cut out a submatrix

live ↗
In[[1,2,3],[4,5,6],[7,8,9]][Span(2,3),Span(1,2)]\bigl\lbrack\bigl\lbrack1, 2, 3\bigr\rbrack, \bigl\lbrack4, 5, 6\bigr\rbrack, \bigl\lbrack7, 8, 9\bigr\rbrack\bigr\rbrack[\mathrm{Span}(2, 3), \mathrm{Span}(1, 2)]Out[[4,5],[7,8]]\bigl\lbrack\bigl\lbrack4, 5\bigr\rbrack, \bigl\lbrack7, 8\bigr\rbrack\bigr\rbrack

The first five letters

live ↗
InCharacterRange(a,z)[Span(1,5)]\mathrm{CharacterRange}(\text{a}, \text{z})[\mathrm{Span}(1, 5)]Out[a,b,c,d,e]\bigl\lbrack\text{a}, \text{b}, \text{c}, \text{d}, \text{e}\bigr\rbrack

A negative end counts from the last element, so the slice runs to the end

live ↗
In(1..10)[Span(3,−1)](1..10)[\mathrm{Span}(3, -1)]Out[3,4,5,6,7,8,9,10]\bigl\lbrack3, 4, 5, 6, 7, 8, 9, 10\bigr\rbrack

A step of 22 takes every other element

live ↗
In(1..10)[Span(1,−1,2)](1..10)[\mathrm{Span}(1, -1, 2)]Out[1,3,5,7,9]\bigl\lbrack1, 3, 5, 7, 9\bigr\rbrack
live ↗
In[a,b,c,d,k,f,g,h][Span(2,−3)]\bigl\lbrack a, b, c, d, k, f, g, h\bigr\rbrack[\mathrm{Span}(2, -3)]Out[b,c,d,k,f]\bigl\lbrack b, c, d, k, f\bigr\rbrack
About
Examples11
Checked againstWolfram Language 11 agree
CrosswalkWolfram Language

See also: At, UpTo