<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 <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> 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);"> </span>LazySequenceType<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""> <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);"> </span>@warn_unused_result</div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(187, 44, 162);">public</span> <span class="" style="color: rgb(187, 44, 162);">func</span> flatMap<T>(transform: Elements.Generator.Element -> T?)</div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(0, 0, 0);"> -> </span>LazyMapSequence<span class="" style="color: rgb(0, 0, 0);"><</span>LazyFilterSequence<span class="" style="color: rgb(0, 0, 0);"><</span>LazyMapSequence<span class="" style="color: rgb(0, 0, 0);"><</span>Elements<span class="" style="color: rgb(0, 0, 0);">, </span>T<span class="" style="color: rgb(0, 0, 0);">?>>, </span>T<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; line-height: normal; color: rgb(187, 44, 162);"><span class="" style="color: rgb(0, 0, 0);"> </span>return<span class="" style="color: rgb(0, 0, 0);"> </span>self</div><div class="" style="margin: 0px; line-height: normal;"> .<span class="" style="color: rgb(61, 29, 129);">map</span>(transform)</div><div class="" style="margin: 0px; line-height: normal;"> .<span class="" style="color: rgb(61, 29, 129);">filter</span> { opt <span class="" style="color: rgb(187, 44, 162);">in</span> opt != <span class="" style="color: rgb(187, 44, 162);">nil</span> }</div><div class="" style="margin: 0px; line-height: normal;"> .<span class="" style="color: rgb(61, 29, 129);">map</span> { notNil <span class="" style="color: rgb(187, 44, 162);">in</span> notNil! }</div><div class="" style="margin: 0px; line-height: normal;"> }</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 <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">LazyCollectionType</span> 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);"> </span>LazyCollectionType<span class="" style="color: rgb(0, 0, 0);"> {</span></div><div style="margin: 0px; line-height: normal; min-height: 13px;" class=""> <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);"> </span>@warn_unused_result</div><div class="" style="margin: 0px; line-height: normal;"> <span class="" style="color: rgb(187, 44, 162);">public</span> <span class="" style="color: rgb(187, 44, 162);">func</span> flatMap<T>(transform: Elements.Generator.Element -> T?)</div><div class="" style="margin: 0px; line-height: normal;"><span class="" style="color: rgb(0, 0, 0);"> -> </span>LazyMapCollection<span class="" style="color: rgb(0, 0, 0);"><</span>LazyFilterCollection<span class="" style="color: rgb(0, 0, 0);"><</span>LazyMapCollection<span class="" style="color: rgb(0, 0, 0);"><</span>Elements<span class="" style="color: rgb(0, 0, 0);">, </span>T<span class="" style="color: rgb(0, 0, 0);">?>>, </span>T<span class="" style="color: rgb(0, 0, 0);">> {</span></div><div class="" style="margin: 0px; line-height: normal; color: rgb(187, 44, 162);"><span class="" style="color: rgb(0, 0, 0);"> </span>return<span class="" style="color: rgb(0, 0, 0);"> </span>self</div><div class="" style="margin: 0px; line-height: normal;"> .<span class="" style="color: rgb(61, 29, 129);">map</span>(transform)</div><div class="" style="margin: 0px; line-height: normal;"> .<span class="" style="color: rgb(61, 29, 129);">filter</span> { opt <span class="" style="color: rgb(187, 44, 162);">in</span> opt != <span class="" style="color: rgb(187, 44, 162);">nil</span> }</div><div class="" style="margin: 0px; line-height: normal;"> .<span class="" style="color: rgb(61, 29, 129);">map</span> { notNil <span class="" style="color: rgb(187, 44, 162);">in</span> notNil! }</div><div class="" style="margin: 0px; line-height: normal;"> }</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 </span><span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">LazyCollectionType</span> with a <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">BidirectionalIndexType</span> would rely on a similar <span class="" style="font-size: 11px; font-family: Menlo; color: rgb(112, 61, 170);">LazyFilterCollection</span>, but a <span class="" style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;">BidirectionalFilterCollection</span> 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 <<a href="mailto:gribozavr@gmail.com" class="">gribozavr@gmail.com</a>> 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=""><<a href="mailto:oisin.kidney@gmail.com" target="_blank" class="">oisin.kidney@gmail.com</a>></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 <span style="color:rgb(112,61,170);font-family:Menlo;font-size:11px" class="">SequenceType</span> in the standard library have lazy variants. <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 <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<G: GeneratorType, Element>: <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=""> </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=""> -> </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=""> </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=""> <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() -> <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=""> <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=""> <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=""> <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=""> }</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;color:rgb(187,44,162)" class=""><span style="" class=""> </span>return<span style="" class=""> </span>nil</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" 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<S: LazySequenceType, Element>: <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=""> </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=""> -> </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=""> </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=""> <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">func</span> generate() -> <span style="color:rgb(79,129,135)" class="">FlatMapOptionalGenerator</span><<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>> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)" class=""><span style="" class=""> </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=""> }</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=""> <span style="color:rgb(187,44,162)" class="">public</span> <span style="color:rgb(187,44,162)" class="">func</span> flatMap<T>(transform: <span style="color:rgb(112,61,170)" class="">Generator</span>.<span style="color:rgb(112,61,170)" class="">Element</span> -> <span style="color:rgb(112,61,170)" class="">T</span>?) -> <span style="color:rgb(79,129,135)" class="">FlatMapOptionalSequence</span><<span style="color:rgb(112,61,170)" class="">Self</span>, <span style="color:rgb(112,61,170)" class="">T</span>> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""> <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=""> }</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. 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 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). 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 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<i;j++){if(!(i%j)){j=0;break;}}if<br class="">(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank" class="">gribozavr@gmail.com</a>>*/</div>
</div></div>
</div></blockquote></div><br class=""></body></html>