<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="">Currently, several of the methods on&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">SequenceType</span>&nbsp;in the standard library have lazy variants.&nbsp;<span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px;" class="">flatMap</span>, though, (seems) to have a version missing: while there’s a lazy version for nested sequences, there’s no lazy version for sequences of&nbsp;<span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span>s. Is there maybe a reason for this that I haven’t thought of? At any rate, here’s what I had in mind:<div class=""><br class=""></div><div class=""><div style="margin: 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> FlatMapOptionalGenerator&lt;G: GeneratorType, Element&gt;: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">GeneratorType</span> {</div><div style="margin: 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: #000000" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> f: </span>G<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> -&gt; </span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; </span>private<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>var<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> g: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">G</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&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="">Element</span>? {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">while</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> x = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">g</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">next</span>() {</div><div style="margin: 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="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> y = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">f</span>(x) {</div><div style="margin: 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> y</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span>return<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>nil</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 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> FlatMapOptionalSequence&lt;S: LazySequenceType, Element&gt;: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">LazySequenceType</span> {</div><div style="margin: 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: #000000" class="">&nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">private</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> f: </span>S<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Generator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> -&gt; </span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">?</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; </span>private<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>let<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> s: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">S</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&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; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">FlatMapOptionalGenerator</span>&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">S</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>&gt; {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>FlatMapOptionalGenerator<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(f: </span>f<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, g: </span>s<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">generate</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">())</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 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; color: #000000" class=""> </span>LazySequenceType<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&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> flatMap&lt;T&gt;(transform: <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> -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>?) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">FlatMapOptionalSequence</span>&lt;<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Self</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">T</span>&gt; {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">FlatMapOptionalSequence</span>(f: transform, s: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class="">Does this seem like a good idea?</div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">Oisin.</div></body></html>