<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>+1<br><br>Sent from my iPad</div><div><br>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><br></div><blockquote type="cite"><div>


<title></title>

<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>
<p 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></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);">/// underlying sequence.<br></p>
<p 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></p>
<p 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></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">class</span> BufferedSequence&lt;Base : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">SequenceType</span>&gt; : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">SequenceType</span> {<br></p><div><p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">_</span> base: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Base</span>)</p></div>


<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// Returns ` BufferedGenerator` with a lookahead size of `1`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> generate() -&gt; BufferedSequence.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187">BufferedGenerator</span>&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Generator</span>&gt;<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// Returns the first element of the underlying sequence, **nondestructively**.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">var</span> first: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Element</span>? { get }<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p 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></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);">/// underlying generator.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">struct</span> BufferedGenerator&lt;Base : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">GeneratorType</span>&gt; : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">GeneratorType</span> {<br></p>

<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// Construct an instance that buffers access to an underlying generator.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Parameter base: The underlying generator.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Parameter lookahead: The amount of lookahead to allow. Default is `1`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// &nbsp; Values less than `1` will be treated the same as `1`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">_</span> base: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Base</span>, lookahead: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">default</span>)<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// The amount of lookahead that this generator offers.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Invariant: `lookahead &gt;= 1`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">let</span> lookahead: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// Advance to the next element and return it, or `nil` if no next element exists.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #902dd0">///</span><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Requires: Neither `next()` nor `peek()` have been applied to a copy of<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// &nbsp; `self` since the copy was made, and no preceding call to `self.next()` has<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// &nbsp; returned `nil`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">mutating</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> next() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Element</span>?<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// Returns the value that will be returned from subsequent calls to `next()`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Parameter n: The number of elements to look ahead. Default is `0`. A value<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// &nbsp; of `0` means to look at the next element.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Precondition: `n &gt;= 0 &amp;&amp; n &lt; lookahead`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Requires: Neither `next()` nor `peek()` have been applied to a copy of<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// &nbsp; `self` since the copy was made, and no preceding call to `self.next()` has<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// &nbsp; returned `nil`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>/// - Note: It is safe to peek at values past the end of the underlying generator<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>///&nbsp; (`peek()` will return `nil` in such cases). It is also safe to call `peek()`<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>///&nbsp; repeatedly, even after it's returned `nil`, and similarly it is safe to call<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(144, 45, 208);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000">&nbsp; &nbsp; </span>///&nbsp; `next()` after `peek()` has returned `nil`.<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">mutating</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">func</span> peek(n: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Int</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">default</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">Element</span>?<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;">}<br></p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;"><br></p><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>

<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Vm9j-2B2K6zLqxUFTO82XA8HV2TThDz5lA3-2F-2Fpeujw7DSAjqeoLzh4BKbV-2FdKFnocAc1V6fKwX0czAE013Adrs9VSMlnlDPNPqAiulQmAEUPUVvpCx0iw05hidoT5prgs8OrddyqycemgSjtyGsGxuSgNciZBIDXKsS-2Byw78T1-2Fh6L1QFL6UdegRVTgCuajx71pQVM5l7ojpl1Fs22YOYjj1xiiGnEvnSzOo4jcfeGjGk-3D" alt="" width="1" height="1" border="0" style="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><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>