Folks are focusing on Swift 3 and non-critical additive things aren&#39;t going to get attention. It is best to avoid discussion of non-critical additive proposals until the focus returns to future looking (e.g post Swift 3).<br><div class="gmail_quote"><div dir="ltr">On Thu, Jul 7, 2016 at 1:48 PM Daryle Walker via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I doesn’t seem that anyone noticed the previous post.  Here’s an inline copy:</div><div><br></div><div><h1 style="font-size:2.25em;margin-right:0px;margin-bottom:16px;margin-left:0px;line-height:1.2;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255);margin-top:0px!important">Implement a mismatch algorithm, equivalent to std::mismatch() in C++</h1><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)"><li>Proposal: <a href="https://gist.github.com/CTMacUser/NNNN-filename.md" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">SE-NNNN</a></li><li>Author: <a href="https://github.com/CTMacUser" style="background-color:transparent;color:rgb(64,120,192);outline-width:0px" target="_blank">Daryle Walker</a></li><li>Status: <span>Awaiting review</span></li><li>Review manager: TBD</li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#introduction" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Introduction</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">This proposal is to add difference detection to Swift&#39;s standard library collections.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">Swift-evolution thread: <a href="http://news.gmane.org/gmane.comp.lang.swift.evolution" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none" target="_blank">Discussion thread topic for that proposal</a></p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#motivation" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Motivation</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">Finding where two similar collections differ is needed in algorithms that have different policies on handling the common part versus the uncommon parts. Similar tests already exist in the standard library: the <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">elementsEqual</code> methods in <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">Sequence</code> for instance; the methods can indicate two sequences are different but not where they diverged. Flipping it around, it means that sequence equivalence, and several other sequence methods, can be expressed in terms of mismatch-finding. However, returning the divergence point means returning references to the diverging elements, which means an index, which means that collections are required instead of plain sequences.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#proposed-solution" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Proposed solution</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">The Swift standard library should provide generic implementations of the &quot;mismatch&quot; algorithm for forward searches on prefixes and backward searches on suffixes. The forward/prefix form is called <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">diverges(from: isEquivalent:)</code>. The backward/suffix form is called <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">converges(with: isEquivalent:)</code>, and is present only when the collection type supports bidirectional indexing. If the collection&#39;s element type conforms to <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">Equatable</code>, there variants of the method(s) that drop the second argument and instead use <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">==</code> for the equivalency test.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#detailed-design" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Detailed design</h2><h3 style="margin-top:1em;margin-bottom:16px;line-height:1.43;font-size:1.5em;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#divergesfromisequivalent-and-divergesfrom" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2" target="_blank"><u></u><u></u><u></u><u></u></a><code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:inherit;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">diverges(from:isEquivalent:)</code> and <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:inherit;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">diverges(from:)</code></h3><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">Forward mismatching on prefixes will be added to the <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">Collection</code> protocol requirements with a default implementation. Its variant will extend <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">Collection</code> with a default implementation. These methods will have the following declarations:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)"><pre style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span style="color:rgb(167,29,93)">protocol</span> Collection {
    <span style="color:rgb(150,152,150)">// existing declarations</span>

    <span style="color:rgb(150,152,150)">/**</span>
<span style="color:rgb(150,152,150)">        Compares the collection against a given collection element-wise until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its end index.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        The predicate must be an equivalence relation over the elements.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        - parameter from: A collection to compare to this one.</span>
<span style="color:rgb(150,152,150)">        - parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.</span>
<span style="color:rgb(150,152,150)">        - returns: A pair of indices, indicating where the two collections mismatched.  The first member is the index of the element that mismatched in this collection, the second is the index of the element that mismatched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection&#39;s end index.  If both tuple members are at their respective collection&#39;s end index, then the collections were equivalent.</span>
<span style="color:rgb(150,152,150)">        - complexity: `min(count, from.count)` comparisons.</span>
<span style="color:rgb(150,152,150)">        - throws: Whatever `isEquivalent` may throw.</span>
<span style="color:rgb(150,152,150)">    */</span>
    <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">diverges</span>&lt;PossiblePrefix: Collection <span style="color:rgb(167,29,93)">where</span> PossiblePrefix<span style="color:rgb(167,29,93)">.</span>Iterator<span style="color:rgb(167,29,93)">.</span>Element <span style="color:rgb(167,29,93)">==</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element&gt;(<span style="color:rgb(121,93,163)">from</span> <span>possiblePrefix</span>: PossiblePrefix, isEquivalent: (Iterator<span style="color:rgb(167,29,93)">.</span>Element, Iterator<span style="color:rgb(167,29,93)">.</span>Element) throws <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">Bool</span>) rethrows -&gt; (Index, PossiblePrefix.Index)
}

<span style="color:rgb(167,29,93)">extension</span> Collection {
    <span style="color:rgb(150,152,150)">/**</span>
<span style="color:rgb(150,152,150)">        Compares the collection against a given collection element-wise until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its end index.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        The predicate must be an equivalence relation over the elements.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        - parameter from: A collection to compare to this one.</span>
<span style="color:rgb(150,152,150)">        - parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.</span>
<span style="color:rgb(150,152,150)">        - returns: A pair of indices, indicating where the two collections mismatched.  The first member is the index of the element that mismatched in this collection, the second is the index of the element that mismatched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection&#39;s end index.  If both tuple members are at their respective collection&#39;s end index, then the collections were equivalent.</span>
<span style="color:rgb(150,152,150)">        - complexity: `min(count, from.count)` comparisons.</span>
<span style="color:rgb(150,152,150)">        - throws: Whatever `isEquivalent` may throw.</span>
<span style="color:rgb(150,152,150)">    */</span>
    <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">diverges</span>&lt;PossiblePrefix: Collection <span style="color:rgb(167,29,93)">where</span> PossiblePrefix<span style="color:rgb(167,29,93)">.</span>Iterator<span style="color:rgb(167,29,93)">.</span>Element <span style="color:rgb(167,29,93)">==</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element&gt;(<span style="color:rgb(121,93,163)">from</span> <span>possiblePrefix</span>: PossiblePrefix, isEquivalent: (Iterator<span style="color:rgb(167,29,93)">.</span>Element, Iterator<span style="color:rgb(167,29,93)">.</span>Element) throws <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">Bool</span>) rethrows -&gt; (Index, PossiblePrefix.Index)
}

<span style="color:rgb(167,29,93)">extension</span> Collection <span style="color:rgb(167,29,93)">where</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element: <span style="color:rgb(0,134,179)">Equatable</span> {
    <span style="color:rgb(150,152,150)">/**</span>
<span style="color:rgb(150,152,150)">        Compares the collection against a given collection element-wise until either corresponding elements are no longer equal, or at least one collection reaches its end index.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        - parameter from: A collection to compare to this one.</span>
<span style="color:rgb(150,152,150)">        - returns: A pair of indices, indicating where the two collections mismatched.  The first member is the index of the element that mismatched in this collection, the second is the index of the element that mismatched in the given collection.  If the testing stopped because the collections were of different lengths, but were equal until that point, then exactly one member of the tuple will be at its collection&#39;s end index.  If both tuple members are at their respective collection&#39;s end index, then the collections were equal.</span>
<span style="color:rgb(150,152,150)">        - complexity: `min(count, from.count)` comparisons.</span>
<span style="color:rgb(150,152,150)">    */</span>
    <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">diverges</span>&lt;PossiblePrefix: Collection <span style="color:rgb(167,29,93)">where</span> PossiblePrefix<span style="color:rgb(167,29,93)">.</span>Iterator<span style="color:rgb(167,29,93)">.</span>Element <span style="color:rgb(167,29,93)">==</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element&gt;(<span style="color:rgb(121,93,163)">from</span> <span>possiblePrefix</span>: PossiblePrefix) <span style="color:rgb(167,29,93)">-&gt;</span> (Index, PossiblePrefix.Index)
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">I don&#39;t know if we should insist that at least one (or both) of the collections tested should be finite. I don&#39;t know if the results should be discardable.</p><h3 style="margin-top:1em;margin-bottom:16px;line-height:1.43;font-size:1.5em;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#convergeswithisequivalent-and-convergeswith" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2" target="_blank"><u></u><u></u><u></u><u></u></a><code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:inherit;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">converges(with:isEquivalent:)</code> and <code style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:inherit;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">converges(with:)</code></h3><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">Backward mismatching on suffixes will be added to the <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">BidirectionalCollection</code> protocol requirements with a default implementation. Its variant will extend <code style="font-family:Consolas,&#39;Liberation Mono&#39;,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">BidirectionalCollection</code> with a default implementation. These methods will have the following declarations:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)"><pre style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span style="color:rgb(167,29,93)">protocol</span> BidirectionalCollection {
    <span style="color:rgb(150,152,150)">// existing declarations</span>

    <span style="color:rgb(150,152,150)">/**</span>
<span style="color:rgb(150,152,150)">        Compares the collection against a given collection element-wise and backwards until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its start index.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        Both collections must be finite.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        The predicate must be an equivalence relation over the elements.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        - parameter with: A collection to compare to this one.</span>
<span style="color:rgb(150,152,150)">        - parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.</span>
<span style="color:rgb(150,152,150)">        - returns: A pair of indices, indicating where the two collections started to match.  The first member is the index of the element that suffix-matched in this collection, the second is the index of the element that suffix-matched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection&#39;s start index.  If both tuple members are at their respective collection&#39;s start index, then the collections were equivalent.  If both tuple members are at their respective collection&#39;s end index, then either the collections&#39; last elements differ or at least one collection was empty.</span>
<span style="color:rgb(150,152,150)">        - complexity: `min(count, from.count)` comparisons.</span>
<span style="color:rgb(150,152,150)">        - throws: Whatever `isEquivalent` may throw.</span>
<span style="color:rgb(150,152,150)">    */</span>
    <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">converges</span>&lt;PossibleSuffix: BidirectionalCollection <span style="color:rgb(167,29,93)">where</span> PossibleSuffix<span style="color:rgb(167,29,93)">.</span>Iterator<span style="color:rgb(167,29,93)">.</span>Element <span style="color:rgb(167,29,93)">==</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element&gt;(<span style="color:rgb(121,93,163)">with</span> <span>possibleSuffix</span>: PossibleSuffix, isEquivalent: (Iterator<span style="color:rgb(167,29,93)">.</span>Element, Iterator<span style="color:rgb(167,29,93)">.</span>Element) throws <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">Bool</span>) rethrows -&gt; (Index, PossibleSuffix.Index)
}

<span style="color:rgb(167,29,93)">extension</span> BidirectionalCollection {
    <span style="color:rgb(150,152,150)">/**</span>
<span style="color:rgb(150,152,150)">        Compares the collection against a given collection element-wise and backwards until either corresponding elements are no longer equivalent, using the given predicate as the equivalence test, or at least one collection reaches its start index.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        Both collections must be finite.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        The predicate must be an equivalence relation over the elements.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        - parameter with: A collection to compare to this one.</span>
<span style="color:rgb(150,152,150)">        - parameter isEquivalent: A predicate the returns `true` if and only if its two arguments are equivalent.</span>
<span style="color:rgb(150,152,150)">        - returns: A pair of indices, indicating where the two collections started to match.  The first member is the index of the element that suffix-matched in this collection, the second is the index of the element that suffix-matched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection&#39;s start index.  If both tuple members are at their respective collection&#39;s start index, then the collections were equivalent.  If both tuple members are at their respective collection&#39;s end index, then either the collections&#39; last elements differ or at least one collection was empty.</span>
<span style="color:rgb(150,152,150)">        - complexity: `min(count, from.count)` comparisons.</span>
<span style="color:rgb(150,152,150)">        - throws: Whatever `isEquivalent` may throw.</span>
<span style="color:rgb(150,152,150)">    */</span>
    <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">converges</span>&lt;PossibleSuffix: BidirectionalCollection <span style="color:rgb(167,29,93)">where</span> PossibleSuffix<span style="color:rgb(167,29,93)">.</span>Iterator<span style="color:rgb(167,29,93)">.</span>Element <span style="color:rgb(167,29,93)">==</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element&gt;(<span style="color:rgb(121,93,163)">with</span> <span>possibleSuffix</span>: PossibleSuffix, isEquivalent: (Iterator<span style="color:rgb(167,29,93)">.</span>Element, Iterator<span style="color:rgb(167,29,93)">.</span>Element) throws <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">Bool</span>) rethrows -&gt; (Index, PossibleSuffix.Index)
}

<span style="color:rgb(167,29,93)">extension</span> BidirectionalCollection <span style="color:rgb(167,29,93)">where</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element: <span style="color:rgb(0,134,179)">Equatable</span> {
    <span style="color:rgb(150,152,150)">/**</span>
<span style="color:rgb(150,152,150)">        Compares the collection against a given collection element-wise and backwards until either corresponding elements are no longer equal, or at least one collection reaches its start index.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        Both collections must be finite.</span>
<span style="color:rgb(150,152,150)"></span>
<span style="color:rgb(150,152,150)">        - parameter with: A collection to compare to this one.</span>
<span style="color:rgb(150,152,150)">        - returns: A pair of indices, indicating where the two collections started to match.  The first member is the index of the element that suffix-matched in this collection, the second is the index of the element that suffix-matched in the given collection.  If the testing stopped because the collections were of different lengths, but were equivalent until that point, then exactly one member of the tuple will be at its collection&#39;s start index.  If both tuple members are at their respective collection&#39;s start index, then the collections were equivalent.  If both tuple members are at their respective collection&#39;s end index, then either the collections&#39; last elements differ or at least one collection was empty.</span>
<span style="color:rgb(150,152,150)">        - complexity: `min(count, from.count)` comparisons.</span>
<span style="color:rgb(150,152,150)">    */</span>
    <span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">converges</span>&lt;PossibleSuffix: BidirectionalCollection <span style="color:rgb(167,29,93)">where</span> PossibleSuffix<span style="color:rgb(167,29,93)">.</span>Iterator<span style="color:rgb(167,29,93)">.</span>Element <span style="color:rgb(167,29,93)">==</span> Iterator<span style="color:rgb(167,29,93)">.</span>Element&gt;(<span style="color:rgb(121,93,163)">with</span> <span>possibleSuffix</span>: PossibleSuffix) <span style="color:rgb(167,29,93)">-&gt;</span> (Index, PossibleSuffix.Index)
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">I don&#39;t know if the results should be discardable.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#impact-on-existing-code" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Impact on existing code</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">The comparison methods are an additive feature that doesn’t impact existing code.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc#alternatives-considered" style="background-color:transparent;color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Alternatives considered</h2><div style="margin-top:0px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255);margin-bottom:0px!important">The alternative is to not include these methods in the standard library, but the user will need to develop their custom implementation of the mismatch algorithms tailored for their needs.</div></div><div><br></div><br><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div>— </div><div>Daryle Walker<br>Mac, Internet, and Video Game Junkie<br>darylew AT mac DOT com </div></div>
</div>
<br><div><blockquote type="cite"><div>On Jul 6, 2016, at 5:01 AM, Daryle Walker &lt;<a href="mailto:darylew@mac.com" target="_blank">darylew@mac.com</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word"><div>I mentioned in other messages about adding permutations and combinations (probably as generators/iterators) to the standard library.  I tried making sample implementations and a proposal, but it transitioned to adapting C++’s “is_permutation,” “next_permutation,” and “prev_permutation” instead.  The sample implementation of “is_permutation” I saw at &lt;<a href="http://en.cppreference.com/w/cpp/algorithm/is_permutation" target="_blank">http://en.cppreference.com/w/cpp/algorithm/is_permutation</a>&gt; involves the “mismatch” function, which we also don’t seem to have.  Since that function seems like a small enough bite the chew, I finally made a proposal at &lt;<a href="https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc" target="_blank">https://gist.github.com/CTMacUser/c1a0d7ac60cf827184c33e8768a23dfc</a>&gt;.  (The Gist is currently flagged as secret.)</div><div><br></div><div>Oh, it seems that everyone here has moved on to Swift 3, and so has my third-party documentation program.  Unfortunately, I still on non-beta code, which means Swift 2.2.  So I took some time having to translate concepts between the versions, including new names.</div><div><br></div><div>The name “mismatch” didn’t seem Swift-y enough since it doesn’t describe what’s happening from a Swift programming perspective.  I tried:</div><div><br></div><div>* commonPrefixUntil / commonSuffixUntil</div><div>* elementsEqualUntil / elementsEqualSince</div><div>* elementsShared(until:) / elementsShared(since:)</div><div>* elementsDiverge / elementsConverge</div><div><br></div><div>No, those parameters on the third one don’t make sense.  The last one inspired me to trim the fat and just use “diverge(from:)”.  Since we use indexes here like C++’s iterators, that was the best choice for a return type that allows the users to take the results in an inspecting manner or mutating manner.  But Swift’s model doesn’t handle reversed collections like C++ does, so I need a separate routine for mismatching with reverse iterators, i.e. searching backwards with indexes.  Since I used the “diverge” name for the forward search, I flipped it to “converge(with:)” for the reverse/suffix search.  The returns aren’t used in quite the same manner since I have to avoid needing a before-the-start index.</div><div><br></div><div>A lot of the format was badly copied from the rotate/reverse proposal (&lt;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0078-rotate-algorithm.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0078-rotate-algorithm.md</a>&gt;).  Looking for opinions, mistakes/clean-up, anything major missing?...</div><div><br></div><div>
<div style="letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div>— </div><div>Daryle Walker<br>Mac, Internet, and Video Game Junkie<br>darylew AT mac DOT com </div></div>
</div>
<br></div></div></blockquote></div><br></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>