<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="">Now that I think about it, of course <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">CollectionOfZeroOrOne</span> would (or <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Optional</span>) have a random access index. I’ll update the proposal to include that.<br class=""><div class=""><br class=""></div><div class="">I really like the <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">CollectionOfZeroOrOne</span> solution. The semantics and types certainly make more sense. I’m not sure that it reduces the amount of code, though: the three extensions are still needed, plus an extra struct. (I suppose that’s not counting the extra version of <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">LazyFilterCollection</span>. Am I right in saying that that is intended to be added?)</div><div class=""><br class=""></div><div class="">Here’s what I have for the collection (I tried to mimic the standard library’s <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">CollectionOfOne</span>):</div><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> CollectionOfZeroOrOne<Element> : <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">CollectionType</span> {</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(187, 44, 162);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>public<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span>typealias<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Index = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bit</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span> element: <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=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">element</span> = element</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">var</span> startIndex: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Index</span> {</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="">return</span> .<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">Zero</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">var</span> endIndex: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Index</span> {</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="">switch</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">element</span> {</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="">case</span> .Some: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> .<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">One</span></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="">case</span> .None: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> .<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">Zero</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><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">func</span> generate() -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">GeneratorOfOne</span><<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; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </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>GeneratorOfOne<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">element</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=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">subscript</span>(position: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Index</span>) -> <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=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">if</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">case</span> .Zero = position, <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">let</span> result = <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">element</span> {</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="">return</span> result</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="">else</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(201, 49, 8);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">fatalError</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span>"Index out of range"<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=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""> <br class="webkit-block-placeholder"></p><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="">let</span> element: <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="">}</div></div><div class=""><br class=""></div><div class="">Does that seem reasonable?</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On 16 Dec 2015, at 02:29, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">First, I’d like to thank <a href="https://github.com/oisdk" class="">Oisin</a> for his proposal. It’s great to see people filling in gaps.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>* What is your evaluation of the proposal?<br class=""></div></div></div></div></div></blockquote><div class=""><br class=""></div>The basic idea is solid and obviously appropriate. I have some quibbles with the proposed solution.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>* Is the problem being addressed significant enough to warrant a change to Swift?<br class=""></div></div></div></div></div></blockquote><div class=""><br class=""></div>Yes; it’s a non-uniformity, nonuniformities create complexity for users.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>* Does this proposal fit well with the feel and direction of Swift?<br class=""></div></div></div></div></div></blockquote><div class=""><br class=""></div>Yes.</div><div class=""><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class=""><div class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>* How much effort did you put into your review? A glance, a quick reading, or an in-depth study?</div></div></div></div></div></blockquote><div class=""><br class=""></div>A quick reading, but I’m deeply familiar with the domain, having written the lazy facilities we have in the stdlib.</div><div class=""><br class=""></div><div class="">I don’t see why the proposal states, “<code style="color: rgb(51, 51, 51); box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">Optional</code><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class=""> </span><em style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255); box-sizing: border-box;" class="">probably</em><span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);" class=""> wouldn't have a </span><code style="color: rgb(51, 51, 51); box-sizing: border-box; font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 14px; padding: 0.2em 0px; margin: 0px; background-color: rgba(0, 0, 0, 0.0392157); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px;" class="">BidirectionalIndexType</code>“. It seems to me that as a collection, an optional’s index should be random access, just like CollectionOfOne’s.</div><div class=""><br class=""></div><div class="">I’d like to suggest a different implementation approach that not only handles the bidirectional issue but also probably reduces the amount of code involved: create a generic <font face="Menlo" class="">CollectionOfZeroOrOne<T></font> that wraps a <font face="Menlo" class="">T?</font>, and implement the <font face="Menlo" class="">x.flatmap(f)</font> where <font face="Menlo" class="">f</font> returns an optional as <font face="Menlo" class="">x.flatmap { CollectionOfZeroOrOne(f($0)) }</font></div><div class=""><br class=""></div><div class="">What do you think?</div><div class=""><br class=""></div><div class="">
-Dave<div class=""><br class=""></div><br class="Apple-interchange-newline">
</div>
<br class="">
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Lo8TP3b1oIn3yQXUt9zA1UCQfR-2BMBCuqnubTuDg47-2B2io8a8mbcdV7c5gNq39cuKYM2-2BfTUh4i5JbXhUlZrrpLueCNqrRmJpTNlo41r47H-2BLsRvk9JWY-2Be4ejsmbsPjPYBmy5-2Flw-2Fdd1klxO8NkZwz7oQl21spWA-2FymDneP18dW72v3gQXyl4nCiVi36i1-2F8L0Vj2kfW0p6kTxoo1vjIPSznwA4lGrNzvIXZc92ZEIM-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
</div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></body></html>