Skip to content
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

count(itr) to count trues #20403

Closed
cossio opened this issue Feb 2, 2017 · 4 comments
Closed

count(itr) to count trues #20403

cossio opened this issue Feb 2, 2017 · 4 comments
Labels
collections Data structures holding multiple items, e.g. sets

Comments

@cossio
Copy link
Contributor

cossio commented Feb 2, 2017

I think it would be useful to add a method of count that takes a single iterable argument of booleans and returns the number of true elements.

For example (imagine that the function iseven does not exist), I find this:

count(i % 2 == 0 for i in 1:10)

to be more readable than this:

count(i -> i % 2 == 0, 1:10)
@cossio
Copy link
Contributor Author

cossio commented Feb 2, 2017

I could do a PR if someone points me to the correct source file.

@nalimilan
Copy link
Member

You can find out where the method lives with:

julia> @which count(isodd, 1:2)
count(pred, itr) in Base at reduce.jl:654

@ararslan
Copy link
Member

ararslan commented Feb 2, 2017

This change could be as simple as

count(itr) = count(identity, itr)

That definition matches what we do with other reduction functions.

@ararslan ararslan added the collections Data structures holding multiple items, e.g. sets label Feb 2, 2017
@stevengj
Copy link
Member

stevengj commented Feb 2, 2017

countnz(i % 2 == 0 for i in 1:10) works (as does sum), but since we already have count I agree that a one-argument version makes sense.

stevengj added a commit that referenced this issue Feb 2, 2017
stevengj added a commit that referenced this issue Feb 2, 2017
@cossio cossio closed this as completed Feb 2, 2017
@cossio cossio reopened this Feb 2, 2017
stevengj added a commit to stevengj/julia that referenced this issue Feb 3, 2017
stevengj added a commit to stevengj/julia that referenced this issue Feb 3, 2017
ararslan pushed a commit that referenced this issue Feb 3, 2017
… encountered (#20421)

* add count(itr) (fixes #20403) and throw and error in count if non-boolean values are encountered (fixes #20404)

* fix reliance on sum behavior of count in length(t::ImmutableDict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

No branches or pull requests

4 participants