<div dir="ltr"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">PermutationGenerator confuses me that it&#39;s confirm to both of <span style="color:rgb(112,61,170)">SequenceType </span>and <span style="color:rgb(112,61,170)">GeneratorType</span>. Should it replace by PermutationSequence and PermutationGenerator?</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">Also, we should have a PermutationCollection because we can:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">struct</span> PermutationCollection&lt;C : CollectionType, I : CollectionType <span style="color:rgb(187,44,162)">where</span> C.Index == I.Generator.Element&gt; : <span style="color:rgb(112,61,170)">CollectionType</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">typealias</span> Generator = <span style="color:rgb(112,61,170)">PermutationGenerator</span>&lt;<span style="color:rgb(112,61,170)">C</span>, <span style="color:rgb(112,61,170)">I</span>&gt;</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">typealias</span> Index = <span style="color:rgb(112,61,170)">I</span>.<span style="color:rgb(112,61,170)">Index</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">typealias</span> Element = <span style="color:rgb(112,61,170)">C</span>.<span style="color:rgb(112,61,170)">Generator</span>.<span style="color:rgb(112,61,170)">Element</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">private</span> <span style="color:rgb(187,44,162)">let</span> _base: <span style="color:rgb(112,61,170)">C</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">private</span> <span style="color:rgb(187,44,162)">let</span> _indices: <span style="color:rgb(112,61,170)">I</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">subscript</span>(idx: <span style="color:rgb(112,61,170)">Index</span>) -&gt; <span style="color:rgb(112,61,170)">Element</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> _base[_indices[idx]]</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> startIndex : <span style="color:rgb(112,61,170)">Index</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> _indices.startIndex</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> endIndex : <span style="color:rgb(112,61,170)">Index</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> _indices.endIndex</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">var</span> count : <span style="color:rgb(112,61,170)">Index</span>.<span style="color:rgb(112,61,170)">Distance</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> _indices.count</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> generate() -&gt; <span style="color:rgb(112,61,170)">Generator</span> {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> PermutationGenerator(elements: _base, indices: _indices)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">




























</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">and some methods provide:</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)">public<span style="color:rgb(0,0,0)"> </span>extension<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(112,61,170)">CollectionType</span><span style="color:rgb(0,0,0)"> {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> collect&lt;I : </span>SequenceType<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">where</span><span style="color:rgb(0,0,0)"> </span>Index<span style="color:rgb(0,0,0)"> == </span>I<span style="color:rgb(0,0,0)">.</span>Generator<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)">&gt;(indices: </span>I<span style="color:rgb(0,0,0)">) -&gt; </span>PermutationGenerator<span style="color:rgb(0,0,0)">&lt;</span>Self<span style="color:rgb(0,0,0)">, </span>I<span style="color:rgb(0,0,0)">&gt; {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(112,61,170)">PermutationGenerator</span>(elements: <span style="color:rgb(187,44,162)">self</span>, indices: indices)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(187,44,162)">func</span> collect&lt;I : <span style="color:rgb(112,61,170)">CollectionType</span> <span style="color:rgb(187,44,162)">where</span> <span style="color:rgb(112,61,170)">Index</span> == <span style="color:rgb(112,61,170)">I</span>.<span style="color:rgb(112,61,170)">Generator</span>.<span style="color:rgb(112,61,170)">Element</span>&gt;(indices: <span style="color:rgb(112,61,170)">I</span>) -&gt; <span style="color:rgb(79,129,135)">PermutationCollection</span>&lt;<span style="color:rgb(112,61,170)">Self</span>, <span style="color:rgb(112,61,170)">I</span>&gt; {</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">PermutationCollection</span>(_base: <span style="color:rgb(187,44,162)">self</span>, _indices: indices)</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">public</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">extension</span><span style="color:rgb(0,0,0)"> </span>LazyCollectionType<span style="color:rgb(0,0,0)"> {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> collect&lt;I : </span>SequenceType<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">where</span><span style="color:rgb(0,0,0)"> </span>Elements<span style="color:rgb(0,0,0)">.</span>Index<span style="color:rgb(0,0,0)"> == </span>I<span style="color:rgb(0,0,0)">.</span>Generator<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)">&gt;(indices: </span>I<span style="color:rgb(0,0,0)">) -&gt; </span>LazySequence<span style="color:rgb(0,0,0)">&lt;</span>PermutationGenerator<span style="color:rgb(0,0,0)">&lt;</span>Elements<span style="color:rgb(0,0,0)">, </span>I<span style="color:rgb(0,0,0)">&gt;&gt; {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(112,61,170)">elements</span>.<span style="color:rgb(49,89,93)">collect</span>(indices).<span style="color:rgb(112,61,170)">lazy</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">    </p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">    </span>@warn_unused_result</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">func</span><span style="color:rgb(0,0,0)"> collect&lt;I : </span>CollectionType<span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">where</span><span style="color:rgb(0,0,0)"> </span>Elements<span style="color:rgb(0,0,0)">.</span>Index<span style="color:rgb(0,0,0)"> == </span>I<span style="color:rgb(0,0,0)">.</span>Generator<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)">&gt;(indices: </span>I<span style="color:rgb(0,0,0)">) -&gt; </span>LazyCollection<span style="color:rgb(0,0,0)">&lt;</span><span style="color:rgb(79,129,135)">PermutationCollection</span><span style="color:rgb(0,0,0)">&lt;</span>Elements<span style="color:rgb(0,0,0)">, </span>I<span style="color:rgb(0,0,0)">&gt;&gt; {</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(112,61,170)">elements</span>.<span style="color:rgb(49,89,93)">collect</span>(indices).<span style="color:rgb(112,61,170)">lazy</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">
























</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><div><br></div></div>