SE-0197 — Add in-place remove(where:)

What is your evaluation of the proposal?

+1

The following point is a bit orthogonal to the performance aspect, or improving the readability of filter meaning "keep", but I wanted to point out that if all you're looking to do is change a "keep" filter into a "remove" one then the readability of nums = nums.filter { !isOdd($0) } could be slightly improved by extending the ! operator to act on Bool returning functions, e.g.

prefix func !<D>(_ fn: @escaping (D) -> Bool) -> (D) -> Bool { return { !fn($0) } }

Then the example reduces to:

var nums = [1,2,3,4,5]
// remove odd elements
nums = nums.filter(!isOdd)

In-fact this may be useful for avoiding writing a closure when using this new in-place remove(where:) when simply the negation of a function is desired.

To remove even numbers, you could write nums.remove(where: !isOdd) for example.

That said, I think there is enough value in the performance aspect, and the convenience of being an in-place removal if dealing with an already mutable collection to warrant adding this new method.

Is the problem being addressed significant enough to warrant a change to Swift?

Yes.

Does this proposal fit well with the feel and direction of Swift?

Yes.

How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

A couple of quick reads though.

2 Likes