<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Sep 27, 2017, at 12:44 PM, Tony Allevato &lt;<a href="mailto:tony.allevato@gmail.com" class="">tony.allevato@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Sep 27, 2017 at 10:36 AM Ben Cohen via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></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" class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Sep 25, 2017, at 2:12 AM, Alwyn Concessao via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_4809176130377118644Apple-interchange-newline"><div class=""><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" class="">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" class=""><br class=""></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" class="">//check if elementoBeRemoved exists; if yes, check if the index of elementToBeRemoved is part of the subrange, if yes then remove else don'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" class=""><br class=""></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" class="">}</div></div></blockquote></div><br class=""></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class="">Hi Alwyn,</div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div></div></div></div></blockquote><div><br class=""></div><div>Yep.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">In those situations where I'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's been somewhat awkward to write compared to the code I would write if such a function exists.</div><div class=""><br class=""></div><div class="">That being said, I don't believe I've tried it since Swift 4 introduced the new partial range operators so it's possible that those simplify the pattern quite a bit.</div><div class=""><br class=""></div></div></div></div></blockquote><div><br class=""></div><div>Hopefully! I’ve found it that way in practice e.g.&nbsp;<a href="https://gist.github.com/airspeedswift/e7e02943c4c4463da919a9a825f33def#file-lazysplit-swift-L14" class="">https://gist.github.com/airspeedswift/e7e02943c4c4463da919a9a825f33def#file-lazysplit-swift-L14</a></div><div><br class=""></div><div>An alternative pattern is to create a mutable slice variable, and continuously “eat” from the front of it, removing elements as you go (via removeFirst or similar). Eating from the front of a slice ought to just move the startIndex further along the base collection.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">&nbsp;</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" class=""><div class="">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 class=""><br class=""></div><div class=""><div style="margin:0px;font-stretch:normal;font-size:16px;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)" class=""><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="color:#ba2da2" class="">var</span> a = <span style="color:#703daa" class="">Array</span>(<span style="color:#272ad8" class="">0</span>..&lt;<span style="color:#272ad8" class="">10</span>)</div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="color:#ba2da2" class="">let</span> isEven = { $0%<span style="color:#272ad8" class="">2</span> == <span style="color:#272ad8" class="">0</span> }</div><div style="margin:0px;font-stretch:normal;font-size:12px;line-height:normal;font-family:Helvetica;min-height:14px" class=""><br class=""></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="color:#4f8187" class="">a</span>[<span style="color:#272ad8" class="">3</span>...].<span style="color:#31595d" class="">remove</span>(where: <span style="color:#4f8187" class="">isEven</span>)</div><div style="margin:0px;font-stretch:normal;line-height:normal;color:rgb(0,132,0)" class="">// a = [0, 1, 2, 3, 5, 7, 9]</div><div class=""><br class=""></div></div></div><div class="">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 class=""><br class=""></div><div class=""><br class=""></div></div>_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</blockquote></div></div>
</div></blockquote></div><br class=""></body></html>