Calculate average - a shorter way

When you have a collection of Transactions objects and want to calculate its average amount, instead of looping through the collection like this:

double sum = 0;
for (Transaction *transaction in transactions) {
    sum += [transaction.amount doubleValue];
NSNumber *avg = [NSNumber numberWithDouble:(sum / [transactions count])];

you can reduce the loop to 1 line of code, using Objective-C key-value coding:

NSNumber *avg = [transactions valueForKeyPath:@"@avg.amount"];

Currently, there is a fixed set of collection operators:

  • Simple collection operators (@avg, @count, @sum, @max, @min)
  • Object operators (@distinctUnionOfObjects, @unionOfObjects)
  • Array and set operators (@distinctUnionOfArrays, @unionOfArrays)

For details, refer to more usage examples.