title: How do I return a sequence in Swift?
body: <p>I'm trying to write an extension for the <code>Matrix</code> example from the book, <a href="https://stackoverflow.com/q/24136604/458193">slightly tweaked to be generic</a>.<br>
  I'm trying to write a method called <code>getRow</code> that returns a sequence of values at the given row.</p>
  
  <p>In C#, I would have written this:</p>
  
  <pre><code>IEnumerable&lt;T&gt; GetRow (int row)
  {
      return Enumerable
          .Range (0, this.columns)
          .Select ((column) =&gt; this.grid[row, columns]);
  }
  </code></pre>
  
  <p>or alternatively</p>
  
  <pre><code>IEnumerable&lt;T&gt; GetRow (int row)
  {
      for (var column = 0; column &lt; this.columns; column++) {
          yield return this.grid[row, column];
      }
  }
  </code></pre>
  
  <p>I'm not sure how to do this in Swift though.</p>
  
  <p><code>Sequence</code> seems to be the equivalent to <code>IEnumerable&lt;T&gt;</code> but I don't understand why it uses <code>typealias</code> instead of just being defined as <code>Sequence&lt;T&gt;</code> (<a href="https://schani.wordpress.com/2014/06/03/playing-with-swift/2/" rel="nofollow noreferrer">see also this</a>). Defining a method that returns generic <code>Sequence&lt;T&gt;</code> did not work:</p>
  
  <pre><code>extension Matrix {
      // Cannot specialize non-generic type 'Sequence'
      func getRow&lt;T&gt;(index: Int) -&gt; Sequence&lt;T&gt; {
          return map(0..self.columns, { self[index, $0] })
      }
  }
  </code></pre>
  
  <p>Then I got rid of <code>&lt;T&gt;</code> (but how is it supposed to be generic?):</p>
  
  <pre><code>extension Matrix {
      func getRow(index: Int) -&gt; Sequence {
          return map(0..self.columns, { self[index, $0] })
      }
  }
  </code></pre>
  
  <p>This compiles! However I can't use it:</p>
  
  <pre><code>var row = grid.getRow(0)
  // 'Sequence' does not conform to protocol '_Sequence_'
  for i in row {
      println("\(i)")
  }
  </code></pre>
  
  <p>How do I properly type <code>map</code> result so it can be consumed in a <code>for..in</code> loop?</p>
  
  <p>More on this issue: <a href="http://schani.wordpress.com/2014/06/11/associated-types-considered-weird/" rel="nofollow noreferrer">Associated Type Considered Weird</a></p>