swlnet / google-collections

Automatically exported from code.google.com/p/google-collections
Apache License 2.0
0 stars 0 forks source link

Aggregation Methods #230

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I find myself writing code often that is a simple loop for doing sums, 
averages, etc of a collection.  For readability sake (and possibly later 
optimizations), it would be nice to be able to write

int total = Aggregation.sum(nums);

rather than

int total = 0;
for (Integer i : nums) {
  total += i;
}

It's a small low priority idea, but I thought I'd throw it out there.

(Kind of related would be a countIf method that takes a collection and 
counts the number of elements that cause the passed predicate to return 
true)

Original issue reported on code.google.com by brianopp on 6 Sep 2009 at 9:04

GoogleCodeExporter commented 9 years ago
Internally, Google has some statistics classes that do what you want. They take 
a
collection of a numeric elements as input and compute the sum, median, standard
deviation, etc. While those methods are useful, they probably don't belong in a
collection library.

To determine the number of elements that satisfy a predicate, say
  int count = Collections2.filter(collection, predicate).size();

It's not clear whether there should be an Iterables method that provides that 
logic
directly.

Original comment by jared.l....@gmail.com on 6 Sep 2009 at 6:05

GoogleCodeExporter commented 9 years ago
Couldn't this be solved with a Fold Function (Issue 218)?

Original comment by andre.ru...@gmail.com on 7 Sep 2009 at 11:22

GoogleCodeExporter commented 9 years ago
A fold/reduce method would also work, though a standalone sum() method would be 
just
as concise and more readable. Besides, as Kevin explained in issue 218, we 
aren't
eager to add more functional programming logic to the library.

Original comment by jared.l....@gmail.com on 8 Sep 2009 at 6:01

GoogleCodeExporter commented 9 years ago
Our math-related libraries are on my radar screen for the Guava project. Will 
close 
this one.

Original comment by kevin...@gmail.com on 17 Sep 2009 at 4:53