# SE-0220: count(where:)

**URL:** <https://forums.swift.org/t/se-0220-count-where/15048>\
**Category:** Proposal Reviews\
**Created:** [August 6, 2018, 8:13pm UTC](https://forums.swift.org/t/se-0220-count-where/15048 "2018-08-06T20:13:15Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [August 6, 2018, 8:13pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/1 "2018-08-06T20:13:15Z")

</div>

The review of [SE-0220: count(where:)](https://github.com/apple/swift-evolution/blob/master/proposals/0220-count-where.md) begins now and runs through Sunday, August 12th, 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 keep your feedback private, directly to me as the review manager via email or direct message on the forums. If you send me email, please put "SE-0220" somewhere in the subject line.

**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?

As always, thank you for contributing to Swift.

Chris Lattner  
Review Manager

---

<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:** [August 6, 2018, 8:20pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/2 "2018-08-06T20:20:22Z")

</div>

+1 Fairly helpful addition to the standard library; actually a little surprised we're just now adding this. The motivation section of the proposal does a good job at showing how trying to implement this behavior can run into pitfalls. And I think the name is pretty self explanatory, and will definitely be found by people looking for this functionality out of the box.

---

<div class="post-metadata">

**Author:** ![Ponyboy47](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ponyboy47/32/2605_2.png) [@Ponyboy47](https://forums.swift.org/u/Ponyboy47)\
**Post date:** [August 6, 2018, 8:21pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/3 "2018-08-06T20:21:53Z")

</div>

+1

> [@Chris\_Lattner3](#):
>
> What is your evaluation of the proposal?

This would be a minimal, but worthwhile inclusion to the stdlib that will benefit a large number of developers.

> [@Chris\_Lattner3](#):
>
> Is the problem being addressed significant enough to warrant a change to Swift?

Yes, I've wanted this addition for a while since this is a frequent enough operation and the lack of it has been rather annoying. (I tend to go with the `.reduce` route in my own code to avoid creating the intermediate array)

> [@Chris\_Lattner3](#):
>
> Does this proposal fit well with the feel and direction of Swift?

This kind of additive change matches other similar types of proposals.

> [@Chris\_Lattner3](#):
>
> How much effort did you put into your review? A glance, a quick reading, or an in-depth study?

Read through the proposal and kept up with the associated pitch. The API is clean and easy/intuitive to find. This inclusion will definitely simplify some of my own projects.

---

<div class="post-metadata">

**Author:** ![griotspeak](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/griotspeak/32/1966_2.png) [@griotspeak](https://forums.swift.org/u/griotspeak)\
**Post date:** [August 6, 2018, 9:05pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/4 "2018-08-06T21:05:23Z")

</div>

**What is your evaluation of the proposal?**  
+1

**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

**If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?**  
Yes. It is as straightforward/efficient as I've seen.

**How much effort did you put into your review? A glance, a quick reading, or an in-depth study?**  
I read the proposal and have followed discussion here and there.

---

<div class="post-metadata">

**Author:** ![nnnnnnnn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nnnnnnnn/32/264_2.png) [@nnnnnnnn](https://forums.swift.org/u/nnnnnnnn)\
**Post date:** [August 6, 2018, 9:11pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/5 "2018-08-06T21:11:13Z")

</div>

```swift
let review = [
    "What is your evaluation of the proposal?":
    """
    ✅ This is a useful building-block operation that I \
       frequently have to write myself. It should absolutely \
       be a part of the standard library.
    """,
    
    "Is the problem being addressed significant enough to warrant a change to Swift?":
    """
    ✅ As the proposal describes, the naive approach \
       is needlessly inefficient and can be unclear \
       at the point of use.
    """,
    
    "Does this proposal fit well with the feel and direction of Swift?":
    """
    ✅ The naming is intuitive and matches other `Sequence` \
       operations, such as `first(where:)` and `lastIndex(where:)`.
    """,
    
    "If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?":
    """
    ✅ It matches the power & expressiveness of Ruby's `count`, \
       which is the closest parallel that I've used.
    """
]

```

```swift
let score = review.values.count(where: { $0.contains("✅") })

```

---

<div class="post-metadata">

**Author:** ![SiliconUnicorn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/siliconunicorn/32/3069_2.png) [@SiliconUnicorn](https://forums.swift.org/u/SiliconUnicorn)\
**Post date:** [August 6, 2018, 9:17pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/6 "2018-08-06T21:17:33Z")

</div>

This proposal seems like a worthwhile and useful change to Swift. It seems like it could be useful in a wide variety of situations, including ones relevant to my own development work. Furthermore, this change simplifies syntax, expanding upon the existing ideas of the language. I gave the proposal a thorough read-through.

---

<div class="post-metadata">

**Author:** ![timv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/timv/32/13304_2.png) [@timv](https://forums.swift.org/u/timv)\
**Post date:** [August 6, 2018, 9:45pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/7 "2018-08-06T21:45:55Z")

</div>

+1. The `count(where:)` method is fairly straight-forward to write yourself but in my experience people often don't identify that their `filter` + `count` approach is sub-optimal (both in readability and efficiency). Many codebases will benefit from this addition to the standard library.

I'd also like to see the addition of `count(of:)` which the proposal briefly mentions – it's certainly not as widely applicable as `count(where:)`, but I've had use cases for it. `count(of: x)` is a lot more readable than `count(where: { $0 == x })` and it would fit well in the standard library alongside the `Equatable` counterparts of `firstIndex(where:)`, `contains(where:)`, `split(whereSeparator:)`, etc.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [August 6, 2018, 9:50pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/8 "2018-08-06T21:50:47Z")

</div>

I thought we already had this in the standard library so that itself is enough for me to be +1 on this

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [August 7, 2018, 12:27am UTC](https://forums.swift.org/t/se-0220-count-where/15048/9 "2018-08-07T00:27:06Z")

</div>

+1. I don't have much to add beyond what other folks have already said. This is a worthwhile addition, and the proposal illustrates incredibly well how trying to piece this together with existing constructs leads to either **less readable** or **less efficient** code. That's evidence enough that `count(where:)` should be among the fundamental building blocks that sequences/collections in stdlib should provide.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [August 7, 2018, 12:53am UTC](https://forums.swift.org/t/se-0220-count-where/15048/10 "2018-08-07T00:53:47Z")

</div>

+1000000

I needed this just the other day so I could show the number of items in a category for a list of available categories.

My only other comment for this would be:

**FINALLY**

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [August 7, 2018, 1:30pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/11 "2018-08-07T13:30:36Z")

</div>

+1 Sure, but why not use the `for … in … where` syntax in the implementation?

```swift
extension Sequence {
    func count(where predicate: (Element) throws -> Bool) rethrows -> Int {
        var count = 0
        for element in self where try predicate(element) {
            count += 1
        }
        return count
    }
}

```

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [August 7, 2018, 7:20pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/12 "2018-08-07T19:20:01Z")

</div>

I have followed along from the start and could have used this so many times just within the last few weeks. Yes, please.

---

<div class="post-metadata">

**Author:** ![hooman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hooman/32/43536_2.png) [@hooman](https://forums.swift.org/u/hooman)\
**Post date:** [August 7, 2018, 7:52pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/13 "2018-08-07T19:52:25Z")

</div>

**What is your evaluation of the proposal?**  
+1

**Is the problem being addressed significant enough to warrant a change to Swift?**  
A tiny additive change: Yes.

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

**If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?**  
Yes. It is one of the best I've seen.

**How much effort did you put into your review? A glance, a quick reading, or an in-depth study?**  
Read the proposal and followed the discussion.

---

<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:** [August 8, 2018, 4:17am UTC](https://forums.swift.org/t/se-0220-count-where/15048/14 "2018-08-08T04:17:52Z")

</div>

+1. Simple, useful, enhances readability, likely to improve performance for many.

As ABI stability lands, I hope that we can be less shy about letting the standard library grow to include conveniences and ergonomic improvements like this. There's a point where conveniences go too far, but Swift is still well shy of it.

---

<div class="post-metadata">

**Author:** ![palimondo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/palimondo/32/1144_2.png) [@palimondo](https://forums.swift.org/u/palimondo)\
**Post date:** [August 8, 2018, 4:29am UTC](https://forums.swift.org/t/se-0220-count-where/15048/15 "2018-08-08T04:29:38Z")

</div>

+1 for all the reasons that were stated by others above.

On a meta level, I'm slightly worries that such minimal, common sense and uncontroversial extensions to Swift have to go through the disproportionately heavyweight Swift Evolution process. See [exhibit B](https://forums.swift.org/t/add-filter-and-ifsome-then-to-optional/8143/44).

---

<div class="post-metadata">

**Author:** ![salutis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/salutis/32/1156_2.png) [@salutis](https://forums.swift.org/u/salutis)\
**Post date:** [August 8, 2018, 1:45pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/16 "2018-08-08T13:45:54Z")

</div>

+1 Needed this method in pretty much every project I’ve ever worked on. Finally!

---

<div class="post-metadata">

**Author:** ![nickmoore](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nickmoore](https://forums.swift.org/u/nickmoore)\
**Post date:** [August 14, 2018, 7:36am UTC](https://forums.swift.org/t/se-0220-count-where/15048/17 "2018-08-14T07:36:06Z")

</div>

At last, a proposal simple enough that I feel qualified to comment on 🙂

+1, yes please (though it already looks like a slam-dunk), and a well made argument in the proposal.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [August 14, 2018, 9:37am UTC](https://forums.swift.org/t/se-0220-count-where/15048/18 "2018-08-14T09:37:58Z")

</div>

**What is your evaluation of the proposal?**

Simple and elegant additional feature which should be added to the stdlib.

**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?**

Quick reading of the proposal.

---

<div class="post-metadata">

**Author:** ![abdulajet](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/abdulajet/32/18991_2.png) [@abdulajet](https://forums.swift.org/u/abdulajet)\
**Post date:** [August 14, 2018, 10:25am UTC](https://forums.swift.org/t/se-0220-count-where/15048/19 "2018-08-14T10:25:09Z")

</div>

**What is your evaluation of the proposal?**

+1. I really like the addition

**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?**

Reading of the proposal.

---

<div class="post-metadata">

**Author:** ![possen](https://avatars.discourse-cdn.com/v4/letter/p/ed8c4c/32.png) [@possen](https://forums.swift.org/u/possen)\
**Post date:** [August 14, 2018, 7:42pm UTC](https://forums.swift.org/t/se-0220-count-where/15048/20 "2018-08-14T19:42:30Z")

</div>

+1 good addition.

[Next page](https://forums.swift.org/t/se-0220-count-where/15048.md?page=2)
