# SE-0207: Add a containsOnly algorithm to Sequence

**URL:** <https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686>\
**Category:** Proposal Reviews\
**Created:** [April 5, 2018, 4:38am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686 "2018-04-05T04:38:42Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [April 5, 2018, 4:38am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/1 "2018-04-05T04:38:43Z")

</div>

The review of [SE-0207: Add a containsOnly algorithm to Sequence](https://github.com/apple/swift-evolution/blob/master/proposals/0207-containsOnly.md) begins now and runs through April 13th, 2018.

Reviews are an important part of the Swift evolution process. All review feedback should be either on this forum thread or, if you would like to avoid this forum or just keep your feedback private, directly to me via email, twitter, or direct message. If emailing me directly, please put “SE-0207: Add a containsOnly algorithm to Sequence” in the subject line. Your feedback is equally valuable to us no matter how we receive it.

What goes into a review of a proposal?

The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift.

When reviewing a proposal, here are some questions to consider:

What is your evaluation of the proposal?

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

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

If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

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

Thanks,  
Dave Abrahams  
Review Manager

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [April 5, 2018, 8:31am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/2 "2018-04-05T08:31:24Z")

</div>

- What is your evaluation of the proposal?

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

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

- If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?

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

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [April 5, 2018, 9:18am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/3 "2018-04-05T09:18:59Z")

</div>

The functionality is useful and fits with the existing API. However—in the grand tradition of Swift Evolution—I am not sold on the name `containsOnly(where:)`. I spent some time pondering alternatives, and came up with two that seem plausible:

• `allMatch()` — `allEqual()`  
• `entirely()` — `isEntirely()`

For comparison, the proposal and the one alternative it mentions are:

• `containsOnly(where:)` — `containsOnly()`  
• `all()` — `all()`

Here is how these four options look in different scenarios:

```swift
// Trailing closure — check for straight-A student
if (grades.allMatch{ $0 >= 90 }) { … }
if (grades.entirely{ $0 >= 90 }) { … }
if (grades.containsOnly{ $0 >= 90 }) { … }
if (grades.all{ $0 >= 90 }) { … }

// Predicate function — check for blank string
if text.allMatch(isWhitespace) { … }
if text.entirely(isWhitespace) { … }
if text.containsOnly(where: isWhitespace) { … }
if text.all(isWhitespace) { … }

// Equatable element — check for the origin
if coordinates.allEqual(0) { … }
if coordinates.isEntirely(0) { … }
if coordinates.containsOnly(0) { … }
if coordinates.all(0) { … }

```

In the first and third examples, all these wordings are reasonable. However, in the middle example two of them are quite awkward:

```swift
text.allMatch(isWhitespace)
text.containsOnly(where: isWhitespace)

```

For `allMatch`, the problem is specific to `String`, as the name of the variable does not evoke being a `Sequence`. For `containsOnly(where:)` though, the problem is broader and stems from the argument label.

I am not sure what spelling I prefer, but I think we should consider alternatives.

---

<div class="post-metadata">

**Author:** ![nuclearace](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nuclearace](https://forums.swift.org/u/nuclearace)\
**Post date:** [April 5, 2018, 9:19am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/4 "2018-04-05T09:19:16Z")

</div>

I’m +1 to this. I use methods like this in other languages and find them very useful.

After reading the original thread I’ve been swayed from the term ‘all’ and think containsOnly is a better name for this.

---

<div class="post-metadata">

**Author:** ![nuclearace](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nuclearace](https://forums.swift.org/u/nuclearace)\
**Post date:** [April 5, 2018, 9:27am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/5 "2018-04-05T09:27:28Z")

</div>

Hm I do agree that the where in the predicate version looks a bit odd when you just supply a variable. But I think thats fine in the case where you’re supplying a closure directly. None of the alternatives I can think of are really much better.

Although I will say, when I started thinking of alternatives, the best and first that came to mind is one you pointed out. allEqual. So I think if we do consider alternatives, that is a strong one.

---

<div class="post-metadata">

**Author:** ![Tino](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tino/32/1073_2.png) [@Tino](https://forums.swift.org/u/Tino)\
**Post date:** [April 5, 2018, 10:39am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/6 "2018-04-05T10:39:25Z")

</div>

`containsOnly` has the small possibility that someone may think that

```
[1].containsOnly(1) // true
[1, 1, 1].containsOnly(1) // false?

```

I don't see it as a big danger, but `allEqual` leaves no room for such misinterpretation.

But imho we have to be careful with adding small helpers to the stdlib - if we had a dedicated library for collections, I'd probably put all extensions that aren't already included into such a package (together with some less common datatypes).

---

<div class="post-metadata">

**Author:** ![nuclearace](https://avatars.discourse-cdn.com/v4/letter/n/51bf81/32.png) [@nuclearace](https://forums.swift.org/u/nuclearace)\
**Post date:** [April 5, 2018, 10:52am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/7 "2018-04-05T10:52:05Z")

</div>

I definitely wouldn't mind spinning another discussion off of this one to discuss adding a Collections framework that would either ship with the toolchain, or be a package importable through SPM. Might even want to wrap the linked list proposal in that framework.

---

<div class="post-metadata">

**Author:** ![kaan](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kaan](https://forums.swift.org/u/kaan)\
**Post date:** [April 5, 2018, 12:59pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/9 "2018-04-05T12:59:08Z")

</div>

+1 from me too.

I've had to write this functionality in an extension more times than I can count.

I'm not too enthusiastic about the name `containsOnly()`. Would prefer seeing `all()` or `allMatch()` as mentioned above.

---

<div class="post-metadata">

**Author:** ![BalestraPatrick](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/balestrapatrick/32/730_2.png) [@BalestraPatrick](https://forums.swift.org/u/BalestraPatrick)\
**Post date:** [April 5, 2018, 3:07pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/10 "2018-04-05T15:07:02Z")

</div>

I really like this proposal since I often find myself writing a negation of `contains` and it takes a little bit of time every time I have to read again the code because of its suboptimal readability. I also agree on the naming `containsOnly` since it really fits in with the existing `contains`. Adding this new method with a completely different name compared to the existing one would definitely create some confusion.

---

<div class="post-metadata">

**Author:** ![Paul\_Cantrell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paul_cantrell/32/16764_2.png) [@Paul\_Cantrell](https://forums.swift.org/u/Paul_Cantrell)\
**Post date:** [April 5, 2018, 4:03pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/11 "2018-04-05T16:03:11Z")

</div>

+1 to the functionality, -0.5 to the name.

The proposed names are … acceptable, but not satisfying. Their asymmetry feels awkward to me. The terminology of `contains(where:)` has always felt a bit off when I use it, and feels more off now.

I often implement this pair myself as `any(match:)` and `all(match:)` simply because I find they read better in context. [Here is an example of that.](https://github.com/bustoutsolutions/siesta/blob/1.3.1/Source/SiestaUI/ResourceStatusOverlay.swift#L242-L248)

One could quibble with the details of that particular naming — this is Swift evolution, after all! — but I would add my voice to those strongly in favor of using the words **`any`** and **`all`** in _some_ form as the distinguishing feature of these two methods. The pair are a widely understood term of art:

- [Python uses them](https://docs.python.org/3/library/functions.html#all).
- [Ruby uses them](https://ruby-doc.org/core-2.5.0/Enumerable.html#method-i-all-3F).
- [Rust uses them](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.all).
- [Haskell](https://www.haskell.org/hoogle/?hoogle=any) [uses them](https://www.haskell.org/hoogle/?hoogle=all).
- [C#](https://msdn.microsoft.com/en-us/library/system.linq.enumerable.any(v=vs.110).aspx) [uses them](https://msdn.microsoft.com/en-us/library/bb548541(v=vs.110).aspx).
- [C++ uses them](http://www.cplusplus.com/reference/algorithm/all_of/).
- [Java recently added them](https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#anyMatch-java.util.function.Predicate-).
- (This list is hardly exhaustive.)

They have worked well in practice in all these languages — for at least a decade in the case of Python and Ruby!

Their wide usage is argument enough, I think. But their ubiquity is in this case a sign of good design: they communicate the right idea even to someone not yet familiar with them, and do not require the extra step of mental indirection that `contains` / `containsOnly` do.

---

<div class="post-metadata">

**Author:** ![jarod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jarod/32/364_2.png) [@jarod](https://forums.swift.org/u/jarod)\
**Post date:** [April 5, 2018, 7:34pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/12 "2018-04-05T19:34:58Z")

</div>

I'm happy to see this make it to a proposal! I participated in the original discussion and supported the proposed naming. I don't have much to add beyond that discussion, but here's a summary of my reasoning for choosing `containsOnly`:

- The alignment with `contains` makes it easy to remember, and it's easily discoverable via autocomplete when you try to use the inverted `contains` (i.e. `collection.contains { !expression }`).
- It is one of the only suggested names that satisfies this API design guideline: "Uses of Boolean methods and properties should read as assertions about the receiver". Following this rule is what makes `containsOnly` the most natural-sounding option to me.

Of the alternatives, I'm not a huge fan of `all` in particular because it looks to me like an alias of `filter`, i.e. `integers.all { $0.isEven }`

---

<div class="post-metadata">

**Author:** ![Paul\_Cantrell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paul_cantrell/32/16764_2.png) [@Paul\_Cantrell](https://forums.swift.org/u/Paul_Cantrell)\
**Post date:** [April 5, 2018, 8:17pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/13 "2018-04-05T20:17:00Z")

</div>

To be clear, I’m only voting in favor of the prefixes “any” and “all,” not necessarily in favor of those being the entire method name.

> [@jarod](#):
>
> It is one of the only suggested names that satisfies this API design guideline: “Uses of Boolean methods and properties should read as assertions about the receiver”.

Variants such as `anyMatch` / `allMatch` also satisfy that guideline, and also have the advantage of satisfying this one:

> **Embrace precedent.** Don’t optimize terms for the total beginner at the expense of conformance to existing culture.

At this point, per the links above, the programming community precedent for names starting with any\* / all\* is widespread and longstanding.

That said, if bringing `contains(where:)` into alignment with the rest of the programming world is not on the table, using an “all-” prefix without a corresponding “any-” doesn’t feel right.

> [@jarod](#):
>
> The alignment with contains makes it easy to remember, and it’s easily discoverable via autocomplete

FWIW, I spent an embarrassingly long time without even realizing that `contains(where:)` existed at all, and reimplementing it on every project. Perhaps this is just my own uniquely addled brain, but that name was not easily discoverable for me in actual experience; I’m skeptical that `containsOnly` would be.

---

<div class="post-metadata">

**Author:** ![jarod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jarod/32/364_2.png) [@jarod](https://forums.swift.org/u/jarod)\
**Post date:** [April 5, 2018, 9:41pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/14 "2018-04-05T21:41:44Z")

</div>

> [@Paul\_Cantrell](#):
>
> Variants such as anyMatch / allMatch also satisfy that guideline

This is maybe getting a little too far into the weeds, but I wouldn't consider them to satisfy the guideline because they read as assertions about the /contents/ of the receiver rather than the receiver itself. That sounds like a minor distinction, but I think it creates a grammatical issue.

Consider `if array.containsOnly(5)` vs. `if array.allMatch(5)`. The former reads naturally on its own as "if array contains only 5", while the latter requires inserting some additional implied words to read naturally: "if [the elements of] array all match 5".

That said:

> [@Paul\_Cantrell](#):
>
> and also have the advantage of satisfying this one:
> 
> > [@](#):
> >
> > **Embrace precedent.** Don’t optimize terms for the total beginner at the expense of conformance to existing culture.

This is certainly a good argument for `all` or `allMatch`. It's likely the name that developers coming from the languages you listed will initially reach for. I do agree though that it would be unfortunate to use that name without renaming `contains` to `any` or `anyMatch`, and that would be pretty heavily source-breaking.

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [April 5, 2018, 9:48pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/15 "2018-04-05T21:48:04Z")

</div>

> Consider `if array.containsOnly(5)` vs. `if array.allMatch(5)`. The former reads naturally on its own as “if array contains only 5”, while the latter requires inserting some additional implied words to read naturally: “if [the elements of] array all match 5”.

Meta-point: when doing these readability experiments, it's important not to use variable names that merely parrot their types, so I'd look at

> if gradePointAverages.containsOnly(5)

vs.

> if gradePointAverages.allMatch(5)

-Dave

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [April 5, 2018, 10:16pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/16 "2018-04-05T22:16:01Z")

</div>

> [@jarod](#):
>
> Consider `if array.containsOnly(5)` vs. `if array.allMatch(5)`. The former reads naturally on its own as “if array contains only 5”, while the latter requires inserting some additional implied words to read naturally: “if [the elements of] array all match 5”.

FYI, what I suggested above would only use `allMatch` with a predicate. When taking an `Equatable` element it would be spelled `allEqual`. Adopting Dave’s suggestion, that becomes:

```swift
if gradePointAverages.containsOnly(5)
if gradePointAverages.allEqual(5)

```

With a trailing closure it would be `allMatch`:

```swift
if (gradePointAverages.containsOnly{ $0 > 4 })
if (gradePointAverages.allMatch{ $0 > 4 })

```

And also with a predicate function:

```swift
if gradePointAverages.containsOnly(where: isPassingGrade)
if gradePointAverages.allMatch(isPassingGrade)

```

An alternative to `allMatch` would be `allSatisfy`, though personally I prefer `allMatch`.

> [@jarod](#):
>
> This is certainly a good argument for `all` or `allMatch`. It’s likely the name that developers coming from the languages you listed will initially reach for. I do agree though that it would be unfortunate to use that name without renaming `contains` to `any` or `anyMatch`, and that would be pretty heavily source-breaking.

I think it is fine to leave the existing `contains` as-is, regardless of how we spell the new method.

---

<div class="post-metadata">

**Author:** ![jarod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jarod/32/364_2.png) [@jarod](https://forums.swift.org/u/jarod)\
**Post date:** [April 5, 2018, 10:23pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/17 "2018-04-05T22:23:30Z")

</div>

> [@dabrahams](#):
>
> Meta-point: when doing these readability experiments, it’s important not to use variable names that merely parrot their types, so I’d look at
> 
> > if gradePointAverages.containsOnly(5)
> 
> vs.
> 
> > if gradePointAverages.allMatch(5)
> 
> -Dave

That's a good point that I hadn't considered. `allMatch` does read better when the variable is a pluralization of the elements, which is usually the case for a collection.

For what it's worth, existing collection APIs like `isEmpty` read better when the variable is named after the container rather than the elements, i.e. `array.isEmpty` vs. `gradePointAverages.isEmpty`. But both naming conventions are used, and it's impossible to accommodate both perfectly. At this point, I've seen enough tradeoffs in either direction that I'd be happy with either set of names.

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [April 5, 2018, 10:35pm UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/18 "2018-04-05T22:35:19Z")

</div>

Re the `all` family of names: one of the clear signals from the [SE-0204](https://forums.swift.org/t/se-0204-add-last-where-and-lastindex-where-methods-rename-index-of-and-index-where-methods/) thread (adding `lastIndex(where:)` is that people really don't like mismatched APIs. Presumably this would also go for having `all`/`contains` rather than `all`/`any`.

But we really _really_ cannot be renaming `contains` at this stage, purely so that we can introduce a new related method.\* If you value the symmetry, this pretty much locks us in to `containsOnly` instead.

Personally, I don't find the symmetry all that critical, so I'd be fine with ending up with `all`/`contains` (hence that was in the original proposal). But at the same time I don't feel that `all` or `allMatch` are any better than `containsOnly`.

\* the rename of `index(where:)` is a little different, since it was a clear outlier from the existing `first`/`last` family, and `index(where:)` didn't really fit in with it's siblings `index(after:)` etc which are all about index advancement, not search.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 6, 2018, 4:17am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/19 "2018-04-06T04:17:49Z")

</div>

> [@jarod](#):
>
> Consider `if array.containsOnly(5)` vs. `if array.allMatch(5)`. The former reads naturally on its own as “if array contains only 5”

Is it a correct English interpretation of “if array contains only 5” to make it match only `[5]`?

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [April 6, 2018, 4:45am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/20 "2018-04-06T04:45:22Z")

</div>

Correction: it has been pointed out to me that this review should run through the 13th, not the 6th as originally stated. I've updated the announcement accordingly.

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [April 6, 2018, 5:38am UTC](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686/21 "2018-04-06T05:38:24Z")

</div>

> [@gwendal.roue](#):
>
> Is it a correct English interpretation of “if array contains only 5” to make it match only [5]?

I would say yes.

[Next page](https://forums.swift.org/t/se-0207-add-a-containsonly-algorithm-to-sequence/11686.md?page=2)
