<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>On Fri, Jan 1, 2016, at 12:35 PM, Félix Cloutier wrote:<br></div>
<blockquote type="cite"><div>Not a fan of anything that reminds me of Java streams.<br></div>
</blockquote><div>&nbsp;</div>
<div>I don't know anything at all about Java streams, but this is based on the very common concept of being able to "peek" at the next element of an enumerable value, just extended a bit to support lookahead of more than 1.</div>
<div>&nbsp;</div>
<blockquote type="cite"><div>Is the case where making an array from the sequence isn't possible significant enough for a new standard API?<br></div>
</blockquote><div>&nbsp;</div>
<div>Why would you make an array from the sequence? That's flagrantly wasteful if all you're doing is processing a sequence and simply need some lookahead. And in the case of an infinite sequence it's downright impossible.<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard</div>
<div>&nbsp;</div>
<blockquote type="cite"><div><span class="colour" style="color:rgb(0, 0, 0)"><span class="font" style="font-family:'Lucida Grande'">Félix</span></span><br></div>
<div>&nbsp;</div>
<div><blockquote type="cite"><div>Le 31 déc. 2015 à 20:50:40, Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; a écrit :<br></div>
<div>&nbsp;</div>
<div><div><div><div>+1<br></div>
<div>&nbsp;</div>
<div>Sent from my iPad<br></div>
</div>
<div><div>&nbsp;</div>
<div>On 1 Jan 2016, at 11:16 AM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div>
</div>
<blockquote type="cite"><div><div>BufferedSequence is a sequence adaptor that wraps any underlying sequence and provides a stable `first` property. BufferedGenerator is a generator adaptor that wraps any underlying generator and provides arbitrary lookahead with a `peek(n: Int)` method.<br></div>
<div>&nbsp;</div>
<div>The method name "peek()" has precedent in various languages (e.g. Rust with std::iter::Peekable, C++ with std::basic_istream::peek, Ruby with Enumerator#peek, etc). I considered the name "lookahead()" but I decided it made more sense to use that name for the property that describes how much lookahead there is.<br></div>
<div>&nbsp;</div>
<div>The proposed API looks like this:<br></div>
<div>&nbsp;</div>
<div><div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);">/// A sequence adaptor that adds a nondestructive `first` property to any<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);">/// underlying sequence.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);">/// - Note: If the underlying sequence is not destructively "consumed" by<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);">/// &nbsp; iteration, then neither is `BufferedSequence`.<br></div>
<div style="margin-top:0px;margin-bottom: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)">class</span> BufferedSequence&lt;Base : <span class="colour" style="color:rgb(112, 61, 170)">SequenceType</span>&gt; : <span class="colour" style="color:rgb(112, 61, 170)">SequenceType</span> {<br></div>
<div><div style="margin-top:0px;margin-bottom: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)">init</span>(<span class="colour" style="color:rgb(187, 44, 162)">_</span> base: <span class="colour" style="color:rgb(112, 61, 170)">Base</span>)<br></div>
</div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;">&nbsp;</div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// Returns ` BufferedGenerator` with a lookahead size of `1`.<br></div>
<div style="margin-top:0px;margin-bottom: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() -&gt; BufferedSequence.<span class="colour" style="color:rgb(79, 129, 135)">BufferedGenerator</span>&lt;<span class="colour" style="color:rgb(112, 61, 170)">Base</span>.<span class="colour" style="color:rgb(112, 61, 170)">Generator</span>&gt;<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;">&nbsp;</div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// Returns the first element of the underlying sequence, **nondestructively**.<br></div>
<div style="margin-top:0px;margin-bottom: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> first: <span class="colour" style="color:rgb(112, 61, 170)">Base</span>.<span class="colour" style="color:rgb(112, 61, 170)">Generator</span>.<span class="colour" style="color:rgb(112, 61, 170)">Element</span>? { get }<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;">}<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;">&nbsp;</div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);">/// A generator adaptor that adds a nondestructive `peek()` method to any<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);">/// underlying generator.<br></div>
<div style="margin-top:0px;margin-bottom: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> BufferedGenerator&lt;Base : <span class="colour" style="color:rgb(112, 61, 170)">GeneratorType</span>&gt; : <span class="colour" style="color:rgb(112, 61, 170)">GeneratorType</span> {<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// Construct an instance that buffers access to an underlying generator.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Parameter base: The underlying generator.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Parameter lookahead: The amount of lookahead to allow. Default is `1`.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// &nbsp; Values less than `1` will be treated the same as `1`.<br></div>
<div style="margin-top:0px;margin-bottom: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)">init</span>(<span class="colour" style="color:rgb(187, 44, 162)">_</span> base: <span class="colour" style="color:rgb(112, 61, 170)">Base</span>, lookahead: <span class="colour" style="color:rgb(112, 61, 170)">Int</span> = <span class="colour" style="color:rgb(187, 44, 162)">default</span>)<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;">&nbsp;</div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// The amount of lookahead that this generator offers.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Invariant: `lookahead &gt;= 1`.<br></div>
<div style="margin-top:0px;margin-bottom: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)">let</span> lookahead: <span class="colour" style="color:rgb(112, 61, 170)">Int</span><br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;">&nbsp;</div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// Advance to the next element and return it, or `nil` if no next element exists.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;"><span class="colour" style="color:rgb(144, 45, 208)">///</span><br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Requires: Neither `next()` nor `peek()` have been applied to a copy of<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// &nbsp; `self` since the copy was made, and no preceding call to `self.next()` has<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// &nbsp; returned `nil`.<br></div>
<div style="margin-top:0px;margin-bottom: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)">mutating</span> <span class="colour" style="color:rgb(187, 44, 162)">func</span> next() -&gt; <span class="colour" style="color:rgb(112, 61, 170)">Base</span>.<span class="colour" style="color:rgb(112, 61, 170)">Element</span>?<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;">&nbsp;</div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// Returns the value that will be returned from subsequent calls to `next()`.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Parameter n: The number of elements to look ahead. Default is `0`. A value<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// &nbsp; of `0` means to look at the next element.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Precondition: `n &gt;= 0 &amp;&amp; n &lt; lookahead`.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Requires: Neither `next()` nor `peek()` have been applied to a copy of<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// &nbsp; `self` since the copy was made, and no preceding call to `self.next()` has<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// &nbsp; returned `nil`.<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>/// - Note: It is safe to peek at values past the end of the underlying generator<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>///&nbsp; (`peek()` will return `nil` in such cases). It is also safe to call `peek()`<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>///&nbsp; repeatedly, even after it's returned `nil`, and similarly it is safe to call<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(144, 45, 208);"><span style=""></span>///&nbsp; `next()` after `peek()` has returned `nil`.<br></div>
<div style="margin-top:0px;margin-bottom: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)">mutating</span> <span class="colour" style="color:rgb(187, 44, 162)">func</span> peek(n: <span class="colour" style="color:rgb(112, 61, 170)">Int</span> = <span class="colour" style="color:rgb(187, 44, 162)">default</span>) -&gt; <span class="colour" style="color:rgb(112, 61, 170)">Base</span>.<span class="colour" style="color:rgb(112, 61, 170)">Element</span>?<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;">}<br></div>
<div style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px;">&nbsp;</div>
<div>BufferedSequence is a class because the generate() function needs to mutate it. After the `first` property has been accessed, it needs to cache the generator it used for that so it can return it from the next call to generate(), but it also needs to nil out that cache at that time so it doesn't try and return the same generator instance a second time on a subsequent call to generate() (this way BufferedSequence can be written to support non-destructive iteration if the underlying sequence is non-destructive).<br></div>
<div>&nbsp;</div>
<div>I've already started sketching out an implementation as well. I believe it should be possible to optimize BufferedGenerator for a lookahead of 1 to avoid the heap allocation of an array.<br></div>
<div>&nbsp;</div>
<div>-Kevin Ballard<br></div>
</div>
<div>&nbsp;</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/4f09a76a6888ae88daffedac5fc3bff0411f1ea65860c7d8072e70319375bdd9/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d365d693a6d2232423b463a7c41787556445f4832385148384652345458644a753c61433d22364d22364075657a67773443514a61756f6c4a7864324b42665d2236446b464e6f636143613656366b477850336a7145403133314462737936535d4c6e6c64405e40517149657c615d6145455055565670734870396770353869646f645530727763783f42746469717973656d67635a6479774377487573576e43696a524944485b43735d22324977773834513d223648663c4131564c4635546567625654576345716a6877313071565d453c673f6a607c6136437232395f495a6a61387969674e65467e635a7f4f643a636665674a674b6d23344/open"><br></div>
</div>
</blockquote><blockquote type="cite"><div><div><span>_______________________________________________</span><br></div>
<div><span>swift-evolution mailing list</span><br></div>
<div><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br></div>
<div><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div>
</div>
</blockquote><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/05f2781b483a3d3b42ba614a3e94e550170e9deeb8f6f906e4b8c838866dc167/8647470737a3f2f25723030323431303e23647e23756e64676279646e2e65647f27766f2f60756e6f35707e6d396259433265684455633558795148445c66533c6143383a794056684d486975725a77645d674b46563b673959664b66554f675957537146356b497a5761673d223644327e69737472396a533138505454374b46656d493876666959334b6d47655654463b43585c45603d645d613d2232486671343150547b6835607239755b6d6a405b6d22364e456952346d22324c4d43456b4d4961794249333031484359407a75443b663276593668754653514d22364b43774a405163686c6643717254565457305f666037434d22324a454744765a436148373444763c4a52413e467b61514d23344d23344/open"><br></div>
</div>
<div>_______________________________________________<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>https://lists.swift.org/mailman/listinfo/swift-evolution<br></div>
</div>
</blockquote></div>
</blockquote><div>&nbsp;</div>
</body>
</html>