<div dir="ltr"><div class="gmail_extra"><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: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">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:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">struct</span> FlatMapOptionalGenerator<G: GeneratorType, Element>: <span style="color:rgb(112,61,170)">GeneratorType</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">private</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> f: </span>G<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)"> -> </span>Element<span style="color:rgb(0,0,0)">?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)"> </span>private<span style="color:rgb(0,0,0)"> </span>var<span style="color:rgb(0,0,0)"> g: </span><span style="color:rgb(112,61,170)">G</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">mutating</span> <span style="color:rgb(187,44,162)">func</span> next() -> <span style="color:rgb(112,61,170)">Element</span>? {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">while</span> <span style="color:rgb(187,44,162)">let</span> x = <span style="color:rgb(79,129,135)">g</span>.<span style="color:rgb(61,29,129)">next</span>() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">if</span> <span style="color:rgb(187,44,162)">let</span> y = <span style="color:rgb(79,129,135)">f</span>(x) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">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:rgb(0,0,0)"> </span>return<span style="color:rgb(0,0,0)"> </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:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">struct</span> FlatMapOptionalSequence<S: LazySequenceType, Element>: <span style="color:rgb(112,61,170)">LazySequenceType</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">private</span><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> f: </span>S<span style="color:rgb(0,0,0)">.</span>Generator<span style="color:rgb(0,0,0)">.</span>Element<span style="color:rgb(0,0,0)"> -> </span>Element<span style="color:rgb(0,0,0)">?</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)"> </span>private<span style="color:rgb(0,0,0)"> </span>let<span style="color:rgb(0,0,0)"> s: </span><span style="color:rgb(112,61,170)">S</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> generate() -> <span style="color:rgb(79,129,135)">FlatMapOptionalGenerator</span><<span style="color:rgb(112,61,170)">S</span>.<span style="color:rgb(112,61,170)">Generator</span>, <span style="color:rgb(112,61,170)">Element</span>> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)"> </span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> </span>FlatMapOptionalGenerator<span style="color:rgb(0,0,0)">(f: </span>f<span style="color:rgb(0,0,0)">, g: </span>s<span style="color:rgb(0,0,0)">.</span><span style="color:rgb(61,29,129)">generate</span><span style="color:rgb(0,0,0)">())</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:rgb(187,44,162)">extension</span><span style="color:rgb(0,0,0)"> </span>LazySequenceType<span style="color:rgb(0,0,0)"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">public</span> <span style="color:rgb(187,44,162)">func</span> flatMap<T>(transform: <span style="color:rgb(112,61,170)">Generator</span>.<span style="color:rgb(112,61,170)">Element</span> -> <span style="color:rgb(112,61,170)">T</span>?) -> <span style="color:rgb(79,129,135)">FlatMapOptionalSequence</span><<span style="color:rgb(112,61,170)">Self</span>, <span style="color:rgb(112,61,170)">T</span>> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:rgb(187,44,162)">return</span> <span style="color:rgb(79,129,135)">FlatMapOptionalSequence</span>(f: transform, s: <span style="color:rgb(187,44,162)">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"><br></div></div></blockquote><div><br></div><div>Hi,</div><div><br></div><div>Thank you for the proposal.</div><div><br></div><div>Defining only one overload would cause the collection-ness of the input to be lost. Please take a look at the current flatMap() overloads in stdlib/public/core/FlatMap.swift: there's one in LazySequenceType, another one in LazyCollectionType, and one more in LazyCollectionType with bidirectional indices.</div><div><br></div><div>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). Current flatMap() overloads compose existing types, but they rely on intermediate data structure being a sequence or a collection. Optional is not a sequence, so that exact approach won't work here. 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></div>Dmitri<br clear="all"><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>