[swift-evolution] Additional methods for removing elements from a collection in Swift
Alwyn Concessao
alsav196 at yahoo.com
Tue Sep 26 04:37:33 CDT 2017
First of all thanks to you all for the suggestions/thoughts on this idea.
@Jonathan Hull - You've mentioned about splitting a list using a filter which returns the filtered list and the remainder.Is it similar to having a filter function which does the filtering on the original collection and also returns the elements which are removed from the collection?
@Félix Cloutier and @Robert Bennett - Yes, in-place filter method is not provided by Swift.The in-place filter method would also take care of removing elements without actually accessing it's index.
Alwyn On Tuesday, 26 September 2017, 12:00:23 PM IST, Félix Cloutier <felixcloutier at icloud.com> wrote:
Same as `filter`, but in-place. We thought that `sorted` deserved an in-place version; that +(Array, Array) deserved an in-place version (append(contentsOf:)); there are parallels to be made between `dropFirst` and `removeFirst`; we have index and formIndex. There's a ton more, Swift loves to have a mutating and a non-mutating version of everything. Not sure why we should break consistency and not have an in-place version of filter.
Félix
Le 25 sept. 2017 à 21:55, Xiaodi Wu <xiaodi.wu at gmail.com> a écrit :
What is the use case?
On Mon, Sep 25, 2017 at 23:27 Félix Cloutier <felixcloutier at icloud.com> wrote:
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.
Félix
Le 25 sept. 2017 à 15:17, Xiaodi Wu <xiaodi.wu at gmail.com> a écrit :
On Mon, Sep 25, 2017 at 4:55 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
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.
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.
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.
On Mon, Sep 25, 2017 at 11:14 Félix Cloutier via swift-evolution <swift-evolution at swift.org> wrote:
Another alternative is to use `array = array.filter { $0 != someElement }`.
I thought that there would be a `remove(where:)` method, but there isn't.
Félix
Le 25 sept. 2017 à 02:12, Alwyn Concessao via swift-evolution <swift-evolution at swift.org> a écrit :
Hello,
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 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.
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.
The prototype of the methods will be as follows -
extension RangeReplaceableCollection where Element:Equatable{
mutating func removeElement(_ elementToBeRemoved:Element){
//check if elementToBeRemoved exists ;if yes, remove all occurrences of elementsToBeRemoved from the collection.
}
mutating func removeElementInSubrange(_ elementToBeRemoved:Element,in range:Range<Index>){
//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.
}
mutating func removeContentsOf<C:Collection>(_ elementsToBeRemoved:C){
//check if each element in the elementsToBeRemoved sequence exists in the collection, if the element exists, remove it.
}
I've implemented the above in the pull request https://github.com/apple/swift/pull/12058
Any thoughts/suggestions on this are appreciated.
Thanks!
Alwyn
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170926/31d55270/attachment.html>
More information about the swift-evolution
mailing list