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 method #702

Closed
justin808 opened this issue Sep 7, 2014 · 11 comments
Closed

_.count method #702

justin808 opened this issue Sep 7, 2014 · 11 comments

Comments

@justin808
Copy link

Would it make sense for lodash to have a _.count method?

Like this: http://ruby-doc.org/core-2.1.2/Enumerable.html#method-i-count
I'd be happy to implement one and submit a PR if this is appropriate.

@jdalton
Copy link
Member

jdalton commented Sep 7, 2014

Thanks for asking. This looks like a combo of a couple of existing methods.
There's _.filter & _.size which can be used to come to similar results.

We'll keep an eye on the popularity of the request.

@nickperkinslondon
Copy link

👍 for _.count ( and _.countWhere)

I was surprised to find that these don't already exist! Wouldn't it be nice, when you just want to count something, to simply reach for the _.count() function?

Of course you can do it with "filter/length" but that seems a tiny bit awkward, and counting is such a simple and fundamental concept -- it deserves it's own function. ( plus, there's the performance thing -- you could implement it without creating a new array ).

@c-dante
Copy link

c-dante commented Mar 19, 2016

My only conflict is how count is useful in applications -- I'm trying to think of times where I want how many satisfy a predicate and not which -- I keep coming to UI concerns only.

Is there a meaningful algorithm or use case you can come up with that just wants to know how many and not which? Or rather, one such that you do not want the filtered set (either side)?

Edit:
Oh. We get _.sumBy. So here's your count:

_.sumBy(collection, (x) => pred(x) ? 1 : 0)

@jdalton
Copy link
Member

jdalton commented Mar 19, 2016

@c-dante Oh neat!

_.sumBy(collection, _.flow(pred, Boolean));

@lindem
Copy link

lindem commented Jun 8, 2016

Just stumbled across this (because I need to count predicate-satisfying values); I'd like to add that _.count(seq, predicate) is also self-documenting code. I think _.countBy() just doesn't achieve the same degree of simplicity, its return value always being an object. I am really surprised that there's opposition to this particular concept of just returning a number.
EDIT: That's 👍 for a simple _.count() returning a number.

@c-dante
Copy link

c-dante commented Aug 1, 2016

I'm for a mixin of:

_.count = _.sumBy(collection, _.flow(pred, Boolean));

Whether its implemented that way or not. It's pretty clear what would be expected from:

_.count(myNumbers, x => x > 5);  // I read this is, "how many of myNumbers are > 5

_.count(name.split(''), x => x === 'a'); // How many "a" are in name?

_.count(resturants, x => distanceFromMe(x) > 10mi); // How many resturants are within 10mi of my location

@mologie
Copy link

mologie commented Aug 30, 2016

There also is an argument of efficiency to make here.

Most combined functions which achieve the behavior described in this ticket will build some kind of temporary object, which is then further evaluated for deriving the final count. Obvious examples are the filter+length and the map+sum approaches described above.

A more efficient implementation would only require a single integer for counting. This ticket therefore has my vote, because bits are precious.

@SmedbergM
Copy link

I'd upvote this too. _.sumBy(collection, predicate) works but is unidiomatic.

@gunar
Copy link

gunar commented Mar 2, 2017

The predicate needs to return a number not a boolean, hence

_.sumBy(collection, _.flow(pred, Number));

@SmedbergM
Copy link

The predicate needs to return a number not a boolean

In a type-safe world, sure. But + coerces booleans into numbers, which is why I said it works.

@lodash lodash deleted a comment from vance Dec 22, 2017
@lock
Copy link

lock bot commented Dec 26, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

8 participants