Let's say I have an array of object. and also assume that there are some attributes (attr1, attr2, ...attrn) in each object.
I would like to have function/class that can count over any attributes' values. e.g if object is person, and person has attributes, sex ('Male','Female'), ethnic ('a','b','c',...), occupation ('programmer','hacker',...).
If I want to find count# on sex =>
count( array_of_objects, 'sex') => returns
( 'Male' => 45 #male count, 'Female' => 54)
But in some cases, I would also need to group,
count(array, ['sex','occupation']) =>
( 'Male' => ( 'programmer' => 10, 'blah' => 34),
'Female' => ('blah' => 3, 'programmer'=>4)
How could I have generic function/class to group and count over collection?