<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Dec 30, 2015, at 3:57 PM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">


<title class=""></title>

<div class=""><div class="">It's sometimes useful to get the first element of a sequence. To that end I'd like to propose<br class=""></div>
<div class="">&nbsp;</div>
<div class=""><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span>SequenceType<span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </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);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </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);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> first: </span>Self<span style="font-variant-ligatures: no-common-ligatures;" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures;" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures;" class="">? {</span></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> gen = <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">generate</span>()</div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> gen.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">next</span>()</div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}<br class=""></div><div class="">&nbsp;</div>
<div class="">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 class=""></div>
</div>
<div class="">&nbsp;</div>
<div class="">With this added to SequenceType, we can remove it from CollectionType, as the behavior will be the same.<br class=""></div></div></div></blockquote><div><br class=""></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 class=""></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 class=""></div><div><br class=""></div><div><div class="">-Dave</div><br class=""><div><blockquote type="cite" class=""></blockquote></div></div></body></html>