<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>We have plenty of examples of GeneratorTypes that also conform to SequenceType. There's no harm in it, and it saves having to declare a separate type that consists solely of a generate() method that returns the generator. In fact, the stdlib automatically derives the generate() method for any GeneratorType that conforms to SequenceType already, specifically to make this pattern as easy as possible. The only thing at all odd about this is the name PermutationGenerator doesn't tell you it's a sequence, but that's no different than AnyGenerator, EmptyGenerator, EnumerateGenerator, FlattenGenerator, IndexingGenerator, JoinGenerator, LazyFilterGenerator, LazyMapGenerator, RangeGenerator, and UnsafeBufferPointerGenerator (though to be fair all of those have a matching distinct Sequence type).<br></div>
<div> </div>
<div>As for PermutationCollection, that's not a bad idea. I guess the biggest objection is that I'm not sure if PermutationGenerator is even pulling its own weight, and adding more permutation types won't help. I don't see any uses of PermutationGenerator in the stdlib, and I've never found a use for it in my own code, though I imagine that someone somewhere is actually using it. In any case, the argument "because we can" isn't sufficient to add something to the stdlib; it has to actually be of enough use to be worth both the added maintenance burden and the added code size.<br></div>
<div> </div>
<div>That said, even if we do add a PermutationGenerator, I definitely don't think extending CollectionType with a collect() method set like that is worth doing. If we did have a method it should probably be called "permute", but I doubt enough people would use it to be worth the added semantic overhead of yet another collection method.<br></div>
<div> </div>
<div>-Kevin Ballard</div>
<div> </div>
<div>On Wed, Dec 30, 2015, at 06:22 PM, Susan Cheng via swift-evolution wrote:<br></div>
<blockquote type="cite"><div dir="ltr"><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;">PermutationGenerator confuses me that it's confirm to both of <span class="colour" style="color:rgb(112, 61, 170)">SequenceType </span>and <span class="colour" style="color:rgb(112, 61, 170)">GeneratorType</span>. Should it replace by PermutationSequence and PermutationGenerator?<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;">Also, we should have a PermutationCollection because we can:<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">struct</span> PermutationCollection<C : CollectionType, I : CollectionType <span class="colour" style="color:rgb(187, 44, 162)">where</span> C.Index == I.Generator.Element> : <span class="colour" style="color:rgb(112, 61, 170)">CollectionType</span> {<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">typealias</span> Generator = <span class="colour" style="color:rgb(112, 61, 170)">PermutationGenerator</span><<span class="colour" style="color:rgb(112, 61, 170)">C</span>, <span class="colour" style="color:rgb(112, 61, 170)">I</span>><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">typealias</span> Index = <span class="colour" style="color:rgb(112, 61, 170)">I</span>.<span class="colour" style="color:rgb(112, 61, 170)">Index</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">typealias</span> Element = <span class="colour" style="color:rgb(112, 61, 170)">C</span>.<span class="colour" style="color:rgb(112, 61, 170)">Generator</span>.<span class="colour" style="color:rgb(112, 61, 170)">Element</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">private</span> <span class="colour" style="color:rgb(187, 44, 162)">let</span> _base: <span class="colour" style="color:rgb(112, 61, 170)">C</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">private</span> <span class="colour" style="color:rgb(187, 44, 162)">let</span> _indices: <span class="colour" style="color:rgb(112, 61, 170)">I</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">subscript</span>(idx: <span class="colour" style="color:rgb(112, 61, 170)">Index</span>) -> <span class="colour" style="color:rgb(112, 61, 170)">Element</span> {<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> _base[_indices[idx]]<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">var</span> startIndex : <span class="colour" style="color:rgb(112, 61, 170)">Index</span> {<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> _indices.startIndex<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">var</span> endIndex : <span class="colour" style="color:rgb(112, 61, 170)">Index</span> {<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> _indices.endIndex<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">var</span> count : <span class="colour" style="color:rgb(112, 61, 170)">Index</span>.<span class="colour" style="color:rgb(112, 61, 170)">Distance</span> {<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> _indices.count<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">public</span> <span class="colour" style="color:rgb(187, 44, 162)">func</span> generate() -> <span class="colour" style="color:rgb(112, 61, 170)">Generator</span> {<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> PermutationGenerator(elements: _base, indices: _indices)<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;">}<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;">and some methods provide:<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);">public<span class="colour" style="color:rgb(0, 0, 0)"></span>extension<span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(112, 61, 170)">CollectionType</span><span class="colour" style="color:rgb(0, 0, 0)"> {</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);"><span class="colour" style="color:rgb(0, 0, 0)"></span>@warn_unused_result<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112, 61, 170);"><span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(187, 44, 162)">func</span><span class="colour" style="color:rgb(0, 0, 0)"> collect<I : </span>SequenceType<span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(187, 44, 162)">where</span><span class="colour" style="color:rgb(0, 0, 0)"></span>Index<span class="colour" style="color:rgb(0, 0, 0)"> == </span>I<span class="colour" style="color:rgb(0, 0, 0)">.</span>Generator<span class="colour" style="color:rgb(0, 0, 0)">.</span>Element<span class="colour" style="color:rgb(0, 0, 0)">>(indices: </span>I<span class="colour" style="color:rgb(0, 0, 0)">) -> </span>PermutationGenerator<span class="colour" style="color:rgb(0, 0, 0)"><</span>Self<span class="colour" style="color:rgb(0, 0, 0)">, </span>I<span class="colour" style="color:rgb(0, 0, 0)">> {</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> <span class="colour" style="color:rgb(112, 61, 170)">PermutationGenerator</span>(elements: <span class="colour" style="color:rgb(187, 44, 162)">self</span>, indices: indices)<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);"><span class="colour" style="color:rgb(0, 0, 0)"></span>@warn_unused_result<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">func</span> collect<I : <span class="colour" style="color:rgb(112, 61, 170)">CollectionType</span> <span class="colour" style="color:rgb(187, 44, 162)">where</span> <span class="colour" style="color:rgb(112, 61, 170)">Index</span> == <span class="colour" style="color:rgb(112, 61, 170)">I</span>.<span class="colour" style="color:rgb(112, 61, 170)">Generator</span>.<span class="colour" style="color:rgb(112, 61, 170)">Element</span>>(indices: <span class="colour" style="color:rgb(112, 61, 170)">I</span>) -> <span class="colour" style="color:rgb(79, 129, 135)">PermutationCollection</span><<span class="colour" style="color:rgb(112, 61, 170)">Self</span>, <span class="colour" style="color:rgb(112, 61, 170)">I</span>> {<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> <span class="colour" style="color:rgb(79, 129, 135)">PermutationCollection</span>(_base: <span class="colour" style="color:rgb(187, 44, 162)">self</span>, _indices: indices)<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;">}<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112, 61, 170);"><span class="colour" style="color:rgb(187, 44, 162)">public</span><span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(187, 44, 162)">extension</span><span class="colour" style="color:rgb(0, 0, 0)"></span>LazyCollectionType<span class="colour" style="color:rgb(0, 0, 0)"> {</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);"><span class="colour" style="color:rgb(0, 0, 0)"></span>@warn_unused_result<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112, 61, 170);"><span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(187, 44, 162)">func</span><span class="colour" style="color:rgb(0, 0, 0)"> collect<I : </span>SequenceType<span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(187, 44, 162)">where</span><span class="colour" style="color:rgb(0, 0, 0)"></span>Elements<span class="colour" style="color:rgb(0, 0, 0)">.</span>Index<span class="colour" style="color:rgb(0, 0, 0)"> == </span>I<span class="colour" style="color:rgb(0, 0, 0)">.</span>Generator<span class="colour" style="color:rgb(0, 0, 0)">.</span>Element<span class="colour" style="color:rgb(0, 0, 0)">>(indices: </span>I<span class="colour" style="color:rgb(0, 0, 0)">) -> </span>LazySequence<span class="colour" style="color:rgb(0, 0, 0)"><</span>PermutationGenerator<span class="colour" style="color:rgb(0, 0, 0)"><</span>Elements<span class="colour" style="color:rgb(0, 0, 0)">, </span>I<span class="colour" style="color:rgb(0, 0, 0)">>> {</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> <span class="colour" style="color:rgb(187, 44, 162)">self</span>.<span class="colour" style="color:rgb(112, 61, 170)">elements</span>.<span class="colour" style="color:rgb(49, 89, 93)">collect</span>(indices).<span class="colour" style="color:rgb(112, 61, 170)">lazy</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187, 44, 162);"><span class="colour" style="color:rgb(0, 0, 0)"></span>@warn_unused_result<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112, 61, 170);"><span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(187, 44, 162)">func</span><span class="colour" style="color:rgb(0, 0, 0)"> collect<I : </span>CollectionType<span class="colour" style="color:rgb(0, 0, 0)"></span><span class="colour" style="color:rgb(187, 44, 162)">where</span><span class="colour" style="color:rgb(0, 0, 0)"></span>Elements<span class="colour" style="color:rgb(0, 0, 0)">.</span>Index<span class="colour" style="color:rgb(0, 0, 0)"> == </span>I<span class="colour" style="color:rgb(0, 0, 0)">.</span>Generator<span class="colour" style="color:rgb(0, 0, 0)">.</span>Element<span class="colour" style="color:rgb(0, 0, 0)">>(indices: </span>I<span class="colour" style="color:rgb(0, 0, 0)">) -> </span>LazyCollection<span class="colour" style="color:rgb(0, 0, 0)"><</span><span class="colour" style="color:rgb(79, 129, 135)">PermutationCollection</span><span class="colour" style="color:rgb(0, 0, 0)"><</span>Elements<span class="colour" style="color:rgb(0, 0, 0)">, </span>I<span class="colour" style="color:rgb(0, 0, 0)">>> {</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(187, 44, 162)">return</span> <span class="colour" style="color:rgb(187, 44, 162)">self</span>.<span class="colour" style="color:rgb(112, 61, 170)">elements</span>.<span class="colour" style="color:rgb(49, 89, 93)">collect</span>(indices).<span class="colour" style="color:rgb(112, 61, 170)">lazy</span><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"> }<br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;"><br></p><p style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:11px;line-height:normal;font-family:Menlo;">}<br></p><div> </div>
</div>
<div><img style="height:1px !important;width:1px !important;border-top-width:0px !important;border-right-width:0px !important;border-bottom-width:0px !important;border-left-width:0px !important;margin-top:0px !important;margin-bottom:0px !important;margin-right:0px !important;margin-left:0px !important;padding-top:0px !important;padding-bottom:0px !important;padding-right:0px !important;padding-left:0px !important;" border="0" height="1" width="1" alt="" src="https://www.fastmailusercontent.com/proxy/53ca657220b6706c7eed63d6b7e731d6ce1f143bc88af6e9dcb41f7d2aa1d53d/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d3148765176786c673171614a7d2236454230345272776e4854496874423731516330365550375d223646524659445d2232454442655370755b433249534441464148443f476f614341775f436940353f47544f617e67646162424832407d63634b686544614071485c45415336485a465672656133784848505e48413474376369747a5d447364527f4079607078677135463b69686563477342335774367a665c4a403d6176764d22324a563a667644337a7d2236477d2236437a66784336313f607930353755494a487f6a513d22364a423253357d22364d2232476564616a6656554a586334714d23344d23344/open"><br></div>
<div><u>_______________________________________________</u><br></div>
<div>swift-evolution mailing list<br></div>
<div><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br></div>
<div><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div>
</blockquote><div> </div>
</body>
</html>