<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 class="">Not a fan of anything that reminds me of Java streams.</div><div class=""><br class=""></div><div class="">Is the case where making an array from the sequence isn't possible significant enough for a new standard API?</div>

<br class=""><div class="">
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Lucida Grande';  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; ">Félix</span>
</div>
<br class=""><div><blockquote type="cite" class=""><div class="">Le 31 déc. 2015 à 20:50:40, Howard Lovatt via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class="">+1<br class=""><br class="">Sent from my iPad</div><div class=""><br class="">On 1 Jan 2016, at 11:16 AM, Kevin Ballard via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class="">


<title class=""></title>

<div class="">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 class=""></div>
<div class="">&nbsp;</div>
<div class="">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 class=""></div>
<div class="">&nbsp;</div>
<div class="">The proposed API looks like this:<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(144, 45, 208);" class="">/// A sequence adaptor that adds a nondestructive `first` property to any<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="">/// underlying sequence.<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="">/// - Note: If the underlying sequence is not destructively "consumed" by<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="">/// &nbsp; iteration, then neither is `BufferedSequence`.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span> BufferedSequence&lt;Base : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">SequenceType</span>&gt; : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">SequenceType</span> {<br class=""></div><div class=""><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp;&nbsp;<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> base: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Base</span>)</div></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><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="">&nbsp; &nbsp; </span>/// Returns ` BufferedGenerator` with a lookahead size of `1`.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> generate() -&gt; BufferedSequence.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">BufferedGenerator</span>&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Generator</span>&gt;<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><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="">&nbsp; &nbsp; </span>/// Returns the first element of the underlying sequence, **nondestructively**.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span> first: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Generator</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</span>? { get }<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; min-height: 13px;" class=""><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="">/// A generator adaptor that adds a nondestructive `peek()` method to any<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="">/// underlying generator.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span> BufferedGenerator&lt;Base : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">GeneratorType</span>&gt; : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">GeneratorType</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="">&nbsp; &nbsp; </span>/// Construct an instance that buffers access to an underlying generator.<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="">&nbsp; &nbsp; </span>/// - Parameter base: The underlying generator.<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="">&nbsp; &nbsp; </span>/// - Parameter lookahead: The amount of lookahead to allow. Default is `1`.<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="">&nbsp; &nbsp; </span>/// &nbsp; Values less than `1` will be treated the same as `1`.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> base: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Base</span>, lookahead: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">default</span>)<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><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="">&nbsp; &nbsp; </span>/// The amount of lookahead that this generator offers.<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="">&nbsp; &nbsp; </span>/// - Invariant: `lookahead &gt;= 1`.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> lookahead: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><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="">&nbsp; &nbsp; </span>/// Advance to the next element and return it, or `nil` if no next element exists.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #902dd0" 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="">&nbsp; &nbsp; </span>/// - Requires: Neither `next()` nor `peek()` have been applied to a copy of<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="">&nbsp; &nbsp; </span>/// &nbsp; `self` since the copy was made, and no preceding call to `self.next()` has<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="">&nbsp; &nbsp; </span>/// &nbsp; returned `nil`.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">mutating</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> next() -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</span>?<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><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="">&nbsp; &nbsp; </span>/// Returns the value that will be returned from subsequent calls to `next()`.<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="">&nbsp; &nbsp; </span>/// - Parameter n: The number of elements to look ahead. Default is `0`. A value<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="">&nbsp; &nbsp; </span>/// &nbsp; of `0` means to look at the next element.<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="">&nbsp; &nbsp; </span>/// - Precondition: `n &gt;= 0 &amp;&amp; n &lt; lookahead`.<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="">&nbsp; &nbsp; </span>/// - Requires: Neither `next()` nor `peek()` have been applied to a copy of<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="">&nbsp; &nbsp; </span>/// &nbsp; `self` since the copy was made, and no preceding call to `self.next()` has<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="">&nbsp; &nbsp; </span>/// &nbsp; returned `nil`.<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="">&nbsp; &nbsp; </span>/// - Note: It is safe to peek at values past the end of the underlying generator<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="">&nbsp; &nbsp; </span>///&nbsp; (`peek()` will return `nil` in such cases). It is also safe to call `peek()`<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="">&nbsp; &nbsp; </span>///&nbsp; repeatedly, even after it's returned `nil`, and similarly it is safe to call<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="">&nbsp; &nbsp; </span>///&nbsp; `next()` after `peek()` has returned `nil`.<br class=""></div><div style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">mutating</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> peek(n: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span> = <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">default</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Base</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Element</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; min-height: 13px;" class=""><br class=""></div><div class="">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 class=""></div>
<div class="">&nbsp;</div>
<div class="">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 class=""></div>
<div class="">&nbsp;</div>
<div class="">-Kevin Ballard<br class=""></div>
</div>
<div class="">&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;" class="">



</div></blockquote><blockquote type="cite" class=""><div class=""><span class="">_______________________________________________</span><br class=""><span class="">swift-evolution mailing list</span><br class=""><span class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a></span><br class=""><span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br class=""></div></blockquote>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k7YiFkVEoWYWsA6eKyZga7-2F4rnyst2iZ31XPT4GKfeM9xffY9CkMgUVD6KSXLe0mTm1-2Bhv14QPtk8ep2yUkmJPk-2FNeY2d-2BLMCeKMiqIB930AHSIpzE4k6rV9fxEVSA-2FKsGJPachlFsqRTVT7Pof0GC-2BJEGtVJcA87Dt6LZB1NvkQA-3D-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;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>