<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Wed, Sep 27, 2017 at 10:36 AM Ben Cohen 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;line-break:after-white-space"><br><div><br><blockquote type="cite"><div>On Sep 25, 2017, at 2:12 AM, Alwyn Concessao via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_4809176130377118644Apple-interchange-newline"><div><div style="font-family:&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">mutating func removeElementInSubrange(_ elementToBeRemoved:Element,in range:Range&lt;Index&gt;){</div><div style="font-family:&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">//check if elementoBeRemoved exists; if yes, check if the index of elementToBeRemoved is part of the subrange, if yes then remove else don&#39;t remove.</div><div style="font-family:&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:&quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">}</div></div></blockquote></div><br></div><div style="word-wrap:break-word;line-break:after-white-space"><div>Hi Alwyn,</div><div><br></div><div>In general, we try to avoid methods on Collection in the standard library that take a limiting range to operate on. </div></div></blockquote><div><br></div><div>This is something of a related aside, but since you mentioned it, is this also the reason that there is no method like `index(of element: Element, from index: Index) -&gt; Index?`?</div><div><br></div><div>In those situations where I&#39;ve needed to do that (e.g., a while loop that skips from match to match) I end up falling back to slices, but historically it&#39;s been somewhat awkward to write compared to the code I would write if such a function exists.</div><div><br></div><div>That being said, I don&#39;t believe I&#39;ve tried it since Swift 4 introduced the new partial range operators so it&#39;s possible that those simplify the pattern quite a bit.</div><div><br></div><div> </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;line-break:after-white-space"><div>Instead, the user can slice the collection using the range, then call the mutating method on the slice. If done directly on the subscript, this has the effect of mutating the parent collection:</div><div><br></div><div><div style="margin:0px;font-stretch:normal;font-size:16px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="color:#ba2da2">var</span> a = <span style="color:#703daa">Array</span>(<span style="color:#272ad8">0</span>..&lt;<span style="color:#272ad8">10</span>)</div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="color:#ba2da2">let</span> isEven = { $0%<span style="color:#272ad8">2</span> == <span style="color:#272ad8">0</span> }</div><div style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px"><br></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="color:#4f8187">a</span>[<span style="color:#272ad8">3</span>...].<span style="color:#31595d">remove</span>(where: <span style="color:#4f8187">isEven</span>)</div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(0,132,0)">// a = [0, 1, 2, 3, 5, 7, 9]</div><div><br></div></div></div><div>The downside of this today is that it can be inefficient, because the mutated slice might be copied as part of the mutaate-and-write-back. But there are changes slated for Swift 5 that should allow this mutation to happen in place.</div><div><br></div><div><br></div></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></div>