<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="">Alwyn just pointed out another use-case as well.<div class=""><br class=""></div><div class="">If there is either a @discardableResult or two variants (one which returns a result) where the method returns the items being removed (which is to be expected of a method named “remove”), then it becomes useful in ways which filter alone is not. &nbsp;Here is how it would be used with a use-case from a project I was working on this weekend:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var longWords = allWords //A very long list of words passed as a parameter</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let shortWords = longWords.remove(where: {$0.characters.count &lt; 4})</div><div class=""><br class=""></div><div class="">compare that to either filtering twice:</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let longWords = allWords.filter({$0.characters.count &gt;= 4})</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let shortWords = allWords.filter({$0.characters.count &lt; 4}) //We are looping over a very long list twice</div><div class=""><br class=""></div><div class="">or what I ended up doing:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var longWords:[String] = []</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var shortWords:[String] = []</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>for word in allWords {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>if word.characters.count &lt; 4 {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>shortWords.append(word)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}else{</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                        </span>longWords.append(word)</div><div class=""><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 26, 2017, at 2:59 AM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class=""><div class="gmail_quote"><div dir="auto" class="">On Tue, Sep 26, 2017 at 00:15 Jonathan Hull &lt;<a href="mailto:jhull@gbis.com" class="">jhull@gbis.com</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" class="">As he says, it is an in-place equivalent of filter, so the use-cases would be similar.&nbsp; I could see this being extremely useful.&nbsp; Off the top of my head:<div class=""><br class=""></div><div class=""><span class="m_6311531772765348523Apple-tab-span" style="white-space:pre-wrap">        </span>views.remove(where: {$0.isHidden}) //Remove all views which are hidden from the list.</div></div></blockquote><div dir="auto" class=""><br class=""></div><div dir="auto" class="">Is such a method ever going to be different (in performance, say) from:</div><div dir="auto" class=""><br class=""></div><div dir="auto" class="">views = Views(views.lazy.filter { $0.isHidden })</div><div dir="auto" class=""><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" class=""><div class=""></div></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">Another thing which seems to be missing (although it could be there and I don’t know the name) is the ability to split a list into two using a filter (one with the filtered items and one with the remainder).&nbsp; I am surprised every time I reach for it and it isn’t there (the last time being yesterday). &nbsp;</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""><div class=""><div class=""><br class=""></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Sep 25, 2017, at 9:55 PM, Xiaodi Wu 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_6311531772765348523Apple-interchange-newline"><div class="">What is the use case?<br class=""><div class="gmail_quote"><div class="">On Mon, Sep 25, 2017 at 23:27 Félix Cloutier &lt;<a href="mailto:felixcloutier@icloud.com" target="_blank" class="">felixcloutier@icloud.com</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" class="">Actually, IMO, it's an oversight that there's no remove(where:), or another in-place equivalent to `filter`. I'm in favor of it.<div class=""><br class=""></div><div class=""></div></div><div style="word-wrap:break-word" class=""><div class="">Félix</div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">Le 25 sept. 2017 à 15:17, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank" class="">xiaodi.wu@gmail.com</a>&gt; a écrit :</div><br class="m_6311531772765348523m_3487363041747758027Apple-interchange-newline"><div class=""><div class="">On Mon, Sep 25, 2017 at 4:55 PM, Xiaodi Wu <span class="">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank" class="">xiaodi.wu@gmail.com</a>&gt;</span> wrote:<br class=""><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Brent has a great proposal in the pipeline regularizing the names of some of these functions and filling in some of the more glaring gaps.<br class=""><br class="">With regard to the specific items proposed here, Felix shows that ‘filter’ provides an idiomatic one-line way of doing some of what is proposed; currently remove(index(of:)) and operating on sliced would accomplish the rest. Therefore, I do not think these proposed additions meet the very high bar for expansion of the standard library API.</blockquote><div class=""><br class=""></div><div class="">I should add, however, it is wonderful (IMO) that more people are thinking about these APIs; welcome and thank you for restarting this very important conversation. It would be nice to get some more eyeballs on the previously discussed set of rationalizations to the Collection APIs so that we can make their use a little more ergonomic--with any luck, some better names for existing extension methods and filling in a very few gaps judiciously would allow us to make the existing facilities sufficiently more discoverable that it will be easier to accomplish what you seek without adding more extensions.</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_6311531772765348523m_3487363041747758027HOEnZb"><div class="m_6311531772765348523m_3487363041747758027h5"><div class="gmail_quote"><div class="">On Mon, Sep 25, 2017 at 11:14 Félix Cloutier via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" 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" class=""><div class="">Another alternative is to use `array = array.filter { $0 != someElement }`.</div><div class=""><br class=""></div><div class="">I thought that there would be a `remove(where:)` method, but there isn't.</div></div><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class="">Félix</div></div><div style="word-wrap:break-word" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">Le 25 sept. 2017 à 02:12, Alwyn Concessao via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="m_6311531772765348523m_3487363041747758027m_-3630028882392956976m_8781221207431004162Apple-interchange-newline"><div class=""><div class=""><div style="font-family:Helvetica Neue,Helvetica,Arial,sans-serif;font-size:13px" class=""><div id="m_6311531772765348523m_3487363041747758027m_-3630028882392956976m_8781221207431004162ydpd3132f34yiv4746156552" class=""><div class=""><div style="font-family:Helvetica Neue,Helvetica,Arial,sans-serif;font-size:13px" class=""><div class="">Hello,</div><div class=""><br class=""></div><div class="">After going through the Swift standard library functions provided for removing elements from a collection, one common pattern can be observed in all those functions and that is the functions provide to remove elements from the collection by passing the position or index of the element or passing a range of indices or positions to remove the elements.The standard library does not provide options to remove an element from a collection by passing the actual element &nbsp;to be removed directly to the remove method.I've encountered this situation many times when programming in Swift wherein I want an element or a set of elements to be removed directly without always accessing it's index in the collection but I have always ended up having to first access the index of the element or elements which I want to remove and then pass that index to the remove method.</div><div class=""><br class=""></div><div class="">The idea is to have an extension of the RangeReplaceableCollection protocol to include a method to remove elements from a collection by passing directly the element to be removed to the remove method and also include methods to remove multiple elements from the collection by passing in a sequence of the elements to be removed to the remove method and to remove an element in a particular subrange of the collection.</div><div class=""><br class=""></div><div class="">The prototype of the methods will be as follows -&nbsp;</div><div class=""><br class=""></div><div class="">extension RangeReplaceableCollection where Element:Equatable{</div><div class=""><br class=""></div><div class="">mutating func removeElement(_ elementToBeRemoved:Element){</div><div class=""><br class=""></div><div class="">//check if elementToBeRemoved exists ;if yes, remove all occurrences of elementsToBeRemoved from the collection.</div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">mutating func removeElementInSubrange(_ elementToBeRemoved:Element,in range:Range&lt;Index&gt;){</div><div class=""><br class=""></div><div 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 class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">mutating func removeContentsOf&lt;C:Collection&gt;(_ elementsToBeRemoved:C){</div><div class=""><br class=""></div><div class="">//check if each element in the elementsToBeRemoved sequence exists in the collection, if the element exists, remove it.</div><div class=""><br class=""></div><div class="">}</div><div class=""><br class=""></div><div class="">I've implemented the above in the pull request&nbsp;<a href="https://github.com/apple/swift/pull/12058" target="_blank" class="">https://github.com/apple/swift/pull/12058</a>&nbsp;</div><div class=""><br class=""></div><div class="">Any thoughts/suggestions on this are appreciated.</div><div class=""><br class=""></div><div class="">Thanks!</div><div class=""><br class=""></div><div class="">Alwyn</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""><p style="margin:0px;font-size:22px;line-height:normal;font-family:'Helvetica Neue';color:rgb(19,55,255)" class=""></p><div class=""><br class=""></div><div class=""><br class=""></div></div></div></div></div></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" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></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=""></div></div>
</div></blockquote></div><br class=""></div></div></div></blockquote></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" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></div></blockquote></div></div>
</div></blockquote></div><br class=""></div></body></html>