Cats: Add `count` method to Foldable

Created on 30 Aug 2018  路  6Comments  路  Source: typelevel/cats

I'm thinking of this one here:

def count[A](p: A => Boolean): Long

We could also provide these specialized for the various collections we have :)

All 6 comments

count(f) = foldMap { if (f(_)) 1 else 0 }

I think we need commutative foldMap, which we could use here and get probably all of the performance wins of a specialized function.

Yes we could define it on UnorderedFoldable :)
Also we might want to consider returning Long to be consistent with size.

+1 to use Long, +1 to put it on UnorderedFoldable.

PS: we do have a commutative foldMap for those following along: UnorderedFoldable.unorderedFoldMap.

Could I take this?

@leusgalvan Sure! Go right ahead :)

Was this page helpful?
0 / 5 - 0 ratings