<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 4, 2015 at 2:38 PM, Donnacha Oisín Kidney <span dir="ltr"><<a href="mailto:oisin.kidney@gmail.com" target="_blank">oisin.kidney@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Currently, several of the methods on <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">SequenceType</span> in the standard library have lazy variants. <span style="color:rgb(61,29,129);font-family:Menlo;font-size:11px">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 <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px">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><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">public</span> <span style="color:#bb2ca2">struct</span> FlatMapOptionalGenerator<G: GeneratorType, Element>: <span style="color:#703daa">GeneratorType</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#000000"> </span><span style="color:#bb2ca2">private</span><span style="color:#000000"> </span><span style="color:#bb2ca2">let</span><span style="color:#000000"> f: </span>G<span style="color:#000000">.</span>Element<span style="color:#000000"> -> </span>Element<span style="color:#000000">?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:#000000"> </span>private<span style="color:#000000"> </span>var<span style="color:#000000"> g: </span><span style="color:#703daa">G</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">public</span> <span style="color:#bb2ca2">mutating</span> <span style="color:#bb2ca2">func</span> next() -> <span style="color:#703daa">Element</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">while</span> <span style="color:#bb2ca2">let</span> x = <span style="color:#4f8187">g</span>.<span style="color:#3d1d81">next</span>() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">if</span> <span style="color:#bb2ca2">let</span> y = <span style="color:#4f8187">f</span>(x) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> y</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:#000000"> </span>return<span style="color:#000000"> </span>nil</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">public</span> <span style="color:#bb2ca2">struct</span> FlatMapOptionalSequence<S: LazySequenceType, Element>: <span style="color:#703daa">LazySequenceType</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#000000"> </span><span style="color:#bb2ca2">private</span><span style="color:#000000"> </span><span style="color:#bb2ca2">let</span><span style="color:#000000"> f: </span>S<span style="color:#000000">.</span>Generator<span style="color:#000000">.</span>Element<span style="color:#000000"> -> </span>Element<span style="color:#000000">?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:#000000"> </span>private<span style="color:#000000"> </span>let<span style="color:#000000"> s: </span><span style="color:#703daa">S</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">public</span> <span style="color:#bb2ca2">func</span> generate() -> <span style="color:#4f8187">FlatMapOptionalGenerator</span><<span style="color:#703daa">S</span>.<span style="color:#703daa">Generator</span>, <span style="color:#703daa">Element</span>> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:#000000"> </span><span style="color:#bb2ca2">return</span><span style="color:#000000"> </span>FlatMapOptionalGenerator<span style="color:#000000">(f: </span>f<span style="color:#000000">, g: </span>s<span style="color:#000000">.</span><span style="color:#3d1d81">generate</span><span style="color:#000000">())</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:#bb2ca2">extension</span><span style="color:#000000"> </span>LazySequenceType<span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">public</span> <span style="color:#bb2ca2">func</span> flatMap<T>(transform: <span style="color:#703daa">Generator</span>.<span style="color:#703daa">Element</span> -> <span style="color:#703daa">T</span>?) -> <span style="color:#4f8187">FlatMapOptionalSequence</span><<span style="color:#703daa">Self</span>, <span style="color:#703daa">T</span>> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">return</span> <span style="color:#4f8187">FlatMapOptionalSequence</span>(f: transform, s: <span style="color:#bb2ca2">self</span>)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-family:Helvetica;font-size:12px"><br></span></div><div style="margin:0px;line-height:normal">Does this seem like a good idea?</div><div style="margin:0px;line-height:normal"></div></div></blockquote><div><br></div><div>Hi Oisin,</div><div><br></div><div>This is just an omission. Please submit a formal proposal as a pull request to the swift-evolution repository.</div><div><br></div><div>Dmitri</div></div><div><br></div>-- <br><div class="gmail_signature">main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/</div>
</div></div>