title: Shift elements in array by index
body: <p>Given array of n elements, i.e.</p>
  
  <p><code>var array = [1, 2, 3, 4, 5]</code></p>
  
  <p>I can write an extension to the <code>Array</code> so I can modify array to achieve this output: <code>[2, 3, 4, 5, 1]</code>:</p>
  
  <pre><code>mutating func shiftRight() {
    append(removeFirst())
  }
  </code></pre>
  
  <p>Is there a way to implement such a function that would shift array by any index, positive or negative. I can implement this function in imperative style with <code>if-else</code> clauses, but what I am looking for is functional implementation.</p>
  
  <p>The algorithm is simple:</p>
  
  <ol>
  <li>Split array into two by the index provided</li>
  <li>append first array to the end of the second</li>
  </ol>
  
  <p>Is there any way to implement it in functional style?</p>
  
  <p>The code I've finished with:</p>
  
  <pre><code>extension Array {
    mutating func shift(var amount: Int) {
      guard -count...count ~= amount else { return }
      if amount &lt; 0 { amount += count }
      self = Array(self[amount ..&lt; count] + self[0 ..&lt; amount])
    }
  }
  </code></pre>