title: Range shenanigans
body: <p>Okay, I looked at the suggestions above, and none of them quite hit the mark.</p>
  
  <p>The issue I am having is with the Range primitive/class/collection whatzit in Swift.</p>
  
  <p>Ranges are cool.</p>
  
  <p>And badly documented (<strong>NOTE:</strong> Just because <em>"It's in the manual"</em> does NOT mean it's <em>"well documented."</em> If I can't easily find it -which I can't-, then that means it isn't actually well-documented. Indexes and ToCs are <strong>VERY IMPORTANT</strong>).</p>
  
  <p>I certainly appreciate being directed towards the relevant sections of the documentation.</p>
  
  <p>Now, what I REALLY want, is the ability to treat ranges like sets, with intersections and unions, and whatnot.</p>
  
  <p>Failing that, I decided that I'd like to roll my own.</p>
  
  <p>I'll paste my playground below. It's pretty simple.</p>
  
  <p>First of all, in the app I'm writing now, I'd like to use a range to describe a range of relevant indexes, with a starting index and an ending index.</p>
  
  <p>That means a <code>Range&lt;Int&gt;</code>.</p>
  
  <p>What I discovered, is that all <code>Range&lt;Int&gt;</code> ranges are stored as start:Int..&lt;end:Int, even if you specify them as start:Int...end:Int.</p>
  
  <p>That makes getting the last value a bit tricky. If you ask for Range.endIndex, you get end:Int + 1.</p>
  
  <p>Now, if you look at my union sample below, you will see that the final range delivers an endIndex of 21.</p>
  
  <p>I have a feeling that I'm using ranges all wrong.</p>
  
  <pre><code>let rangeA:Range&lt;Int&gt; = 3...7
  
  var low:Int = rangeA.startIndex // This is 3
  var high:Int = rangeA.endIndex  // This is 8
  
  let rangeB:Range&lt;Int&gt; = 1...9
  
  let rangeC:Range&lt;Int&gt; = 7...19
  
  var lowestValue:Int = min ( rangeA.startIndex, rangeA.endIndex )
  
  lowestValue = min ( lowestValue, min ( rangeB.startIndex, rangeB.endIndex ) )
  
  lowestValue = min ( lowestValue, min ( rangeC.startIndex, rangeC.endIndex ) )
  
  var highestValue = max ( rangeA.startIndex, rangeA.endIndex )
  
  highestValue = max ( highestValue, max ( rangeB.startIndex, rangeB.endIndex ) )
  
  highestValue = max ( highestValue, max ( rangeC.startIndex, rangeC.endIndex ) )
  
  let unityRange:Range&lt;Int&gt; = lowestValue...highestValue
  
  low = unityRange.startIndex // This is 1
  high = unityRange.endIndex  // This is 21
  </code></pre>