<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 31, 2015, at 1:58 PM, Kevin Ballard <<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<title class=""></title>
<div class=""><div class=""><div class=""><div class="">On Thu, Dec 31, 2015, at 12:36 AM, Dave Abrahams wrote:<br class=""></div>
</div>
</div>
<blockquote type="cite" class=""><div class=""><blockquote type="cite" class=""><div class="">On Dec 30, 2015, at 3:57 PM, Kevin Ballard via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""></div>
<div class=""> </div>
<div class=""><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=""> </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 class="colour" style="color:rgb(187, 44, 162)">extension</span><span style="" class=""></span>SequenceType<span style="" class=""> {</span><br class=""></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="" class=""></span>/// Returns the first element of `self`, or `nil` if `self` is empty.<br class=""></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="" class=""></span>/// - Complexity: O(1)<br class=""></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="" class=""></span><span class="colour" style="color:rgb(187, 44, 162)">var</span><span style="" class=""> first: </span>Self<span style="" class="">.</span>Generator<span style="" class="">.</span>Element<span style="" class="">? {</span><br class=""></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;" class=""><span class="colour" style="color:rgb(187, 44, 162)">var</span> gen = <span class="colour" style="color:rgb(61, 29, 129)">generate</span>()<br class=""></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;" class=""><span class="colour" style="color:rgb(187, 44, 162)">return</span> gen.<span class="colour" style="color:rgb(61, 29, 129)">next</span>()<br class=""></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;" class=""> }<br class=""></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;" class="">}<br class=""></div>
<div class=""> </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=""> </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 class=""> </div>
<div class="">Property accesses should not mutate the receiver, and because of how Sequences work, inspecting first may consume the first element.<br class=""></div>
</div>
</blockquote><div class=""> </div>
<div class="">Fair enough. Swift does support `mutating get`, but it looks like the stdlib doesn't actually use this anywhere (at least, in the public API).<br class=""></div>
<div class=""> </div>
<div class="">Still, it's a shame that there's no way to do this generically for sequences. It's something I need upon occasion. I wish I could actually say something like `seq.generate().next()`, but you can't call mutating functions on temporaries.<br class=""></div>
<div class=""> </div>
<div class="">I'd be tempted to say we should have a `func first()` method, except then CollectionTypes would have both a property and a method named `first` and that would just be confusing.<br class=""></div>
<div class=""> </div>
<blockquote type="cite" class=""><div class=""><div class="">I suggest you consider adding a BufferedSequence<Base: SequenceType> that has a stable first property (at least until it is iterated)<br class=""></div>
</div>
</blockquote><div class=""> </div>
<div class="">Good idea, though I'd probably call it PeekSequence because it would only buffer a single element (and BufferedSequence sounds like it's got an arbitrary-sized buffer). Perhaps more useful would be the associated PeekGenerator, because peek() is a useful thing to have when writing custom generator-using code.</div></div></div></blockquote><div><br class=""></div>The size of the buffer is an implementation detail, and I don’t find “peek” descriptive.</div><div><br class=""><blockquote type="cite" class=""><div class="">
<div class="">I'll write up a more detailed email with a proposed design in a minute.</div>
<div class=""> </div>
<blockquote type="cite" class=""><div class=""><div class="">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.<br class=""></div>
</div>
</blockquote><div class=""> </div>
<div class="">Also a good idea. Although really it could just be backed by a ContiguousArray, those things already grow in chunks. I'll write up a design for that too.</div></div></blockquote><div><br class=""></div>Not unless you want to keep the buffers alive longer than necessary. Imagine you’re parsing a long stream with some amount of lookahead. You can scan this collection by slicing it and the buffer segments that are no longer in use will be automatically collected.</div><div><br class=""><blockquote type="cite" class=""><div class="">
<div class="">-Kevin Ballard</div>
</div>
</blockquote></div><br class=""><div class="">
-Dave
</div>
<br class=""></body></html>