<div dir="ltr">Related to this I've been toying around with a tweak to GeneratorType - it could clear up some of the issues with <font face="monospace, monospace">.first</font> consuming part of the sequence:<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><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)">protocol</span> NewGeneratorType {</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">typealias</span> Element</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">func</span> next() -> (value: <span style="color:rgb(112,61,170)">Element</span>, state: <span style="color:rgb(112,61,170)">Self</span>)?</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">extension</span><span style="color:rgb(0,0,0)"> </span>NewGeneratorType<span style="color:rgb(0,0,0)"> {</span></p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">mutating</span> <span style="color:rgb(187,44,162)">func</span> next() -> <span style="color:rgb(112,61,170)">Element</span>? {</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">let</span> nextPair: (value: <span style="color:rgb(112,61,170)">Element</span>, state: <span style="color:rgb(112,61,170)">Self</span>)? = <span style="color:rgb(187,44,162)">self</span>.<span style="color:rgb(49,89,93)">next</span>()</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">if</span> <span style="color:rgb(187,44,162)">let</span> state = nextPair?.state {</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">self</span> = state</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> nextPair?.value</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</p></div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p></div></blockquote><div><br></div>I haven't had time for a proposal yet, but thought I'd mention it as it seemed relevant.<div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Dec 31, 2015 at 7:40 PM, Dave Abrahams via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div>
<div>-Dave</div>
</div>
<br><div><span class=""><blockquote type="cite"><div>On Dec 30, 2015, at 5:00 PM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div>
<div><div>On Wed, Dec 30, 2015, at 04:39 PM, Daniel Duan wrote:<br></div>
<blockquote type="cite"><div>Here it is <a href="https://github.com/apple/swift/blob/master/stdlib/public/core/CollectionAlgorithms.swift.gyb#L26" target="_blank">https://github.com/apple/swift/blob/master/stdlib/public/core/CollectionAlgorithms.swift.gyb#L26</a><br></div>
<div><div> </div>
<div><blockquote type="cite"><div>On Dec 30, 2015, at 4:27 PM, Kevin Ballard <<a href="mailto:kevin@sb.org" target="_blank">kevin@sb.org</a>> wrote:<br></div>
<div> </div>
<div><div><div>We already don't have a .last on CollectionType and nobody's been complaining about that. Besides, sequences don't necessarily even terminate.<br></div>
<div> </div>
<div>-Kevin Ballard<br></div>
<div> </div>
<div>On Wed, Dec 30, 2015, at 04:01 PM, Daniel Duan wrote:<br></div>
<blockquote type="cite"><div>Users who don’t get the single-pass nature of SequenceType may expect a .last as well.<br></div>
</blockquote></div>
</div>
</blockquote></div>
</div>
</blockquote><div> </div>
<div>Ah you're right, I was just looking at the unconstrained protocol. In any case, we could theoretically provide a .last, but I don't think that's useful enough on sequences to warrant inclusion. I know I've wanted .first many times and I've never wanted .last.<br></div>
<div> </div>
<div>Another motivation for adding this that I forgot to mention is that today the code `someCol.lazy.filter(pred).first` actually isn't lazy at all, it filters the entire collection and builds a new array (because SequenceType doesn't have .first so it resolves the .filter() to the eager version instead of the lazy version). </div></div></div></blockquote><div><br></div></span>Oh, that’s nasty. I wonder if there’s something we can do with ambiguity to make the eager overload inaccessible in that context? Would you mind opening a bug for this? </div><div><br><blockquote type="cite"><div><span class=""><div><div>Adding .first to SequenceType makes that expression actually do what the user intended (although my other proposal for SequenceType.find() provides a much better way to accomplish the same task).<br></div>
<div> </div>
<div>On Wed, Dec 30, 2015, at 04:40 PM, gs. wrote:<br></div>
<blockquote type="cite"><div>I like this addition and I think that we should take care to document whether or not this mutates the sequence. I actually expect it to but maybe I am mistaken.<br></div>
</blockquote><div> </div>
<div>(moving this back to the list)<br></div>
<div> </div>
<div>I considered documenting that, but none of the existing "destructive" methods on SequenceType document that. I think the assumption is that anything that has to inspect the contents of the sequence is obviously consuming the sequence to do that. In fact, the one method that doesn't consume anything (not counting generate() since any use of the generator is destructive), underestimateCount(), is explicitly documented as being non-destructive.<br></div>
<div> </div>
<div>Also, I couldn't think of a non-awkward way to say "this property partially consumes the sequence if it's a sequence that is destructively "consumed" by iteration". Especially because "partially consumed" isn't actually a property of sequences; it's explicitly documented that calling generate() a second time after any iteration is allowed to return a completely arbitrary sequence of elements from the second generator (for example, a generator that returns lines read from some stream might buffer more data internally and therefore constructing a second generator would possibly skip data that was never returned from the first generator).<br></div>
<div> </div>
<div>-Kevin Ballard</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=JfMPa-2F7wwZPzsZ3QKA8NjtONIYX4SjbWuUxtpfsTY2hr20Kkib8Yd2rLIiLiiElKYMDUuUCXNdm3m9qHodoQ97gEmjN185HJjqjKRGARBxFOSGC5DG-2FWevhfWHltUcA0-2BJ9kON-2FmGD2KNjIOQrKOAJleIaIjTG-2FMYcGrmSHQWffPdtF13u7cvY0gkQBIO0g60eCjCYa-2BaqtY-2Bzd7Cp3OyU-2BghouDXrXAS5easlISNeQ-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div></span><span class="">
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=pQw7h83fWt3LLbgkfL4TSUL0weaZnVFZxDe5GShw4uRq1QASyV7ackd-2F2fyNxFupk-2FU0I1IjzVxjPK-2Ff6GxFr0Q4C3wojkm6MxG-2BJtWsSVwDko8eahkwPgbqZmnAimqJR6z8kMIV9qpi98UrOVKannhODWD-2FeWaeGBXrSU2yrMEWx9lUUeicJlQkgoP-2FdhU3Dhg74QdmVf8dLKxGzmQrkGwLxdzkD1apSeBzCO4aQCE-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
</div>
<br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>