Generator should always generates value from start of sequence. I don&#39;t see any problems with this implementation.<span></span><br><br>Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; 於 2015年12月31日星期四 寫道:<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"><div><blockquote type="cite"><div>On Dec 30, 2015, at 3:57 PM, Kevin Ballard via swift-evolution &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-evolution@swift.org&#39;);" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div>




<div><div>It&#39;s sometimes useful to get the first element of a sequence. To that end I&#39;d like to propose<br></div>
<div> </div>
<div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#bb2ca2">extension</span><span> </span>SequenceType<span> {</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144,45,208)"><span>    </span>/// Returns the first element of `self`, or `nil` if `self` is empty.</div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144,45,208)"><span>    </span>/// - Complexity: O(1)</div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span>    </span><span style="color:#bb2ca2">var</span><span> first: </span>Self<span>.</span>Generator<span>.</span>Element<span>? {</span></div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:#bb2ca2">var</span> gen = <span style="color:#3d1d81">generate</span>()</div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo">        <span style="color:#bb2ca2">return</span> gen.<span style="color:#3d1d81">next</span>()</div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo">    }</div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo">}<br></div><div> </div>
<div>I think it makes sense to add this property to the definition of SequenceType as well, so various sequences can override it to avoid constructing a generator.<br></div>
</div>
<div> </div>
<div>With this added to SequenceType, we can remove it from CollectionType, as the behavior will be the same.<br></div></div></div></blockquote><div><br></div>Property accesses should not mutate the receiver, and because of how Sequences work, inspecting first may consume the first element. I suggest you consider adding a BufferedSequence&lt;Base: SequenceType&gt; that has a stable first property (at least until it is iterated)</div><div><br></div><div>Another related adapter I’d like to add is a model of CollectionType that is backed by a sequence and lazily populated in fixed-sized chunks.</div><div><br></div><div><br></div><div><div>-Dave</div><br><div><blockquote type="cite"></blockquote></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=TLKjIbzJNF7iO00qsyibvTIy-2FQQuQFxT-2FekFfPGMZK9CgbNsO5DpFqL1TjOvERoEEctqIYgs2a6Ro0-2F9MjAqqRis7sYVz4qVs70XxobXEkzJtW58dXZ7i7QDiU2ulKqp2Ut3RiIwaUBFTG4o6nwCAyL3UCZi6RvYzDYcb38uyILXdN7ni-2FNT0Xkmb-2F8VFj-2BoNJeizY4vFwC9gscb26PEzXSI3pPxVU-2F6vqUlAj7cOXw-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>
</blockquote>