<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="">(forgot to cc the mailing list)</div><div class=""><br class=""></div>You can define it in terms of a&nbsp;<span class="" style="font-family: Menlo; font-size: 11px;">map</span>-<span class="" style="font-family: Menlo; font-size: 11px;">filter</span>-<span class="" style="font-family: Menlo; font-size: 11px;">map</span>&nbsp;chain, like this:<div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">extension</span><span class="" style="color: rgb(0, 0, 0);">&nbsp;</span>LazySequenceType<span class="" style="color: rgb(0, 0, 0);">&nbsp;{</span></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; line-height: normal; color: rgb(187, 44, 162);"><span class="" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span>@warn_unused_result</div><div class="" style="margin: 0px; line-height: normal;">&nbsp;&nbsp;<span class="" style="color: rgb(187, 44, 162);">public</span>&nbsp;<span class="" style="color: rgb(187, 44, 162);">func</span>&nbsp;flatMap&lt;T&gt;(transform: Elements.Generator.Element -&gt; T?)</div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(0, 0, 0);">&nbsp; &nbsp; -&gt;&nbsp;</span>LazyMapSequence<span class="" style="color: rgb(0, 0, 0);">&lt;</span>LazyFilterSequence<span class="" style="color: rgb(0, 0, 0);">&lt;</span>LazyMapSequence<span class="" style="color: rgb(0, 0, 0);">&lt;</span>Elements<span class="" style="color: rgb(0, 0, 0);">,&nbsp;</span>T<span class="" style="color: rgb(0, 0, 0);">?&gt;&gt;,&nbsp;</span>T<span class="" style="color: rgb(0, 0, 0);">&gt; {</span></div><div class="" style="margin: 0px; line-height: normal; color: rgb(187, 44, 162);"><span class="" style="color: rgb(0, 0, 0);">&nbsp; &nbsp; &nbsp;&nbsp;</span>return<span class="" style="color: rgb(0, 0, 0);">&nbsp;</span>self</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="" style="color: rgb(61, 29, 129);">map</span>(transform)</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="" style="color: rgb(61, 29, 129);">filter</span>&nbsp;{ opt&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;opt !=&nbsp;<span class="" style="color: rgb(187, 44, 162);">nil</span>&nbsp;}</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="" style="color: rgb(61, 29, 129);">map</span>&nbsp;{ notNil&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;notNil! }</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; }</div><div class="" style="margin: 0px; line-height: normal;">}</div></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">The version for&nbsp;<span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">LazyCollectionType</span>&nbsp;can be done similarly:</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;"><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(187, 44, 162);">extension</span><span class="" style="color: rgb(0, 0, 0);">&nbsp;</span>LazyCollectionType<span class="" style="color: rgb(0, 0, 0);">&nbsp;{</span></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class="">&nbsp;&nbsp;<br class="webkit-block-placeholder"></div><div class="" style="margin: 0px; line-height: normal; color: rgb(187, 44, 162);"><span class="" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;</span>@warn_unused_result</div><div class="" style="margin: 0px; line-height: normal;">&nbsp;&nbsp;<span class="" style="color: rgb(187, 44, 162);">public</span>&nbsp;<span class="" style="color: rgb(187, 44, 162);">func</span>&nbsp;flatMap&lt;T&gt;(transform: Elements.Generator.Element -&gt; T?)</div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(0, 0, 0);">&nbsp; &nbsp; -&gt;&nbsp;</span>LazyMapCollection<span class="" style="color: rgb(0, 0, 0);">&lt;</span>LazyFilterCollection<span class="" style="color: rgb(0, 0, 0);">&lt;</span>LazyMapCollection<span class="" style="color: rgb(0, 0, 0);">&lt;</span>Elements<span class="" style="color: rgb(0, 0, 0);">,&nbsp;</span>T<span class="" style="color: rgb(0, 0, 0);">?&gt;&gt;,&nbsp;</span>T<span class="" style="color: rgb(0, 0, 0);">&gt; {</span></div><div class="" style="margin: 0px; line-height: normal; color: rgb(187, 44, 162);"><span class="" style="color: rgb(0, 0, 0);">&nbsp; &nbsp; &nbsp;&nbsp;</span>return<span class="" style="color: rgb(0, 0, 0);">&nbsp;</span>self</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="" style="color: rgb(61, 29, 129);">map</span>(transform)</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="" style="color: rgb(61, 29, 129);">filter</span>&nbsp;{ opt&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;opt !=&nbsp;<span class="" style="color: rgb(187, 44, 162);">nil</span>&nbsp;}</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; &nbsp; &nbsp; &nbsp; .<span class="" style="color: rgb(61, 29, 129);">map</span>&nbsp;{ notNil&nbsp;<span class="" style="color: rgb(187, 44, 162);">in</span>&nbsp;notNil! }</div><div class="" style="margin: 0px; line-height: normal;">&nbsp; }</div><div class="" style="margin: 0px; line-height: normal;">}</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div></div><div class="" style="margin: 0px; line-height: normal;"><span class="">There seems to be no performance overhead vs the custom struct version in my (very preliminary) testing.</span></div><div class="" style="margin: 0px; line-height: normal;"><span class=""><br class=""></span></div><div class="" style="margin: 0px; line-height: normal;"><span class="">The version for a&nbsp;</span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">LazyCollectionType</span>&nbsp;with a&nbsp;<span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">BidirectionalIndexType</span>&nbsp;would rely on a similar&nbsp;<span class="" style="font-size: 11px; font-family: Menlo; color: rgb(112, 61, 170);">LazyFilterCollection</span>, but a&nbsp;<span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">BidirectionalFilterCollection</span>&nbsp;doesn’t exist (I think). Is that something that might be included in this proposal?</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div><div class="" style="margin: 0px; line-height: normal;">Oisin.</div><div class="" style="margin: 0px; line-height: normal;"><br class=""></div></div></div><div style=""><blockquote type="cite" class=""><div class="">On 6 Dec 2015, at 09:11, Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" class="">gribozavr@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote">On Fri, Dec 4, 2015 at 2:38 PM, Donnacha Oisín Kidney <span dir="ltr" class="">&lt;<a href="mailto:oisin.kidney@gmail.com" target="_blank" class="">oisin.kidney@gmail.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word" 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="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">struct</span> FlatMapOptionalGenerator&lt;G: GeneratorType, Element&gt;: <span style="color:rgb(112,61,170)" 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="" class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">private</span><span style="" class=""> </span><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> f: </span>G<span style="" class="">.</span>Element<span style="" class=""> -&gt; </span>Element<span style="" class="">?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)" class=""><span style="" class="">&nbsp; </span>private<span style="" class=""> </span>var<span style="" class=""> g: </span><span style="color:rgb(112,61,170)" class="">G</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">mutating</span> <span style="color:rgb(187,44,162)" class="">func</span> next() -&gt; <span style="color:rgb(112,61,170)" class="">Element</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">while</span> <span style="color:rgb(187,44,162)" class="">let</span> x = <span style="color:rgb(79,129,135)" class="">g</span>.<span style="color:rgb(61,29,129)" class="">next</span>() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">if</span> <span style="color:rgb(187,44,162)" class="">let</span> y = <span style="color:rgb(79,129,135)" 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="color:rgb(187,44,162)" 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="" class="">&nbsp; &nbsp; </span>return<span style="" 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="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">struct</span> FlatMapOptionalSequence&lt;S: LazySequenceType, Element&gt;: <span style="color:rgb(112,61,170)" 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="" class="">&nbsp; </span><span style="color:rgb(187,44,162)" class="">private</span><span style="" class=""> </span><span style="color:rgb(187,44,162)" class="">let</span><span style="" class=""> f: </span>S<span style="" class="">.</span>Generator<span style="" class="">.</span>Element<span style="" class=""> -&gt; </span>Element<span style="" class="">?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)" class=""><span style="" class="">&nbsp; </span>private<span style="" class=""> </span>let<span style="" class=""> s: </span><span style="color:rgb(112,61,170)" class="">S</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">func</span> generate() -&gt; <span style="color:rgb(79,129,135)" class="">FlatMapOptionalGenerator</span>&lt;<span style="color:rgb(112,61,170)" class="">S</span>.<span style="color:rgb(112,61,170)" class="">Generator</span>, <span style="color:rgb(112,61,170)" 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="" class="">&nbsp; &nbsp; </span><span style="color:rgb(187,44,162)" class="">return</span><span style="" class=""> </span>FlatMapOptionalGenerator<span style="" class="">(f: </span>f<span style="" class="">, g: </span>s<span style="" class="">.</span><span style="color:rgb(61,29,129)" class="">generate</span><span style="" 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="color:rgb(187,44,162)" class="">extension</span><span style="" class=""> </span>LazySequenceType<span style="" class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">func</span> flatMap&lt;T&gt;(transform: <span style="color:rgb(112,61,170)" class="">Generator</span>.<span style="color:rgb(112,61,170)" class="">Element</span> -&gt; <span style="color:rgb(112,61,170)" class="">T</span>?) -&gt; <span style="color:rgb(79,129,135)" class="">FlatMapOptionalSequence</span>&lt;<span style="color:rgb(112,61,170)" class="">Self</span>, <span style="color:rgb(112,61,170)" class="">T</span>&gt; {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">&nbsp; &nbsp; <span style="color:rgb(187,44,162)" class="">return</span> <span style="color:rgb(79,129,135)" class="">FlatMapOptionalSequence</span>(f: transform, s: <span style="color:rgb(187,44,162)" 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=""><br class=""></div></div></blockquote><div class=""><br class=""></div><div class="">Hi,</div><div class=""><br class=""></div><div class="">Thank you for the proposal.</div><div class=""><br class=""></div><div class="">Defining only one overload would cause the collection-ness of the input to be lost.&nbsp; Please take a look at the current flatMap() overloads in stdlib/public/core/FlatMap.swift: there's one in&nbsp;LazySequenceType, another one in&nbsp;LazyCollectionType, and one more in LazyCollectionType with bidirectional indices.</div><div class=""><br class=""></div><div class="">I'm also not a fan of introducing a specialized result type for this operation: given that we need three overloads, this approach would add six more types to the library (three sequences and three generators).&nbsp; Current flatMap() overloads compose existing types, but they rely on intermediate data structure being a sequence or a collection.&nbsp; Optional is not a sequence, so that exact approach won't work here.&nbsp; Can you think of another way we could combine existing types to express the result of this operation?</div></div><div class="gmail_extra"><br class=""></div>Dmitri<br clear="all" class=""><div class=""><br class=""></div>-- <br class=""><div class="gmail_signature">main(i,j){for(i=2;;i++){for(j=2;j&lt;i;j++){if(!(i%j)){j=0;break;}}if<br class="">(j){printf("%d\n",i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" target="_blank" class="">gribozavr@gmail.com</a>&gt;*/</div>
</div></div>
</div></blockquote></div><br class=""></body></html>