Skip to content

Add a proposal for count(where:) #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2018
Merged

Conversation

khanlou
Copy link
Contributor

@khanlou khanlou commented Apr 23, 2018

This is a proposal for count(where:).

Pitch thread: https://forums.swift.org/t/count-where-on-sequence/11186

Recommended implementation: swiftlang/swift#16099


Counting the number of objects that pass a test has a wide range of uses in many domains. However, Swift currently doesn't give its users a simple way to perform this operation. While the behavior can currently be approximated with a `filter` and a `count`, this approach creates an intermediate array which it immediately discards. This is a bit wasteful.

[1, 2, 3, -1, -2].filter({ $0 > 0 }).count // => 3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can, instead of adding the four spaces, wrap these in backticks (```) and add syntax highlighting (```swift on the first line)

@khanlou
Copy link
Contributor Author

khanlou commented Jun 12, 2018

Just double checking on the status of this! Anything else I can do to push it forward?

@tkremenek
Copy link
Member

This review is going to be scheduled for the week of August 6.

@khanlou
Copy link
Contributor Author

khanlou commented Jul 20, 2018

That sounds great. Thanks for heads up, Ted!

@fbartho
Copy link

fbartho commented Jul 20, 2018

Why not use reduce to count things?

My specific point in raising this is that it bypasses the "extra intermediate array" concern (for filter + count) that was raised in the proposal.

@lattner lattner merged commit 59635bf into swiftlang:master Jul 20, 2018
@lattner
Copy link
Collaborator

lattner commented Jul 20, 2018

Scheduled for review August 6 - 12, 2018

@lattner
Copy link
Collaborator

lattner commented Jul 20, 2018

Now AKA SE-0220

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants