Or do the calculation and the amount in the same iteration:
tmp = @results.inject([0.0, 0]) { |sum, result|
if RankedResult === result
[sum.first + result.average_precision, sum.last+1]
else
sum
end
}
tmp.first / tmp.last
Or filter the collection first:
tmp = @results.filter { |result| RankedResult === result }
tmp.inject(0.0) { |sum, result| sum+result.avergage_precision } / tmp.length
source
share